Convert varchar to datetime in sqlserver table - sql-server

In SQL Server I have a column called "dateCreation" defined as varchar(50) and I would like to convert it to datetime type. If I change the column's type to datetime with the MS SQL Server Management Studio I get the following warning:
'atPropostes' table
- Warning: Data might be lost converting column 'dateCreation' from 'varchar(50)'.
Then If I execute this:
SELECT CONVERT(Datetime, dateCreation, 120) as dateCreation from atPropostes -- to convert it to Datetime
I get the following error:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
What would be the best way to
(1) change the column type to datetime and
(2) convert all the registers from varchar(50) to Datetime type?
Thanks!

I assume that you are using SQL Server 2012
Try like this
SELECT TRY_CONVERT(DATETIME, dateCreation, 120) AS dateCreation
FROM atPropostes
Note: Please make sure that your date stored in varchar type have consistent date format. It should be something like yyyy-mm-dd or mm-dd-yyyy or.....

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

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)

The conversion of a datetimeoffset data type to a datetime data type resulted in an out-of-range value

Using SQL Server 2008.I have a table called User which has a column LastLogindata with datetimeoffset datatype
The following query works on production server but not on replication server.
select top 10 CAST(LastLoginDate AS DATETIME) from User.
I am getting the following error.The conversion of a datetimeoffset data type to a datetime data type resulted in an out-of-range value.
Thanks
Check the LastLoginDate columns value like this '0001-01-01' or '0001/01/01'.
If u have means get this error ..
Try this one
select top 10 CAST(CASE when cast(LastLoginDate as varchar) = '0001-01-01 00:00:00'
THEN NULL ELSE GETDATE() end AS DATETIME) from User
If a field in database is of type datetimeoffset type, then it should contain date within range 0001-01-01 through 9999-12-31. I think the issue is the date inside your database.
Please check the official link of SQL server Click Here
I solved it this way. I had an nvarchar(max) column casted as an xml and used the T-SQL expression ISDATE() to exclude the bad rows in the where clause.
where cast(DataObject as xml).value('(/DataObjects/#LastLoginDate)[1]', 'varchar(10)') is not null
and isdate(cast(DataObject as xml).value('(/DataObjects/#LastLoginDate)[1]', 'varchar(10)')) = 1
On SQL Server 2016, I used:
CONVERT(DATETIME2, DateValueColumn)
This worked for values that were giving errors when trying to convert to DATETIME, giving the message "The conversion of a datetimeoffset data type to a datetime data type resulted in an out-of-range value." The offending values had dates of 0001-01-01, as a previous answer has mentioned.
Not sure if this works on SQL Server 2008 though.

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