How to get a date in YYYY-MM-DD format from a TSQL datetime field?

Kinze picture Kinze · May 20, 2009 · Viewed 1.2M times · Source

How do I retrieve a date from SQL Server in YYYY-MM-DD format? I need this to work with SQL Server 2000 and up. Is there a simple way to perform this in SQL Server or would it be easier to convert it programmatically after I retrieve the result set?

I've read the CAST and CONVERT on Microsoft Technet, but the format I want isn't listed and changing the date format isn't an option.

Answer

Darrel Miller picture Darrel Miller · May 20, 2009
SELECT CONVERT(char(10), GetDate(),126)

Limiting the size of the varchar chops of the hour portion that you don't want.