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.

McG369
  • 192
  • 1
  • 2
  • 8

2 Answers2

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

Taryn
  • 242,637
  • 56
  • 362
  • 405
  • 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. – McG369 Jun 25 '13 at 15:33
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);
Asif Mahamud
  • 583
  • 2
  • 10
  • 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.* – Tamas Rev Dec 12 '19 at 16:21