Conversion of VARCHAR to DATETIME - sql-server

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

Related

Convert value stored in a varchar to date time in sql

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)

T SQL Conversion failed when converting date and/or time from character string from VARCHAR(MAX)

I'm using SQL Server 2014. I have a date stored as varchar(MAX) in the format of:
2019-02-18
However, I want it in the British format dd/mm/yyyy (103).
This is my SQL:
SELECT CONVERT(DATE, DateField, 103) AS "JobStartDate"
FROM tblTest
However, I keep getting this error:
Conversion failed when converting date and/or time from character string.
What am I missing?
Update: The date is initially stored as varchar max as it is coming from a 3rd party system. I have no control over this and I completly understand this is the wrong format, but this is what I have been given.
I have a date stored as varchar(MAX)
There's your problem right there.
Not only you are using the wrong data type to store dates, you are also using max which is a known performance killer.
The solution to the problem is to alter the table and store dates in a Date data type - but first, you must look up all the objects that depends on that column and make sure they will not break or change them as well.
Assuming this can't be done, or as a temporary workaround, you must first convert the data you have to Date, and then convert it back to a string representation of that date using the 103 style to get dd/mm/yyyy.
Since yyyy-mm-dd string format is not culture dependent with the date data type, you can simply do this:
SELECT CONVERT(char(10), TRY_CAST(DateField As Date), 103) As [JobStartDate]
FROM tblTest
Note I've used try_cast and not cast since the database can't stop you from storing values that can't be converted to dates in that column.
You want to format the DateField column and not convert it to date.
So first convert it to DATE and then apply the format:
SELECT FORMAT(CONVERT(DATE, DateField, 21), 'dd/MM/yyyy') AS JobStartDate
See the demo.

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);

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)

Convert varchar column to smalldatetime

I have a table with 800+ records. In this table I have a column named 'Data' of varchar(10) datatype which contains dates in dd.MM.yyyy format.I want to convert it to smalldatetime.
I've tried converting it using Enterprise Management Studio Express, but I receive this error:
The conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value.
How can I convert it?
I think you'll need to do a little string manipulation to get this to work as I think SQL is expecting 'MM.dd.yyyy'. So, update your table to flip-flop the month and day first, then the conversion should go through.
update YourTable
set Data = SUBSTRING(Data,4,3) + LEFT(Data,3) + RIGHT(Data,4)
You can use:
SELECT ID, CAST(VarcharCol As SmallDateTime) as DateTimeCol From Test1
This will return a table with varcharcol values as smalldatetime
Then update the content of varcharcol with the new values.
If you don't want implicit conversion to smalldatetime, you should use CONVERT and the argument style.
dd.MM.yyyy format correspond to style 104.
For example :
SELECT CONVERT(smalldatetime, '31.12.2018', 104) AS "Result"
Result
------
2018-12-31 00:00:00

Resources