Cast or Convert string to DateTime???

  • Hi all,

    I need to take a string of format '01232003' and compare it with today's date. Converting GetDate() to a string is fine, but not for comparing across years, like 2002 and 2003. Does anyone know of a good way to compare these values? Thanks.

  • If the string was in the YYYYMMDD format, SQL will implicitly convert it to a date format.

    You could convert it as follows.....

    set @strDate = '01232003'

    set @strDate = right(@strDate, 4) + left(@strDate, 4)

    print @strDate

  • Thank you very much. That was a simple trick, but would have taken me a while to figure out. Much appreciated.

    -Pat

  • Converting to a string does work, but only if you use the yyyy-mm-dd format. This should work for you if you want to use CONVERT:

    CONVERT(CHAR(10), getdate(), 20)

    -SQLBill

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply