I have string dates that I need converted to the date format. How do I take dates like "Jan 1, 2012 12:40:17 AM" and convert it to 1/1/2012? It doesn't work when I try to cast it as a date or when I use the date function.
Try (play with) below
#standardSQL
WITH yourTable AS (
SELECT 'Jan 1, 2012 12:40:17 AM' AS dt
)
SELECT
dt,
PARSE_TIMESTAMP('%b %d, %Y %r', dt) AS dt_as_timestamp,
DATE(PARSE_TIMESTAMP('%b %d, %Y %r', dt)) AS dt_as_date,
FORMAT_DATE('%m/%d/%Y', DATE(PARSE_TIMESTAMP('%b %d, %Y %r', dt))) AS dt_as_string
FROM yourTable
see more
Related
I have tried below code to convert the normal date to Julian date but now I want to convert HH:MM:SS to Julian time
converting a Julian date
SELECT DATEPART(YEAR, GETDATE()) * 1000 + DATEPART(dy, GETDATE())
Ņonverts back to a regular date
SELECT DATEADD(HOUR, CAST(RIGHT('13000', 3) AS INT) - 1, CAST(CONCAT('', LEFT('13000', 4)) AS time)) AS in_date
How can I convert 2019-07-01T00:00:00+05:30 to DateTime in SQL?
2019-07-01T00:00:00+05:30 is a varchar field. I need to convert this into DateTime to compare this to a date field.
suggest me a query to Convert (2019-07-01T00:00:00+05:30) into DateTime
Convert To date :
select cast('2019-07-01T00:00:00+05:30' as Date)
Convert To time:
select cast('2019-07-01T00:00:00+05:30' as Time)
Convert To datetime :
select convert(datetime2, '2019-07-01T10:00:30+05:30',0)
Try any of these..
select cast(convert(datetime2, '2019-07-01T10:00:30+05:30',0) as datetime)
select convert(datetime2, '2019-07-01T10:00:30+05:30',0)
One option would be to use a combination of CONVERT on the timestamp without the timezone component, then use TODATETIMEOFFSET with the timezone portion to get the final result:
WITH yourTable AS (
SELECT '2019-07-01T00:00:00+05:30' AS dt
)
SELECT
TODATETIMEOFFSET(CONVERT(datetime, LEFT(dt, 19), 126), RIGHT(dt, 6)) AS output
FROM yourTable;
This outputs:
01/07/2019 00:00:00 +05:30
Demo
Unfortunately, SQL Server truncates the time zone information when converting from datetimeoffset to dateordatetime`. But, you can calculate the offset and add it back in:
select dateadd(minute,
datediff(minute, convert(datetimeoffset, dt), convert(datetime, convert(datetimeoffset, dt))),
convert(datetime, convert(datetimeoffset, dt))
)
from (values ('2019-07-01T00:00:00+05:30')) v(dt);
For your particular timezone, the date at midnight matches the UTC date, so you are safe. I'm on the other side of the world, so this would be a more important consideration in the "western" world ("west" being west of UTC).
The following query will convert the given VARCHAR to DATETIME value:
DECLARE #DateVal AS VARCHAR (30) = '2019-07-01T00:00:00+05:30';
SELECT CAST(REPLACE(SUBSTRING(#DateVal, 0, CHARINDEX('+', #DateVal)), 'T', ' ') AS DATETIME);
I have a oracle form that the user loads data. The date format the user uses is different than what I have in my list of values. Need help in adding this format
SELECT
TO_CHAR(TO_DATE( 'Sunday, November 4, 2018', 'DD MON YYYY' ))
FROM
DUAL;
Convert to '11/4/2018'.
You can convert a string like 'Sunday, November 4, 2018' to a DATE datatype with this expression:
TO_DATE( 'Sunday, November 4, 2018', 'Day, Month DD, YYYY' )
Then it is possible to convert the date to a string in another format with TO_CHAR().
You seem to be looking for:
SELECT
TO_CHAR(TO_DATE( 'Sunday, November 4, 2018', 'Day, Month DD, YYYY' ), 'MM/DD/YYYY')
FROM DUAL;
This yields:
11/04/2018
Demo on DB Fiddle
Using Hive, I have date dates in yyyyMMdd format and I need it in 'MM/dd/yyyy' format.
SELECT dt,
CAST(SUBSTRING(FROM_UNIXTIME(UNIX_TIMESTAMP(dt, 'MMddyyyy')), 1, 10) AS date)
FROM timetable
No need for cast and substring.Specify the dateformat for dt in unix_timestamp() and the desired dateformat for from_unixtime()
select
dt,
from_unixtime(unix_timestamp(dt,'yyyyMMdd'),'MM/dd/yyyy')
from timetable;
I have data in following format-
Fri, May 13, 2016 at 10:54 PM
The value is inserted as varchar2 type.
I wanted to convert it to datetime type to insert in another column.
How can this be possible?
You don't need regular expressions at all:
WITH your_table AS (
SELECT 'Fri, May 13, 2016 at 10:54 PM' text_date FROM DUAL
)
SELECT
TO_DATE(text_date, 'DY, Mon DD, YYYY "at" HH:MI AM', 'NLS_DATE_LANGUAGE = ENGLISH') adate
FROM your_table
You can extract the day, month, year and time from the text string using a regular expression and use to_date to convert it to a date:
WITH your_table AS (
SELECT 'Fri, May 13, 2016 at 10:54 PM' text_date FROM DUAL
)
SELECT TO_DATE(REGEXP_REPLACE(text_date, '(\w{3}), (\w{3}) (\d{1,2}), (\d{4}) at (\d{1,2}:\d{2}) (AM|PM)','\3-\2-\4 \5 \6'),'DD-MON-YYYY HH:MI AM') adate
FROM your_table