SQL Server date format yyyymmdd

Kevin M picture Kevin M · Dec 28, 2016 · Viewed 155.4k times · Source

I have a varchar column where some values are in mm/dd/yyyy format and some are in yyyymmdd.

I want to convert all mm/dd/yyyy dates into the yyyymmdd format. What is the best way to do this? Thanks

Table is Employees and column is DOB

Answer

John Cappelletti picture John Cappelletti · Dec 28, 2016

Assuming your "date" column is not actually a date.

Select convert(varchar(8),cast('12/24/2016' as date),112)

or

Select format(cast('12/24/2016' as date),'yyyyMMdd')

Returns

20161224