How can I convert a varchar column in SQL Server 2008 within a table of 1 million+ records containing date/time to the following format: MM/DD/YYYY HH24:MI:SS
Example:
'Feb 14 2017 3:00PM' converted to '02/14/2017 15:00:00'
'Feb 18 2017 10:03AM' converted to '02/18/2017 10:03:00'
The date/time column header = ACTION_TM
Please note the double space between the year and time in the first example vs single space of the second. '2017 3:00' vs '2017 10:03'.
Thank you in advance
https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql
sample ... varchar(20) for length... and convert to 20 for format or any desired format.
select convert(varchar(20),getdate(),20)
however in your case you'll have to convert your column into a valid date so should be
select convert(varchar(20),cast('Feb 14 2017 3:00PM' as datetime),20)
select convert(varchar(20),cast('Feb 18 2017 10:03AM' as datetime),20)
the untidy way of doing it is
select convert(varchar(10),cast('Feb 14 2017 3:00PM' as datetime),101) + ' ' + convert(varchar(112),cast('Feb 14 2017 3:00PM' as datetime),114)
Related
I have a date column in text format of "May 30 2022 9:30PM" and it needs to convert to "2022-05-30 21:30:00" in Snowflake SQL
I tried below SQL and its not working.
SELECT cast('May 30 2022 9:30PM' as datetime);
For Snowflake, try the following:
select to_timestamp_ntz('May 30 2022 9:30PM', 'MON DD YYYY HH12:MIAM');
Reference:
https://docs.snowflake.com/en/sql-reference/functions-conversion.html
I have varchar like this "Wed Jan 26 2022"
I need to convert this to date in sql. How can i do this
for Sql Server:
convert(date, substring('Wed Jan 26 2022',5,11),9)
we ignore the Day name (superfluous), and convert the rest using format 9 indicating Mon dd yyyy format.
SQL*plus server (Here's my code) -
If I am right, you want such type of string which is an invalid one to convert that into a valid one so that you can store valid data into the database. then this code you can use->
SELECT TO_DATE('WED JAN 26 2022','DY MON DD YYYY')FROM DUAL;
(Explanation)->
Code will convert invalid date datatype to a valid date data type which is used in Oracle(SQL).
DY = Abbreviated Week Day
DD = Month day indicator
MON = Abbreviated month
YYYY = Four-digit year indicator
Hi guys can i ask if it's possible to convert a text field to a date/time field?
for example i have a field called Month and the field is a short text data type.
Here is an example:
Month
Sep 2016
Nov 2016
Dec 2016
is it possible that i could convert this one to a date time?
Try this
DateValue("1 " & Month)
I'm assuming you want to put the first of the month in the date.
Output should be
9/1/2016
11/1/2016
12/1/2016
in your case
SELECT convert(datetime, 'Oct 2015 12', 0) or
SELECT convert(datetime, 'Oct 2015', 0) or
SELECT convert(datetime, 'Oct 2015')
It's so easy that it can't be simpler:
FirstOfMonth: CDate([Month])
The values will be primo of [Month].
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