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
This question is related to
sql-server
database
date
datetime
varchar
In SQL Server, you can do:
select coalesce(format(try_convert(date, col, 112), 'yyyyMMdd'), col)
This attempts the conversion, keeping the previous value if available.
Note: I hope you learned a lesson about storing dates as dates and not strings.
SELECT TO_CHAR(created_at, 'YYYY-MM-DD') FROM table; //converts any date format to YYYY-MM-DD
You can do as follows:
Select Format(test.Time, 'yyyyMMdd')
From TableTest test
Select CONVERT(VARCHAR(8), GETDATE(), 112)
Tested in SQL Server 2012
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
try this....
SELECT FORMAT(CAST(DOB AS DATE),'yyyyMMdd') FROM Employees;
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
SELECT YEAR(getdate()) * 10000 + MONTH(getdate()) * 100 + DAY(getdate())
Source: Stackoverflow.com