SQL - Converting a non-standard String to DateTime - sql

I've got some date/time data in a string, in the format '26/10/2009 09:06:43' (i.e. the date is in UK format). When I try
SELECT CAST('26/10/2009 09:06:43' AS DATETIME)
I get the following error:
The conversion of a char data type to a datetime data type resulted in
an out-of-range datetime value.
I'm sure the solution is really, really simple but every example I can find online seems to overcomplicate things!
I'd appreciate any help you can give!

Try with convert function instead providing style number:
SELECT convert(DATETIME, '26/10/2009 09:06:43', 103)
You can find out all formats here: http://www.sqlhub.com/2009/04/list-of-all-available-datetime-format.html

In oracle it's like that :
SELECT to_date('26/10/2009 09:06:43', 'DD/MM/YYYY HH24:MI:SS') from dual;
with a following site :
http://www.sqlines.com/oracle-to-sql-server/to_date
It's seems to be like this in sql server :
SELECT CONVERT(DATETIME, '26/10/2009 09:06:43');

Before the conversion, set the language to British English:
set language [British English]
SELECT CAST('26/10/2009 09:06:43' AS DATETIME)
Or you can use the convert function, and declare the style too:
SELECT convert(DATETIME, '26/10/2009 09:06:43', 103)

Related

Convert datetime string in datetime format in SQL server

I'm using MS SQL server and I have a date field of type text. The dates stored there are in this format
2017-03-01T18:23:02+0700
I'm trying to convert this field in a datetime field but I fail. I have tried
CONVERT(datetimeoffset,date, 127)
CONVERT(datetime,date, 127)
CONVERT(datetime2,date, 127)
but I keep getting
Conversion failed when converting date and/or time from character
string.
I think the problem is that according to ISO8601 the time offset must be in the format hh:mm while mine is hhmm. I don't mind keeping only the date (yyyy-mm-dd) if it is more easy.
I have read similar question but none matches exactly my case and I can't figure out the solution.
Try this
Declare #dt varchar(50)
set #dt = '2017-03-01T18:23:02+0700'
select convert(datetime, replace(LEFT(#dt, LEN(#dt) - 1), '+', '.'), 126)
If you need only date part then you can use below query
SELECT CAST(LEFT('2017-03-01T18:23:02+0700',10) as DATE)
Use Below query to convert datetime :
SELECT CONVERT(DATETIME,REPLACE(REPLACE('2017-03-01T18:23:02+070','T','
'),'+','.'),103)
For DATE only use below query :
SELECT CONVERT(DATE,REPLACE(REPLACE('2017-03-01T18:23:02+010','T','
'),'+','.'),102)
It doesn't seem to work in both ways (both raise error):
SELECT CONVERT(datetime,'2017-03-01T18:23:02+0700',127)
SELECT CONVERT(datetime,'2017-03-01T18:23:02+07:00',127)
It seems that it works only specifying Z as time zone:
SELECT CONVERT(datetime,'2017-03-01T18:23:02Z',127)

SQL date formats

How can I convert this 201402110544 to date(2014-02-11) in SQL server 2008?
You can cast as DATE data type (https://msdn.microsoft.com/en-us/library/bb630352(v=sql.100).aspx)
SELECT CAST(datetime_value AS DATE)
SELECT CAST(GETDATE() AS DATE) --> 2015-08-18
If you have a string to cast as DATE you can use:
SELECT CAST(LEFT('201402110544', 8) AS DATE)
You trim out the time part, by taking 1st 8 chars (YYYYMMDD) and will result a valid string to cast as DATE.
If this format is always the same, you can do this
DECLARE #d VARCHAR(20)='201402110544'
SELECT CAST(SUBSTRING(#d,0,9) as DATETIME)
Also have a look at The ultimate guide to the datetime datatypes which explains in detail about handling date-times
String to date conversion sure is a weakness of SQL Server. CONVERT does that, but can only deal with a number of given formats. So you must convert your string into such a format first and then convert it to date afterwards.
One such format is 120 = 'yyyy-mm-dd hh:mi:ss' which I think comes closest to yours. Another 102 = 'yyyy.mm.dd' suffices to get the date.
convert(
date,
concat(substring(datestring, 1,4), '.',
substring(datestring, 5,2), '.',
substring(datestring, 7,2)),
102)
from data;
SQL fiddle: http://www.sqlfiddle.com/#!3/9eecb7/3689
EDIT: I stand corrected. Horia is right; CAST supports 'yyyymmdd', so you can use it directly by merely cutting of the string's time part. This is more readable than my suggestion above, which makes CAST the better option in your case.

Format a string as American

Please see the SQL below:
select cast('13/01/2015' as datetime)
The error is: 'The conversion of a varchar data type to a datetime data type resulted in an out-of-range value'. I know I can do this will resolve it:
select cast('01/13/2015' as datetime)
Is there a way of formatting a string of: '13/01/2015' as '01/13/2015'
Don't use cast, use Convert. this way you can choose the date format.
select convert(datetime, '13/01/2015', 103)
select convert(datetime, '01/13/2015', 101)
You can set the dateformat and do something like
set dateformat dmy
select cast('13/01/2015' as datetime)
I would use CONVERT, but if you choose to use CAST then you might want to try this...
SET LANGUAGE british
SELECT CAST('13/01/2015' AS Datetime)
SET LANGUAGE us_english
SELECT CAST('01/13/2015' AS Datetime)
This still wont fix your formatting issue though. That's why I would use ..
CONVERT(DATETIME,'01/13/2015',101)
You could try:
SELECT CONVERT(CHAR(10),CONVERT(DATETIME,'13/01/2015',103),101))
This will convert the string to a date time in the format of DD/MM/YYYY, and then it will convert it to the MM/DD/YYYY format you are looking for.
Edit Note: I noticed you wanted the resultant in a string.

Date conversion failed from string

I have a date saved in the format DD/MM/YYYY from a flat file. "20/04/2013"
When I try to insert it into my SQL Server database it changes the value to MM/DD/YYYY.
So of course there in no month 20 and my code fail. How can I work around this?
I have tried stuff like this and I had no luck.
SELECT CONVERT(datetime, CONVERT(varchar, '20/04/2013', 101))
just do this directly,
SELECT CONVERT(datetime, '20/04/2013', 103)
On SQL Server you need to use the SET DATEFORMAT option. (See http://msdn.microsoft.com/en-us/library/ms189491.aspx).
In your case, you would need to issue the following command before executing the above SELECT-statement:
SET DATEFORMAT dmy
Alternatively, it is possible to globally change the DATEFORMAT setting for the server.
Definition and Usage
The CONVERT() function is a general function that converts an expression of one data type to another.
The CONVERT() function can be used to display date/time data in different formats.
select convert(datetime, '20/04/2013', 103)
enter link description here

datetime conversion issue in sql server

if i try to cast string date to datetime like
select cast('12/01/2010' as datetime) then
it works
but if i try to cast like
select cast('22/01/2010' as datetime) then it is giving error.
again if i try to cast string date to datetime like
select cast('2010/12/01' as datetime) then it works
but if i try to cast like
select cast('2010/25/01' as datetime) then it is giving error. my requirement is whatever way user input date that should be successfully converted to datetime. please tell me best solution
In your case the sql server assumes, that date format id mm/dd/yyyy - US format,
you want to use French format, so use convert
select CONVERT(DATETIME, '12/01/2010', 103)
instead you'll get an error because there in no such a month number - 22
The best solution when passing datetime as string - to use short(without timezone) ISO format:
yyyyMMdd HH:mm:ss.ffff
I recommend you to use fixed '20111231' format. You do not need to use cast, convert or similar command.