Format DateTime as yyyy-MM-dd

You want ISO8601 formats in SQL Server, here you go.

For yyyy-MM-dd :

CONVERT(VARCHAR(10), GetDate(), 120)

e.g.

select CONVERT(VARCHAR(10), GetDate(), 120)
2017-02-02

Format "120" is 24-hour time, down to the second. But we limit the size of the result to remove the time component.

Or for the ever useful sortable reporting month: yyyy-MM

Select CONVERT(VARCHAR(7), GetDate(), 120)
2017-02

To get milliseconds (and 24 hour time)

Select CONVERT(VARCHAR(23), GetDate(), 121)
2017-02-02 11:52:33.980

(Any length 23 or larger will suffice)

See also

results matching ""

    No results matching ""