Monday, March 19, 2012

Formatting Dates as YYYY/MM/DD for a particular query

Anyone know how to format dates from 10/3/05 to 2005/10/03 in a query?
The only way I know is (cast(year(srecordeddate) as char(4)))+ cast('/'as char) + cast (month(srecordeddate) as char(2))... Which seems so rediculous. There's gotta be a better way!select convert(varchar(10), getdate(), 111)|||Anyone know how to format dates from 10/3/05 to 2005/10/03 in a query?

The only way I know is (cast(year(srecordeddate) as char(4)))+ cast('/'as char) + cast (month(srecordeddate) as char(2))... Which seems so rediculous. There's gotta be a better way!

With the caveat that it's almost ALWAYS better to handle formatting on the client side, you might try:

Convert(varchar(10), [YourDate], 120)

Regards,

hmscott|||select convert(varchar(10), getdate(), 111)
Thanks! You've bailed me out of another toughee...

No comments:

Post a Comment