Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server date format yyyymmdd

Tags:

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

like image 329
Kevin M Avatar asked Dec 27 '16 23:12

Kevin M


People also ask

How do you convert date format from Yyyymmdd to yyyy-mm-dd in SQL?

Convert Char 'yyyymmdd' back to Date data types in SQL Server. Now, convert the Character format 'yyyymmdd' to a Date and DateTime data type using CAST and CONVERT. --A. Cast and Convert datatype DATE: SELECT [CharDate], CAST([CharDate] AS DATE) as 'Date-CAST', CONVERT(DATE,[CharDate]) as 'Date-CONVERT' FROM [dbo].


2 Answers

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 
like image 98
John Cappelletti Avatar answered Sep 20 '22 14:09

John Cappelletti


DECLARE @v DATE= '3/15/2013'  SELECT CONVERT(VARCHAR(10), @v, 112) 

you can convert any date format or date time format to YYYYMMDD with no delimiters

like image 28
Daniel Avatar answered Sep 18 '22 14:09

Daniel