Format a string as American - sql

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.

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 - Converting a non-standard String to DateTime

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)

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.

sql server convert string to datetime

I have this string: '30/05/2010', and I would like to enter it to a smallDatetime field.
In the database it should look something like this 2010-05-30 15:33:25
Any Idea how?
TY
use
select convert(smalldatetime,'30/05/2010',103)
SET DATEFORMAT DMY
SELECT CAST('30/05/2010' as smalldatetime)
Where do you want the time aspect to come from? The convert above will append 00:00 (midnight) for smalldatetime because:
the string has no time information
smalldatetime resolves to a minute resolution
You need to use the datetime field type if you want in this format 2010-05-30 15:33:25. If you want only the date, use the date type only.
You can use cast('05/30/2010' as smalldatetime).
If you need to have exact 15:33:25 time then you can use several dateadd calls, e.g. select dateadd(hh, 15, cast('05/30/2010' as smalldatetime)) returns 2010-05-30 15:00:00.