i'm trying to convert following varchar date to datetime format but it doesn't work
select Convert(datetime, '2010-04-14',103)+10 AS DocDueDate
from tblActionHeader AH
i'm getting the 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.
You are using a date style for the format dd/mm/yyyy, use the style 120 instead:
select Convert(datetime, '2010-04-14',120) + 10 AS DocDueDate
from tblActionHeader AH
Have you tried a cast, as "select cast('2010-04-14' as datetime)"?
for example
create table test2 (updatetime varchar(20));
insert into test2 values ('2010-9-12 10:33:5');
select updatetime from test2 where convert(datetime,updatetime,110)>'2010-9-12
If you're using style 103, you should be using British French Date formatting.
select DATEADD(Day, 10,Convert(datetime, '14/04/2010',103)) AS DocDueDate
See the docs for details.
Related
I have a data source with 'datetime' that is plain text and am having issues converting it.
When I attempt to convert it to various date formats and INSERT into a new table, I am getting errors.
data example:
"18-07-2015 11:50:30am"
source table structure:
CREATE TABLE [dbo].[Conv_COMMUNICATIONEXPORT]([datetime] [varchar](255) NULL)
destination table structure:
CREATE TABLE [dbo].[TB_X_Attachment]([CreatedDate] [datetime] NULL)
error inserting data:
Msg 242, Level 16, State 3, Line 4
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
When I run the following select, the last convert returns an error:
SELECT CONVERT(VARCHAR(30), GETDATE(), 120) ,getdate() ,CONVERT(DATE, CONVERT(VARCHAR(40), '01-01-2000'), 120)
When I run the following into the table, there is success:
INSERT INTO TB_X_Attachment (CreatedDate)
CONVERT(DATE, CONVERT(VARCHAR(30), '2000-01-01'), 120) AS WORKS
None of the following conversion attempts works when trying to insert:
SELECT ce.[datetime] AS VARCHAR_DATETIME,
CONVERT(VARCHAR(10), CE.[datetime], 120),
CONVERT(char(10), CONVERT(VARCHAR(10), CE.[datetime], 120),120),
CONVERT(VARCHAR(10),CONVERT(char(10), CONVERT(VARCHAR(10), CE.[datetime], 120),120),120),
CONVERT(VARCHAR,(CONVERT(VARCHAR(10), CE.[datetime], 120))) ,
CONVERT(VARCHAR(10), CE.[datetime], 120)
FROM Conv_ATTACHMENTEXPORT ae
INNER JOIN Conv_COMMUNICATIONEXPORT ce
ON ae.[attachment record id] = ce.[communication id]
See image below of the results of the above convert trys
I think if I can reverse the output to YYYY-MM-DD it may work. All the convert attempts dont want to set the format to this. How can I reverse the output to YYYY-MM-DD?
SQL Server is pretty smart when it comes to converting strings to dateTime values.
However, as smart as it is, string representations of date and time values are pretty tricky - that's why we have the ISO 8601 standard which is guaranteed to always be interpreted correctly by SQL Server when converting to datetime.
However, I understand that you don't have any control over the source data, and therefor must handle the format specified.
A quick look at the Date and Time Styles table in the documentation of the Convert function will give you the following formats:
Without With Standard Input/Output (3)
century (yy) (1) century (yyyy)
3 103 British/French 3 = dd/mm/yy
103 = dd/mm/yyyy
4 104 German 4 = dd.mm.yy
104 = dd.mm.yyyy
5 105 Italian 5 = dd-mm-yy
105 = dd-mm-yyyy
Any one of the three options with century would give you a correct value of DateTime for the specified string:
DECLARE #DateString varchar(30) = '18-07-2015 1:5:3pm';
SELECT #DateString As string,
CONVERT(DateTime2, #DateString, 103) As [103],
CONVERT(DateTime2, #DateString, 104) As [104],
CONVERT(DateTime2, #DateString, 104) As [105]
Result:
string 103 104 105
18-07-2015 1:5:3pm 2015-07-18 13:05:03 2015-07-18 13:05:03 2015-07-18 13:05:03
Since the original string use hyphens as a separator, I would go with the Italian standard (105) because it's the closest to the source string format.
Try this:
select convert(datetime, [datetime], 105)
where [datetime] is your varchar column which has the date to be converted
or simply
select convert(datetime, '18-07-2015 11:50:30pm', 105)
I have a RegDate column of nvarchar(max) type in my table in which dates are stored in mm/dd/yyyy (5/22/2015 11:09:39 PM) and dd-mm-yyyy (19-05-2015 22:55:05) format. I want to get all these entries in one format i.e. dd/mm/yyyy. I tried to convert it by using
Convert(varchar(10),cast(vr.RegDate as DATETIME),105) as RegistrationDate
but it gives following error:
The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.
Please help me regarding this problem.
You need to determine the format of the value you are converting before you can convert it. If it's simply between those two formats, you can simply search for - to determine it's format.
I would also suggest storing the value in a datetime column as opposed to a varchar, and if you can't do that for whatever reason, you should definitely store it in an ISO format: YYYY-MM-DD HH:MM:SS.
Here's a sample that uses a case statement to provide optional formatting of your two date formats, using the presence of the - character:
CREATE TABLE #temp ( RegDate VARCHAR(50) )
INSERT INTO #temp
( RegDate )
VALUES ( '5/22/2015 11:09:39 PM' ),
( '19-05-2015 22:55:05' )
SELECT CASE WHEN CHARINDEX('-', RegDate) != 0
THEN CONVERT(DATETIME, RegDate, 105)
ELSE CONVERT(DATETIME, RegDate, 101)
END AS FormattedToDate
FROM #temp
DROP TABLE #temp
Produces:
FormattedToDate
2015-05-22 23:09:39.000
2015-05-19 22:55:05.000
I have varchar column with values in the format d.m.yyyy so the date could be 2.2.2014 or 11.11.2014
I need to update a smalldatetime column from this varchar column.
All I tried was ended with error message:
The conversion of a varchar data type to a smalldatetime data type resulted in an out-of-range value.
How to do that?
This is working solution ...
UPDATE tTable
SET dDate = CONVERT(SMALLDATETIME, CONVERT(SMALLDATETIME, vDate, 104), 20)
I am trying to convert a float in the format yyyymmdd to datetime. According to this the correct style code for that format is 112.
Code:
select
convert(datetime,cast(myDate as numeric),112)
from MyTable
Error:
Msg 8115, Level 16, State 2, Line 1
Arithmetic overflow error converting expression to data type datetime.
I get the same error without the cast as numeric part. I've been looking around for a couple hours, but I haven't been able to find anything that fixes this. If you know a better way to convert the float to a datetime I would be open to that idea.
Thank you for your help.
EDIT
Here is the working code:
SELECT
case when isdate(CAST(CAST(myDate AS INT) AS VARCHAR(8))) = 1
then CAST(CAST(CAST(myDate AS INT) AS VARCHAR(8)) AS DATETIME)
end
from MyTable
I wrapped it in the isdate because there were a few invalid dates in there. Thanks to Matt for the help.
EDIT2
Better version:
SELECT
TRY_CAST(CAST(CAST(myDate AS INT) AS VARCHAR(8)) AS DATETIME)
FROM MyTable
First you must convert the FLOAT to a VARCHAR. And since FLOAT has a number of decimal points, it must first be converted to an INT.
DECLARE #myDate FLOAT
SET #myDate = 20140721
SELECT CAST(CAST(#myDate AS INT) AS VARCHAR(8))
--20140721
Then you can convert the VARCHAR to DATE or DATETIME format.
DECLARE #myDate FLOAT
SET #myDate = 20140721
SELECT CAST(CAST(CAST(#myDate AS INT) AS VARCHAR(8)) AS DATE)
--2014-07-21
I want to convert a varchar value that iI set up to a date time value. I want to load rows of a database into another database, but conversion is not going well.
This is my query:
select Krant
, cast(jaar as varchar(4))+'-'
+RIGHT('0'+cast(maand as varchar(2)),2)+'-'
+RIGHT('0'+cast(dag as varchar(2)),2) as datum
, Inhoud as artikel
, LEN(Inhoud)-LEN(Replace(Inhoud,' ','')) as numwords
, 'Goirle' as vestiging
from [Sitecore_Bibliotheekmb_Krantenknipsel].[dbo].[KRANGOI]
The cast to Datum has to be a datetime value, but i am not getting it to work properly. When i tried to cast to datetime it gave me an out of range exception.
These are the results of this query:
alt text http://94.100.115.48/837450001-837500000/837478801-837478900/837478868_5_dE_7.jpeg
I want the "Datum" field to be a Datetime field with the same values but in Datetime format. Could anyone help me please :).
Thanks,
Younes
Use this:
select Krant, cast(cast(jaar as varchar(4))+'-'
+RIGHT('0'+cast(maand as varchar(2)),2)+'-'
+RIGHT('0'+cast(dag as varchar(2)),2) as datetime) as datum,
Inhoud as artikel,
LEN(Inhoud)-LEN(Replace(Inhoud,' ','')) as numwords,
'Goirle' as vestiging
from [Sitecore_Bibliotheekmb_Krantenknipsel].[dbo].[KRANGOI]
You are not casting the Varchars to a Datetime
CAST and CONVERT on MSDN.
Can you try this:
SELECT CAST(('2010' + '-' + '01' + '-' + '06') AS DATETIME)
If any of your dates are earlier than 1753, a simple datetime type will not be sufficient. For this, you'd need to use the datetime2 datatype introduced in SQL Server 2008.
Also check that all values of maand and dag are valid days and months.