Hi @Tim Mullady ,
would you please try
FORMAT(CONVERT(datetime, ColumnName, 101), 'MM/dd/yyyy')
Thanks!
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello,
I have table with a date column in varchar data type. Some of the data has leading zero's and other do not. I've been struggling to adjust the data with CAST or Convert.
example:
07/13/2020
7/13/2020
I wanted to see if someone could help me adjust it to date type M/DD/YYYY
Thanks,
Tim
Hi @Tim Mullady ,
would you please try
FORMAT(CONVERT(datetime, ColumnName, 101), 'MM/dd/yyyy')
Thanks!
DECLARE @t TABLE (
[Date] varchar(20)
);
INSERT INTO @t VALUES
('07/13/2020'), ('7/13/2020'), ('7/01/2020');
-- Output format 7/1/2020
SELECT REPLACE(CASE WHEN LEFT([Date], 1) = '0' THEN SUBSTRING([Date], 2, LEN([Date]) - 1) ELSE [Date] END, '/0', '/')
FROM @t;
--Output format 07/01/2020
SELECT CONVERT(varchar(10), CAST([Date] AS date), 101)
FROM @t;
If you are using SQL 2016+
DECLARE @t TABLE (
[Date] varchar(20)
);
INSERT INTO @t VALUES
('07/13/2020'), ('7/13/2020'), ('7/01/2020');
SELECT FORMAT(TRY_CAST([Date] as date),'M/d/yyyy')
FROM @t