Convert dd/mm/yy string to yyyy-mm-dd - sql

I am having issues converting a string in dd/mm/yy hh:mi:ss format to yyyy-mm-dd hh:mi:ss format.
I have tried using CONVERT() as follows:
select CONVERT(VARCHAR, '11/10/11 10:56:58', 120)
But that returns no change:
11/10/11 10:56:58

You need to convert to datetime first to change a string to reflect a certain regional format. Be sure you are interpreting the datetime value correctly, too; on some systems, that will be October 11th, on others it will be November 10th.
SELECT CONVERT(CHAR(19), CONVERT(DATETIME, '11/10/11 10:56:58', 3), 120);
Finally, use the correct number of characters in your char or varchar declarations. Not specifying a length is lazy and can lead to problems. See:
Bad habits to kick : declaring VARCHAR without (length)

This will work:
SELECT CONVERT(VARCHAR(19),CONVERT(DATETIME,'11/10/11 10:56:58',3),120)

The issue: you are converting a VARCHAR to a VARCHAR.
Your query is fine if you use a DATETIME.
SELECT CONVERT(VARCHAR(19), CAST('11/10/11 10:56:58' AS DATETIME), 120);
See fiddle.

Related

How to convert varchar to datetime format in SQL Server

I am trying to convert varchar date value to datetime using this query
SELECT
CONVERT(varchar, CONVERT(datetime, [Assigned_DT]), 105) + ' ' +
CONVERT(varchar(8), CONVERT(datetime, [Assigned_DT]), 14)
But for this date '16/02/2021 1:55 PM' I get this error:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
How can I convert so it is valid - the format of the dates are mm/dd/yyyy hh:mm:ss
The 105 number in the code is the wrong style number. It's for Italian dd-mm-yyyy values (note the - vs /). You need to read the CONVERT() function docs and find the style number that matches your input. I could do that for you, but now that I've pointed you where to look, doing it yourself is good practice.

Formatting Datetime2 in SQL Server

I am trying to format a datetime2 into yyyy-mm-dd hh:mm:ss format.
I have tried the below options:
This is throwing an error:
SELECT CONVERT(datetime2, 120);
This works fine, but I need the output of type datetime2
SELECT CONVERT(VARCHAR(19), GETDATE(), 120);
How can I achieve this?
You are calling the CONVERT() function without specifying the data type to convert to, so in your case SELECT CONVERT(datetime2, 120); SQL Server will try to convert the value 120 to a datetime2 and that's why you get this error (which you don't provide)
Explicit conversion from data type int to datetime2 is not allowed.
To use CONVERT() with date and time styles, you need to pass
CONVERT(<datatype to convert to>, <Value to be converted>, <Style>).
The SYSUTCDATETIME returns a datetime2 you can convert to as
SELECT CONVERT(VARCHAR(20), SYSUTCDATETIME(), 120)
--Change SYSUTCDATETIME() with your column/variable
For what you say
but I need the output of type datetime2
A DATETIME doesn't have a format, it's a binary value, thus you need to convert it to a formatted string. Since you need to return a DATETIME2 then you need to leave it as it is, and do the formatting in presentation layer.

How to convert date time format in SQL Server like '2017-03-04 10:07:03.490' to date format which is seperated by - like '2-11-2016'

I am tying to make date comparison with the query
select *
from dbo.OH_Case
where dbo.OH_Case.CreatedDate between Convert(varchar(30),'24/04/2017', 102)
and Convert(varchar(30),'01/05/2017', 102)
but it throws this error:
Msg 242, Level 16, State 3, Line 1
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
My application passes data in '01/05/2017' format and the date information in SQL Server is stored in '2017-03-04 10:07:03.490' format. What should I do to make both of these in same format which allows comparison?
First, you're converting a string literal to a varchar. Convert it to a datetime instead.
Second, you're passing in 24/04/2017, but date format 102 is yyyy.mm.dd. Try date format 103 for dd/mm/yyyy:
convert(datetime, '24/04/2017', 103)
Here dbo.OH_Case.CreatedDate is of data type Datetime and you have to convert your values in Datetime format for filter.
convert columns in Datetime format and format will be same for all. In your case, convert values in Datetime instead of Varchar and it will work fine.
select * from
dbo.OH_Case
where dbo.OH_Case.CreatedDate between cast('24/04/2017' as datetime) and
cast('01/05/2017' as datetime)
Updates:
Siddharth, I have checked you date values and figured out that issue is in date "24/04/2017". This format is for "MM/dd/yyyy". and 24 is passed as month which is not valid. Therefore it is throwing exception. I have updated result
to handle such exceptions.
select * from
dbo.OH_Case
where dbo.OH_Case.CreatedDate between convert(datetime, '24/04/2017', 103) and
convert(datetime, '01/05/2017', 103)
Here dbo.OH_Case.CreatedDate is of data type Datetime and you have to convert your values in Datetime format for filter. convert columns in Datetime format and format will be same for all. In your case, convert values in Datetime instead of Varchar and it will work fine.
select * from dbo.OH_Case format(where dbo.OH_Case.CreatedDate, 'dd/mm/yyyy') as CreatedDate

SQL date formats

How can I convert this 201402110544 to date(2014-02-11) in SQL server 2008?
You can cast as DATE data type (https://msdn.microsoft.com/en-us/library/bb630352(v=sql.100).aspx)
SELECT CAST(datetime_value AS DATE)
SELECT CAST(GETDATE() AS DATE) --> 2015-08-18
If you have a string to cast as DATE you can use:
SELECT CAST(LEFT('201402110544', 8) AS DATE)
You trim out the time part, by taking 1st 8 chars (YYYYMMDD) and will result a valid string to cast as DATE.
If this format is always the same, you can do this
DECLARE #d VARCHAR(20)='201402110544'
SELECT CAST(SUBSTRING(#d,0,9) as DATETIME)
Also have a look at The ultimate guide to the datetime datatypes which explains in detail about handling date-times
String to date conversion sure is a weakness of SQL Server. CONVERT does that, but can only deal with a number of given formats. So you must convert your string into such a format first and then convert it to date afterwards.
One such format is 120 = 'yyyy-mm-dd hh:mi:ss' which I think comes closest to yours. Another 102 = 'yyyy.mm.dd' suffices to get the date.
convert(
date,
concat(substring(datestring, 1,4), '.',
substring(datestring, 5,2), '.',
substring(datestring, 7,2)),
102)
from data;
SQL fiddle: http://www.sqlfiddle.com/#!3/9eecb7/3689
EDIT: I stand corrected. Horia is right; CAST supports 'yyyymmdd', so you can use it directly by merely cutting of the string's time part. This is more readable than my suggestion above, which makes CAST the better option in your case.

Why does SQL Server convert VARCHAR to DATETIME using an invalid style?

I can't make out from the documentation why SQL Server parses a text in a format other than the specified style.
Regardless of whether I provide text in the expected format:
SELECT CONVERT(DATETIME, N'20150601', 112)
or incorrect format (for style 113):
SELECT CONVERT(DATETIME, N'20150601', 113)
The results are the same: 2015-06-01 00:00:00.000 I would expect the latter to fail to convert the date (correctly).
What rules does it employ when trying to convert a VARCHAR to DATETIME? I.e. why does the latter (incorrect format style) still correctly parse the date?
EDIT: It seems I've not been clear enough. Style 113 should expect dd mon yyyy hh:mi:ss:mmm(24h) but it happily converts values in the format yyyymmdd for some reason.
Because the date is in a canonical format ie(20150101). The database engine falls over it implicitly. This is a compatibility feature.
If you swapped these around to UK or US date formats, you would receive conversion errors, because they cannot be implicitly converted.
EDIT: You could actually tell it to convert it to a pig, and it would still implicitly convert it to date time:
select convert(datetime,'20150425',99999999)
select convert(datetime,'20150425',100)
select convert(datetime,'20150425',113)
select convert(datetime,'20150425',010)
select convert(datetime,'20150425',8008135)
select convert(datetime,'20150425',000)
And proof of concept that this is a compatibility feature:
select convert(datetime2,'20150425',99999999)
Although you can still implicitly convert datetime2 objects, but the style must be in the scope of the conversion chart.
Reason why is the date N'20150601' converted to valid datetime is because of fact that literal N'20150601' is universal notation of datetime in SQL Server. That means, if you state datetime value in format N'yyyymmdd', SQL Server know that it is universal datetime format and know how to read it, in which order.
You should convert to varchar type in order to apply those formats:
SELECT CONVERT(varchar(100), CAST('20150601' as date), 113)
OK, you are converting datetime to datetime. What did you expect? In order to apply formats you should convert to varchar and you have to have date or time type as second parameter.