Change converted format into ISO date - SQL Server - sql

I have data where a date is stored as text, for example:
Jan 01, 2001
I see a number of examples showing how to format a datetime to this format but I'm looking for a function to change it back to a date.
What is even more confusing is what I tried gives me "2020-01-01." Any suggestions?
DECLARE #d varchar(10) = 'Jan 01, 2001'
SELECT TRY_CONVERT(date, #d)
I'm using SQL Server 2014.

Your string is not long enough. Declare it with a long-enough length:
DECLARE #d varchar(255) = 'Jan 01, 2001';
SELECT TRY_CONVERT(date, #d);
If you show the value of #d, you will see what this code returns:
DECLARE #d varchar(10) = 'Jan 01, 2001';
SELECT #d, TRY_CONVERT(date, #d);
which is 'Jan 01, 20'. SQL Server interprets that as 2020.

Related

Convert a string to a date in sql

how do I convert this:
DECLARE #FromDate varchar = 'Feb 2020'
to a Date like this
DD/MM/YYYY or DDMMYYYY
I need to sort some dates and from the combobox you can just choose those three : Dez 2019, Jan 2020 or Feb 2020.
So I thought I could do something like that:
DECLARE #FromDate varchar = 'Feb 2020';
SELECT RIGHT(convert(varchar, CONVERT(date, CONVERT(varchar, PIN_DATE)), 106),8)
FROM PIN
WHERE RIGHT(CONVERT(varchar, CONVERT(date, CONVERT(varchar, PIN_DATE)), 106),8)
= CONVERT(datetime, #FromDate,106)
The date in PIN_DATE looks like this : YYYYMMDD aka. 20201231
This is an ERROR:
DECLARE #FromDate varchar = 'Feb 2020'
This assigns #FromDate as a SINGLE character, so the value is 'F'.
When using the character types in SQL Server, always use a length:
DECLARE #FromDate varchar(255) = 'Feb 2020'

Converting varchar to date in SQL Server

I have a varchar column which has values like "Aug 07 2017, 04:14 AM,EDT".
I need to convert this to a date column so that its possible to take the maximum value of the date.
I tried this:
select CONVERT(datetime, #dateField, 108)
from table
But I am getting the following error:
Conversion failed when converting date and/or time from character string.
You can just use left() and convert():
select convert(date, left('Aug 07 2017, 04:14 AM,EDT', 11))
If you want a datetime then convert the date and time separately, then:
select ( convert(datetime,
left('Aug 07 2017, 04:14 AM,EDT', 11)
) +
convert(datetime,
convert(time,
substring('Aug 07 2017, 04:14 AM,EDT', 14, 8)
)
)
)
Note: This is not taking the time zone into account.
Better Datetime solution
DECLARE #dateField AS NVARCHAR(50) = 'Aug 07 2017, 04:14,EDT'
-- Get index of last comma
DECLARE #validDateField AS NVARCHAR(20) = REPLACE(LEFT(#dateField, LEN(#dateField)- CHARINDEX(',', reverse(#dateField))), ',','')
SELECT CONVERT(DATETIME, #validDateField, 108)
Here is a solution:
select CONVERT(datetime, REPLACE(LEFT(#dateField, LEN(#dateField) - 3),',','') ,108 )

SQL Server 2008 Date parameter

I'm passing a start date and end date parameter to my stored procedure. I'm doing a simple test here:
DECLARE #StartDate DATE = '10/06/2013' --dd/mm/yyyy
SELECT #StartDate -- this statement running successfully
DECLARE #EndDate DATE = '30/06/2013' --dd/mm/yyyy
SELECT #EndDate -- this statement giving error
This statement returns the following error
Msg 241, Level 16, State 1, Line 2
Conversion failed when converting date and/or time from character string.
Does anybody have idea what's going wrong with EndDate?
I'm pretty sure the error is on this line:
DECLARE #EndDate DATE = '30/06/2013' --dd/mm/yyyy
Not on the SELECT. It wouldn't make sense that it would be on the SELECT, because processing a variable should be fine.
I would recommend that you use YYYYMMDD formats. The following is my preference:
DECLARE #EndDate DATE = '2013-30-06' ;
However, it can fail for certain internationalization settings. The following is documented to always work:
DECLARE #EndDate DATE = '20133006' ;
'10/06/2013' means 06-Oct-2013 not 10-Jun-2013. There is nothing exists with month 30 as in your #EndDate '30/06/2013'.
DECLARE #StartDate DATE='10/06/2013'
DECLARE #DummyDate DATE = '2013-Oct-06'
IF #StartDate = #DummyDate
BEGIN
SELECT 1
END
By default sql server takes date value in 'yyyy-mm-dd' format,
So you need to follow that format or you need to convert date format accordingly.
T-SQL convert string to datetime - SQL Server convert string to date
SELECT convert(datetime, '10/23/2016', 101) -- mm/dd/yyyy
SELECT convert(datetime, '2016.10.23', 102) -- yyyy.mm.dd
SELECT convert(datetime, '23/10/2016', 103) -- dd/mm/yyyy
SELECT convert(datetime, '23.10.2016', 104) -- dd.mm.yyyy
SELECT convert(datetime, '23-10-2016', 105) -- dd-mm-yyyy
-- mon types are nondeterministic conversions, dependent on language setting
SELECT convert(datetime, '23 OCT 2016', 106) -- dd mon yyyy
SELECT convert(datetime, 'Oct 23, 2016', 107) -- mon dd, yyyy
-- SQL string to datetime conversion without century - some exceptions
SELECT convert(datetime, '10/23/16', 1)   -- mm/dd/yy
SELECT convert(datetime, '16.10.23', 2)   -- yy.mm.dd
SELECT convert(datetime, '23/10/16', 3)   -- dd/mm/yy
SELECT convert(datetime, '23.10.16', 4)   -- dd.mm.yy
SELECT convert(datetime, '23-10-16', 5)   -- dd-mm-yy
SELECT convert(datetime, '23 OCT 16', 6)  -- dd mon yy
SELECT convert(datetime, 'Oct 23, 16', 7) -- mon dd, yy

Convert 24-Hour format datetime to varchar Sql Server 2008

I have a problem in converting sql server 2008 datetime to varchar,
Select convert(varchar(20),convert(datetime, '2013-12-11 00:59:00.000'))
the result is Dec 11 2013 12:59AM but I need it to be actually Dec 11 2013 00:59AM as in the database dates are of 24-Hour format.
How can I correct the query?
There are two functions, cast and convert that you can use.
http://msdn.microsoft.com/en-us/library/ms187928.aspx
You forgot to put in the format style.
-- Using cast
Select cast('2013-12-11 00:59:00.000' as varchar(20)) as my_casted_date
-- Using convert
Select convert(varchar(24), '2013-12-11 00:59:00.000', 113) as my_converted_date
Use format with custom date time strings for utlitmate flexibility.
http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
-- Using format
DECLARE #my_date datetime2 = '2013-12-11 00:59:00.000';
SELECT format(#my_date, 'MMM dd yyyy HH:MM tt', 'en-us') as str_english_date
Solution that will work with SQL Server 2008.
-- Create a date time variable
DECLARE #my_date DATETIME2 = '2013-12-11 00:59:00.000';
-- Using convert
SELECT
CONVERT(varchar(24), #my_date, 113) +
CASE
WHEN DATEPART(HH, #my_date) < 12 THEN ' AM'
ELSE ' PM'
END
AS my_converted_date;
This will be perfect for you..
declare #dt datetime
set #dt='12-Jan-2014 23:59'
SELECT Right(CONVERT(VARCHAR, #dt, 100),7) AS DateTime_In_12h_Format
It's not nice but it does its job:
DECLARE #Var DATETIME;
SET #Var = '2013-12-11T00:59:00.000';
SELECT
#Var AS SourceValue,
CONVERT(VARCHAR(50), #Var, 100) AS FormatedValue1,
CASE
WHEN CONVERT(VARCHAR(50), #Var, 100) LIKE '%[ ]12:__AM' THEN REPLACE(CONVERT(VARCHAR(50), #Var, 100), ' 12:', ' 00:')
ELSE CONVERT(VARCHAR(50), #Var, 100)
END AS FormatedValue2
SourceValue FormatedValue1 FormatedValue2
----------------------- ------------------- -------------------
2013-12-11 00:59:00.000 Dec 11 2013 12:59AM Dec 11 2013 00:59AM
Please try this
Select convert(varchar(20),convert(datetime, '2013-12-11 00:59:00.000'),113) + ' ' +RIGHT(CONVERT(VARCHAR(26), Convert(datetime,'2013-12-11 00:59:00.000'), 109), 2)
Output
11 Dec 2013 00:59:00 AM
Fiddle Demo

Date format date-month-year

I have a table which has a datetime column. I want to show date in date-month-year forma.I am using SQL Server 2008.
You need to convert datetime column
select convert(varchar,datecolumn,103) from yourtable
Some datetime convertions:
SELECT convert(datetime, '10/23/2016', 101) -- mm/dd/yyyy
SELECT convert(datetime, '2016.10.23', 102) -- yyyy.mm.dd
SELECT convert(datetime, '23/10/2016', 103) -- dd/mm/yyyy
SELECT convert(datetime, '23.10.2016', 104) -- dd.mm.yyyy
SELECT convert(datetime, '23-10-2016', 105) -- dd-mm-yyyy
SELECT CONVERT(VARCHAR(10), GETDATE(), 111) AS [YYYY/MM/DD]
/* YYYY/MM/DD
2015/07/11 */
SELECT CONVERT(VARCHAR(10), GETDATE(), 112) AS [YYYYMMDD]
/* YYYYMMDD
20150711 */
-- SQL convert date string to datetime - time set to 00:00:00.000 or 12:00AM
PRINT CONVERT(datetime,'07-10-2012',110) -- Jul 10 2012 12:00AM
PRINT CONVERT(datetime,'2012/07/10',111) -- Jul 10 2012 12:00AM
PRINT CONVERT(datetime,'20120710', 112) -- Jul 10 2012
You can learn all DateTime convertion from here
FORMAT can be used for this;
SELECT FORMAT(GETDATE(), 'dd-MM-yyyy') --20-09-2021
in your case;
SELECT FORMAT(datetime, 'dd-MM-yyyy') AS datetime --20-09-2021
Edit;
Sorry to inform that I've just seen you mentioned SQL Server 2008 this code works for SQL Server 2012 for those who use 2012 and ends up in here!