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.
Related
For example:
The value 2208111603(nvarchar) needs to be converted to 2022-08-11 00:00:00(datetime).
The things I have tried and the corresponding errors I've got so far as follow:
CAST(DATE_TIME AS numeric) works but CAST(CAST(DATE_TIME AS numeric) AS datetime) returns Arithmetic Overflow Error.
convert(datetime2, CAST(DATE_TIME AS numeric), 105) returns
Explicit conversion from data type numeric to datetime2 is not allowed
CONVERT(datetime2, DATE_TIME , 105) returns
Conversion failed when converting date and/or time from character
string.
CONVERT(datetime2, CONVERT(CHAR(10), DATE_TIME), 105) returns
Conversion failed when converting date and/or time from character
string.
Finally, I tried PARSE(DATE_TIME AS date USING 'en-US') which resulted in
Error converting string value '2208111603' into data type date using
culture 'en-US'.
The SQL Server's CONVERT function also has some built-in formats as a third parameter for converting varchar to datetime. So, I tried SELECT convert(datetime,'2208111603',112) which resulted in the same error (Msg 241):
Conversion failed when converting date and/or time from character
string.
Any help would be much appreciated!
With just a bit of string manipulation...
Note: this will assume century of 20
Example
Declare #S varchar(50) ='2208111603'
Select try_convert(datetime,stuff(stuff(#S,7,0,' '),10,0,':'))
Results
2022-08-11 16:03:00.000
A little bit ugly, but to overcome the century indicator you an nest a little logic to conditionally add 19.
Declare #S varchar(50) ='6408111603'
Select try_convert(datetime,case when left(#S,2)>'50' then '19' else '' end + stuff(stuff(#S,7,0,' '),10,0,':'))
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
I am running a query in SSRS which defaults the date picker date format to DD/MM/YYYY. However the dates in the actual database are in YYYY-MM-DD format. I have tried to convert these dates as below but I am getting the message "Conversion of a varchar data type to a datetime data type resulted in an out-of-range value".
Col1 BETWEEN convert(varchar, '22/10/2013 00:00:00', 120) AND convert(varchar, '22/10/2013 23:59:59', 120)
I'm having some trouble working out why that error is triggering. Ie. 22/10/2013 is a valid date format so I was thinking it should convert into 2013-10-2013 easily enough?
But I guess I'm missing something!
DateTime values do not have a format - you apply a format when you display them. You should just be able to set the parameter to a DateTime type and just pass it into the query without any string conversion.
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.
How can i convert a datetime of type dd-mm-yyyy to dd/mm/yyyy in sql?
I dont want to convert to varchar but to datetime.
Can anyone give me an idea please?
A DateTime is a DateTime is a DateTime - it doesn't have " a format" - it's just a DateTime.
The format or display options only come into play when you want to display the DateTime as a string.
of type dd-mm-yyyy to dd/mm/yyyy
To me, this sounds as if:
you have string representing a date, in the format dd-mm-yyyy
you want to convert that to a DateTime and then again display a string representing that DateTime in the dd/mm/yyyy format
Right??
Check out the MSDN SQL Server Books Online for CAST and CONVERT - it lists all possible, valid, supported string formats representing a date and time.
Format dd-mm-yyyy is style no. 105, so this will convert your first string:
SET #YourDateTime = CONVERT(DATETIME, '23-11-2010', 105)
Now you have your DATETIME value (Nov 23, 2010) in #YourDateTime - just a DateTime, no formatting applied.
If you want to display that date value in format dd/mm/yyyy, which is style no. 103, use this snippet of T-SQL to achieve this:
SELECT CONVERT(VARCHAR(50), #YourDateTime, 103)
This will display 23/11/2010.