Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL convert string to date format 112

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.

like image 454
McG369 Avatar asked Jun 25 '13 15:06

McG369


2 Answers

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

like image 60
Taryn Avatar answered Sep 23 '22 23:09

Taryn


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);
like image 43
Asif Mahamud Avatar answered Sep 24 '22 23:09

Asif Mahamud