How to handle MMddyyyy date format in SQL Server? - sql-server

I am getting the date in following string formats from third party vendor API:
MM/dd/yyyy
yyyy/MM/dd
yyyyMMdd
yyyy-MM-dd
MMddyyyy
So some of the records have date of birth in yyyy-MM-dd and some of the records have DOB in other format.
Except format MMddyyyy, all the formats are converted to yyyy-MM-dd using SQL Convert/Cast function.
How do I convert MMddyyyy it to yyyy-MM-dd?
I am trying below statement in SQL Server 2019 but its not working:
DECLARE #dt varchar(8)= '02022020';
SELECT CAST(#dt as date) s1;
Msg 241, Level 16, State 1, Line 20
Conversion failed when converting date and/or time from character string.
Is there any way I can handle this scenario in SQL Server?
Be informed: I am trying to implement below ADF code using SQL stored procedure.
coalesce(toDate(DOB,'MM/dd/yyyy')
,toDate(DOB,'yyyy/MM/dd')
,toDate(DOB,'yyyyMMdd')
,toDate(DOB,'MMddyyyy')
,toDate(DOB,'yyyy-MM-dd'))

There is no style code for MMddyyyy as far as I am aware, so the easiest way is probably to just convert the string to yyyyMMdd first, e.g.
CONCAT(RIGHT(#dt, 4), LEFT(#dt, 4))
Then convert that to a date:
DECLARE #dt varchar(8)= '02022020';
SELECT CONVERT(DATE, CONCAT(RIGHT(#dt, 4), LEFT(#dt, 4)), 112)

Below is the SQL statements to handle all the formats.
MM/dd/yyyy
yyyy/MM/dd
yyyyMMdd
yyyy-MM-dd
MMddyyyy
DECLARE #date varchar(10)= '2020-03-15';
SELECT CASE
WHEN #date IS NULL
THEN NULL
WHEN TRY_CONVERT(DATE, #date) IS NOT NULL
THEN CAST(#date AS DATE)
WHEN TRY_CONVERT(DATE, #date) IS NULL
THEN CONVERT(DATE, CONCAT(RIGHT(#date, 4), LEFT(#date, 4)), 112)
END;

Have you tried the following? SELECT FORMAT (yourdate, 'MM-dd-yy'). let me know

Related

How to convert from DD/mm/yyyy in varchar(max) to yyyymmdd format in SQL Server

Dates in DD/mm/yyyy format stored in a varchar(max) column need to be converted to yyyymmdd format.
The below options to convert / cast are not supported.
CONVERT(VARCHAR(19), CONVERT(DATETIME, wfm.date, 3), 112)
CONVERT(VARCHAR(50), CAST(wfm.date AS DATETIME2), 112)
CAST(CAST(wfm.date AS DATETIME2(15)) AS DATETIME)
I'm getting an error message for CONVERT syntax:
Conversion failed when converting date and/or time from character string.
For CAST syntax, I'm getting this error:
Line 1: Specified scale 15 is invalid. 'dd/mm/yyyy/ in varchar to be converted to yyyymmdd format
Ideally, don't store dates as a varchar, store them as a date and worry about the format they are displayed in in your presentation layer. Therefore, you should just do:
SELECT TRY_CONVERT(date,YourVarcharMAXDate,103);
If you must get the format yyyyMM`` (i don't recommend, change your data type of your column todate), then you can use a furterhCONVERT`:
SELECT CONVERT(varchar(8),TRY_CONVERT(date,YourVarcharMAXDate,103),112);
If the data engine can't convert the date, due to it being an invalid format, TRY_CONVERT will return NULL; you'll need to fix those values (make them valid). Another good reason why not to use a varchar to store a date.
Maybe this helps
DECLARE #dt DATETIME= '2019-12-31 14:43:35.863';
SELECT CONVERT(VARCHAR(10), #dt, 11) s1,
CONVERT(VARCHAR(10), #dt, 111) s2;
You could always just use substring for this. Example is shown below.
DECLARE #samples TABLE
(
Dates NVARCHAR(50)
)
INSERT INTO #samples VALUES
('10/11/2018'),
('12/11/2018'),
('15/11/2018'),
('17/11/2018'),
('19/11/2018'),
('20/11/2018')
SELECT
SUBSTRING(Dates, 7,4) +
SUBSTRING(Dates, 4,2) +
SUBSTRING(Dates, 1,2) as [NewDate]
FROM #samples
Output will be a list of strings with your required format. This won't validate for you, but you can always try convert these to a date using CAST and it should correctly cast the yyyyMMdd to a date if the date is correct.
If you're using SQL Server 2016 or newer, you can use PARSE and FORMAT to do this quite easily:
SELECT
FORMAT(PARSE(wfm.Date AS DATE USING 'de-DE'), 'yyyyMMdd')
FROM
dbo.WhateverYourTableNameIS
The dd/mm/yyyy format is the format used e.g. in Germany, that's why I'm using the de-DE culture to convert that string to a DATE. Then you can format it using the usual formatting strings as used in C#/.NET.
select convert(varchar, convert(datetime, wfm.date, 103), 112)

SQL Server : convert date to YYYMMDD?

I have a date in one of the column in SQL Server, the sample dates are:
10/02/2012
23/11/2012
13/01/2012
10/02/2012
10/02/2012
I have tried the approach to convert the dates to YYYYMMDD
DECLARE #v DATE= '1/11/2012'
SELECT CONVERT(VARCHAR(10), #v, 112)
I have another column in a same table in which i want to update the date in YYYYMMDD format ,the problem here is that the date are not proper
and throws an error
DECLARE #v DATE= '23/11/2012'
SELECT CONVERT(VARCHAR(10), #v, 112)
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
Any help is appreciated in this, The date will come in any order either dd/mm/yyyy or mm/dd/yyyy, it should be able to convert it properly
You may try using the following format to one data type to another.(dd/mm/yyyy to yyyy/mm/dd)
CONVERT(data_type(length),expression,style)
As well as update like following
UPDATE table_name SET
destination_column_name=orig_column_name
WHERE condition_if_necessary
if you want to insert date in SQL server you want to follow certain formats. You can follow either yyyy-MM-dd or MM-dd-yyyy
for your question if you follow MM-dd-yyyy this format and if you using SQL server 2012 or newer you can use this and you can get the result
DECLARE #v DATE = '11/23/2012';
SELECT FORMAT ( #v, 'yyyy/MM/dd', 'en-US' )
Refer these links FORMAT (Transact-SQL) , SQL Server date format function

SQL - Convert varchar (dd/mm/yyyy hh.mm AM/PM) to datetime

I'm trying to convert a varchar field to datetime. The data in the varchar field looks like this dd/mm/yyyy hh.mm AM/PM.
I'm actually trying to extract the date part & time part and convert them to date and time respectively.
And I use the below query to extract the date part from the varchar and convert it to date.
SELECT DISTINCT LEFT([Start Date], 10)
,CASE WHEN ISDATE(LEFT([Start Date], 10)) = 1
THEN CONVERT(DATETIME, LEFT([Start Date], 10), 120)
END
,ISDATE(LEFT([Start Date], 10))
FROM [dbo].[TestTable]
I have timestamp in the format mentioned above for entire August.
But the query gives me the result in yyyy-dd-mm format and the date are converted to date format only till 12 Aug'15. Date after 12 of Aug is null. I know it's because, in the case statement they are returned 0. How can I convert those values also to date?
Any suggestions to convert all the values to yyyy-mm-dd hh:mm:ss will be very helpful.
Thanks in advance.
You could use SET DATEFORMAT as lad2025 suggested.
SET DATEFORMAT dmy
Or you can use the following in your CONVERT()
DECLARE #date VARCHAR(16) = '13/08/2015 11:13';
SELECT CONVERT(DATETIME, #date, 103)
103 is the code for dd/mm/yyyy.

cast in sql server yyyy/mm/dd instead of dd/mm/yyyy

I have a script to insert 1000 rows of data in a table, in the datetime column I have this information.
CAST(N'2015-05-14 00:00:00.000' AS DateTime)
The problem is cast is trying to cast in dd/mm/yyyy format where the input is in yyyy/mm/dd format.
Don't use cast when converting varchar to datetime, use convert instead, as convert takes in a date format.
convert(datetime, '2015-05-14 00:00:00.000', 121)
See more at:
http://www.sqlusa.com/bestpractices/datetimeconversion/
its already written that way since i have a generated script of data to insert, so i cant change one by one, too many rows to insert
Are you unable to modify the script? This seems like an easy find/replace.
CAST(N' -> convert(datetime, '
' AS datetime) -> ', 121)

How to turn DD/MM/YYYY 00:00:0000 to MM/YYYY and keep it as date format and not string

How to turn DD/MM/YYYY 00:00:0000 to MM/YYYY and keep it as date format and not string in MS SQL
i have tried many options but still did not find the right one.
Simply you cannot keep date as mm/yyyy as a date/datetime. But you could keep it as a string and also convert when needed back to a date as below.
Sql-Server Fiddle Example
declare #dt datetime = getdate(),
#mmyyyy varchar(7)
--To format as mm/yyyy
select #mmyyyy = right(convert(varchar(10), #dt, 103),7)
--To convert back to datetime type from mm/yyyy varchar
select #mmyyyy [mm/yyyy],
convert(datetime,'01/'+ #mmyyyy,103) back_To_Date

Resources