Convert value stored in a varchar to date time in sql - sql-server

I have a date stored in varchar variable
I want to covert it to datetime format so that I could update the date to a column with type datetime
declare #dt_original as varchar(30)
set #dt_original original='1997-12-22 00:00:00.000'
I have tried
cast(#dt_original as date time)
Convert(datetime,#dt_original)
But nothing worked
Always throwing "conversion failed when converting date and/or time from character string"

The idea of using convert was correct, but you're missing the style pattern matching your string. In this case, it's 121:
CONVERT(DATETIME, #dt_original, 121)

Related

MS SQL error : conversion of a varchar data type to a datetime data type resulted error

I am using MS SQL Server. One table column is defined as order_date varchar(25) and is stored in a format like 05/11/2015 07:54:16
In my select query, I am trying to convert that into a date format like (yyyy-mm-dd HH:MM:SS:000, e.g. 2015-05-11 08:03:10.000
I have tried with
select CONVERT(varchar(50), CAST(order_date AS datetime),121) from <table>
In my table I have around 500 records, but after fetching 10 records in my expected format, I get this error error:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
Is there any issue with my conversion?
There are a couple of problems here. The first is your data type choice, but I'll just repeat my comment for that: ""am trying to convert that into date format "* This is totally the wrong approach. Stop storing dates as a varchar use a date and time data type. The reason you have this error is because your poor data type choices. Fix that and get the presentation layer to work about the formatting."
Now, moving on. You have your expression below:
CONVERT(varchar(50), CAST(order_date AS datetime),121)
Firstly, as your value is a varchar, you need to tell SQL Server the format it is in; dd/MM/yyyy hh:mm:ss (I guess as '05/11' is ambigous)) is not unambiguous. What you have is the UK style, which is style 103:
CONVERT(datetime,'05/11/2015 07:54:16',103)
Now you can convert that to your ISO format:
CONVERT(varchar(23),CONVERT(datetime,'05/11/2015 07:54:16',103),121)
This returns the varchar value '2015-11-05 07:54:16.000'
I'd try to see what the value of order_date is at the time of the error. Perhaps what you assume to be a datetime string in mm/dd/yyyy... format is actually in dd/mm/yyyy....
I.e., the following outputs 2015-05-11 07:54:16.000 as my system is set to datetime format of mm/dd/yyyy.
declare #order_date varchar(100) = '05/11/2015 07:54:16'; -- mm/dd/yyyy...
print CONVERT(varchar(50), CAST(#order_date AS datetime),121);
the following throws an error: The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
set #order_date = '13/09/2015 07:54:16'; -- dd/mm/yyyy...
print CONVERT(varchar(50), CAST(#order_date AS datetime),121);

How can I convert a JSON date with timezone to a SQL Server datetime?

I saved the output of a JSON object in a CSV file. I want to import the data into SQL Server. I have tried to cast the JSON date column to a SQL Server datetime data type.
select cast('2009-06-18T16:44:20+0000' as datetime)
This causes an error to be raised: Conversion failed when converting date and/or time from character string.
How can I convert a JSON date with timezone to a SQL Server datetime?
As far as I know, SQL Server recognizes timezone with colon. You have to reformat timezone part as follows:
SELECT CONVERT(datetime2, STUFF('2009-06-18T16:44:20+0000', 23, 0, ':'))
According to MSDN ISO 8601 has YYYY-MM-DDThh:mm:ss[.nnnnnnn][{+|-}hh:mm] format.
I had a similar issue; I was getting the same error though the date format for my dates are a little different. The solution for me was to convert it to a DATETIME2 value, instead of just DATETIME. Had I not seen Pawel's answer, I would not have tried converting to DATETIME2.
The dates that I was getting were from a C# object that was serialized to JSON using JSON.Net. Below is a sample SQL script showing what the datetime values were and how to convert it.
DECLARE #effectiveDateJSON VARCHAR(MAX) =
'{"EffectiveDate":"2017-02-07T00:00:00-06:00", "CreateDate":"2017-02-07T16:32:12.9130892-06:00"}';
SELECT *
FROM OPENJSON(#effectiveDateJSON)
WITH (EffectiveDate DATETIME2 '$.EffectiveDate',
CreateDate DATETIME2 '$.CreateDate'
);
For a timezone aware conversion
declare #offset datetimeoffset = JSON_VALUE('{"d": "2021-01-01T00:00:00+03:00"}', '$.d')
declare #inCurrentTimezone datetime = #offset AT TIME ZONE CURRENT_TIMEZONE_ID()
select #inCurrentTimezone
Docs:AT TIME ZONE, datetimeoffset

SQl query datetime from nvarchar error

In SQL,
declare #date datetime ='Jul15'
select CONVERT(varchar(10),#date,105)
getting error
Conversion failed when converting date and/or time from character
string.
Please help
It is because Jul15 is not a valid SQL Server Date or DateTime value, hence the error when it is trying to convert it to date/datetime to assign it to a datetime variable.
Ideally you should be using ANSI-Standard when working with date or datetime values in SQL Server, ANSI-Standard Date value has the format of YYYYMMDD, therefore your statement should look something like this.....
declare #date datetime ='20150701'
select CONVERT(varchar(10),#date,105)

Conversion of VARCHAR to DATETIME

I have a VARCHAR that holds datetime data in the following format:
15/04/2014 16:05
I need to convert this to datetime (with the exact format as before) but get an out of range error on the conversion:
CONVERT(DATETIME, #endDate, 108)
The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.
Could any advise the best way to successfully convert? Bear in mind i cannot change the source format and eventually need to compare the date to see if it is past the present day.
I think you want to use 103 instead of 108 for the final parameter.
Please use date constant values that are language/country independent
-- Independent constant (natural date)
DECLARE #END_DTE VARCHAR(20) = '20140415 16:05'
-- Show the result
SELECT CONVERT(DATETIME, #END_DTE, 108) AS FORMAT_DTE

could not convert varchar to datetime in SQL server

In my database i got problem when converting varchar to datetime due to different format of datetime as per below.
Conversion failed when converting date and/or time from character
string.
I have 2 select clause as per below
Select Getutcdate()
Select top 1 localmachinetime from perfmon
both return following format respectively
2012-05-28 06:54:45.753>
28-05-2012 03:03:07
and when i try to convert it to datetime using CAST then it gives me error in second case as per below.
The conversion of a varchar data type to a datetime data type resulted
in an out-of-range value.
You can use convert with a Date/Time style.
convert(datetime, '28-05-2012 03:03:07', 105)

Resources