SQL convert string to date format 112
Asked Answered
T

2

5

I have an input date and I need to convert it to format 112 (yyyymmdd) for use later on in my SQL statement.

declare @day varchar(10)

set @day = '6/21/2013'

select @day

I've done this before...IDK if it's because I'm on SQL 2000 for this project that it's not working now.

Tektite answered 25/6, 2013 at 15:22 Comment(0)
M
9

I would convert it to a datetime first, then to the format that you want:

declare @day varchar(10)

set @day = '6/21/2013'

select convert(varchar(10), cast(@day as datetime), 112);

See SQL Fiddle with Demo

Mol answered 25/6, 2013 at 15:24 Comment(1)
I was using a request parameter (not manually set like in example) and it was having issues with quotes. Thanks this is the way to do it.Tektite
S
2

You can try this code from this novice user of this site.

declare @day varchar(10);

set @day = '6/21/2013';
select convert(date,@day,112);
Sprouse answered 25/6, 2013 at 15:43 Comment(1)
This gives me the error code: The input character string does not follow style 112, either change the input character string or use a different style.Raggletaggle

© 2022 - 2024 — McMap. All rights reserved.