How can I convert the string 'Fri, 9 Mar 2012 10:43:21 +0000 (UTC)', stored as a varchar(max), to the datetime value '09-03-2012', which is the date part of the string?
Use convert and reference to this page http://msdn.microsoft.com/en-us/library/ms187928.aspx
e.g.
CONVERT(varchar(8), GETDATE(), 112)
According to MS: http://msdn.microsoft.com/en-us/library/ms187928.aspx
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.
and the table lists 105 as the code for the style you want.
So, for your example date:
CONVERT(VARCHAR(10), 'Fri, 9 Mar 2012 10:43:21 +0000 (UTC)', 105)
should probably do the trick.
I had to chop up your date first, but here goes:
DECLARE #ds varchar(30)='Fri, 9 Mar 2012 10:43:21 +0000 (UTC)';
SELECT CONVERT(VARCHAR(10)
,CAST(SUBSTRING(#ds
,CHARINDEX(' ',#ds)
,LEN(#ds)-(PATINDEX('%[0-9][0-9][0-9][0-9]%',#ds)+6))
AS DATETIME)
,105
)
Result:
09-03-2012
If you have SQL Server 2012, you can try this:
select CONVERT(date, TRY_PARSE('Fri, 9 Mar 2012 10:43:21 +0000' AS datetime2))
NOTE: (UTC) is removed, it didn't parse correctly.
Also, be aware if you need to localize that time value. For example, I live in AZ, so typically I just subtract -7 hrs from UTC times to localize. You will need to make similar adjustments based on the time zone
Related
I am receiving a datetime in the following format:
Thu, 18 Mar 2021 10:37:31 +0000
If I'm correct this if RFC2822.
I can't figure out a way to convert this to a 'normal' datetime that would be used by SQl server.
For example I want it to be:
2021-03-18 10:37:31
YYYY-MM-DD hh:mi:ss
I have tried things like CONVERT() and found a sketchy way by doing:
DECLARE #WeirdDate varchar(50) = 'Thu, 30 Jul 2015 20:00:00 +0000'
SELECT
CONVERT(DATETIME, SUBSTRING(#WeirdDate, CHARINDEX(',', #WeirdDate) + 1, 20))
But none of it is working that well.
Is there a way to convert this in a 'proper' way?
edit:
To clarify:
The format should always be the same as the provided example. Including the day name.
I am not sure that it will always be the same timezone. I could be receiving it from a different timezone. This is something to consider.
You could achieve this with a "little" string manipulation is seems, and some style codes:
DECLARE #YourDate varchar(50) = 'Thu, 30 Jul 2015 20:00:00 +0000'
SELECT TRY_CONVERT(datetimeoffset(0),CONVERT(varchar(25),TRY_CONVERT(datetime2(0),STUFF(STUFF(#YourDate,1,5,''),21,6,''),106),126) + STUFF(RIGHT(#YourDate,5),4,0,':'));
This will, however, fail if you're using a LOGIN with a language setting which isn't an English based one.
If the value is always UTC, you can actually just use the "middle" TRY_CONVERT expression.
In my case the string comes with extra information
Mon Feb 15 2021 15:48:09 GMT-0500 (Colombia Standard Time)
Sun Mar 28 2021 09:59:51 GMT-0500 (hora de Ecuador)
Fri Jun 25 2021 08:54:28 GMT-0500 (Ecuador Time)
I used some replace functions to get rid of the extra information
TRY_PARSE( replace( replace( replace( replace( <<date_field>>, '(Colombia Standard Time)', '' ), 'GMT', '' ), '(hora de Ecuador)', ''), '(Ecuador Time)', '' ) as datetime using 'en-US')
So I have a table and I am trying to convert the time from its current format that looks like this
Sep 17 2020 1:07AM
to
2020-09-16 20:07:00.000
if I try to update the entire table by
UPDATE
OOTYPE
SET
OOTYPE.LastModified = CONVERT(DATETIME, SWITCHOFFSET(CONVERT(DATETIMEOFFSET, LastModified),
DATENAME(TzOffset, SYSDATETIMEOFFSET())))
it runs successfully but the times are still in the format
Sep 17 2020 1:07AM
However, if I just run a simple select statement to show it changed
SELECT CONVERT(DATETIME, SWITCHOFFSET(CONVERT(DATETIMEOFFSET,lastmodified), DATENAME(TzOffset, SYSDATETIMEOFFSET()))) FROM [OOTYPE]
The output is exactly what I'm looking for.....
2020-09-16 20:07:00.000
Anyone have any idea where I'm screwing this simple change up at, I have put an embarrassing amount of time into this....
If I follow you correctly:
SELECT CONVERT(varchar, CONVERT(datetime, 'Sep 17 2020 1:07AM'), 121)
You should use convert style 121 which is yyyy-mm-dd hh:mi:ss.mmm.
I'm trying to convert a datetime column to ISO format, as yyyymmdd. For example, I would like to be able to convert '13 dec 2018' to '20181213'.
According to Microsoft's T-SQL Docs, I should be able to do this using
convert(nvarchar(8), '13 dec 2018', 112)
however this doesn't work - I get the result '13 dec 2', which looks to be nothing more than the original string cut down to 8 characters.
I have also tried using
convert(datetime, '13 dec 2018', 112)
which gives me the result of 'Dec 13 2018 12:00AM' - again, nothing like what the function is supposed to produce.
What I am doing wrong? I could solve the problem easily enough using datepart() and concatenated strings, but I'd rather use the more elegant approach if possible.
Combine them:
convert(nvarchar(8), convert(datetime, '13 dec 2018'), 112)
I don't recommend using the format 112 for the first conversion, because it is misleading. SQL Server is very good at converting without a format. If you do use one the appropriate one is 106.
I figured it out - because I was entering the date as a string, and not converting it to a datetime value first, it was treating my date as if it were a string. When I handle it this way:
convert(nvarchar(8), cast('13 dec 2018' as datetime), 112)
I get the expected result, '20181213'.
select convert(nvarchar(8), cast(GETDATE() as datetime), 112) as TodayDate
YYYYMMDD format can obtain by datetime to varchar format.
select convert(varchar(8),convert(datetime, '13 dec 2018'),112)
The above query provides the required result
I have the following date in our SQL Server 2008 database: 06-24-1881 00:00:00:000 as a DOB and it's stored as datetime.
I need to copy this data into our Oracle 11g database and from my understanding Oracle does not accept milliseconds into a Datetime column. For my data after 1900 I converted the data to smalldatetime and it worked but for this piece of data, it won't convert to smalldatetime since it doesn't allow for dates prior to 1/1/1900.
How do I get many rows of data into my Oracle database?
I tried this:
left(DOB, 19) as DOB
but that rendered the data as "Jun 24 1881 12:00AM", so I tried inserting with:
to_date('Jun 24 1881 12:00AM', 'MON-DD-YYYY HH:MI:SSAM')
and that didn't work either. I am stuck and need help.
Jun 24 1881 12:00AM matches the Oracle format string MON DD YYYY HH12:MIAM. Your format mask has an added seconds :SS mask that Oracle is trying to match but not finding in the input so will throw an ORA-01861: literal does not match format string exception.
So, if the output from SQL Server is in that format then in Oracle you should be able to do:
TO_DATE( 'Jun 24 1881 12:00AM', 'MON DD YYYY HH12:MIAM' )
CONVERT(VARCHAR(20),#Date,101) will give you 06/24/1881 I am looking for code now.
REPLACE(CONVERT(VARCHAR(200),#Date,101),'/','-') + ' 00:00:00:000'
Weird I tried a bunch of the century codes with CONVERT and it worked for 101 and some others but the 113 you want failed but because you don't have minutes, hours, seconds etc. you can just take the the 101 format and manipulate the string to what you want.
DECLARE #Date DATE = '06-24-1881 00:00:00:000'
SELECT #Date
,FORMAT(#Date, 'MM-dd-yyyy HH:mm:ss')
,REPLACE(CONVERT(VARCHAR(200),#Date,101),'/','-') + ' 00:00:00:000'
Note the FORMAT(#Date, 'MM-dd-yyyy HH:mm:ss') is SQL 2012 or newer and works well.
Conversion failed when converting date and/or time from character string.
I'm getting the above error when running this statement in SQL Server:
SELECT CONVERT(datetime, 'Fri, 15 Jan 2016 17:30:05 GMT')
Actually I want to insert same string format in Datetime column
As suggested by Tim Biegeleisen, that string needs to be processed to be converted. In order to convert it you need to strip of the day (Fri,) and the GMT timezone at the end, for example:
DECLARE #date varchar(50) = 'Fri, 15 Jan 2016 17:30:05 GMT'
SELECT CONVERT(DATETIME, SUBSTRING(#date, 5, LEN(#date) - 8), 113)
This solution does strip the timezone information, have a look at this post if you want to convert it back to UTC.
If you want to insert the string 'Fri, 15 Jan 2016 17:30:05 GMT' into datetime column then you need to remove Fri, and GMT from the string before inserting.
SELECT CAST(substring(#str_date,5,len(#str_date)-8) as datetime)
DEMO