I have a column filled with dates in string format, e.g. 2023-01-31 11:21:33 GMT.
I am trying to write a query that will select a year and a month and will do some calculations later on. My standard approaches using EXTRACT(YEAR FROM a)) etc. did not work. Therefore, I am trying to parse datetime using PARSE_DATETIME(a, 'yyyy-mm-dd hh:mm:ss'). The thing is, I don't know how to format "GMT" and google did not help with that.
The error message is INVALID_FUNCTION_ARGUMENT: Invalid format: "2023-01-31 11:21:33 GMT" is malformed at "GMT".
Use 'yyyy-MM-dd HH:mm:ss z':
select parse_datetime('2023-01-31 11:21:33 GMT', 'yyyy-MM-dd HH:mm:ss z')
Output:
_col0
2023-01-31 11:21:33.000 UTC
parse_datetime is Java date function which uses JodaTime’s DateTimeFormat pattern format which is mostly compatible with java.text.SimpleDateFormat with z matching general timezone.
Related
I have column name requestdatetime with data type string.
Value for requestdatetime is in format 15/Aug/2022:01:54:41 +0000
I need to convert 15/Aug/2022:01:54:41 +0000 into 'yyyy-MM-dd HH:mm:ss' format.
I have tried date_parse(requestdatetime,'%d/%b/%Y'':''HH:mm:ss'' ''+SSS') but it not working out.
You need to convert string to date then date to string to get expected result.
select date_format(parse_datetime('15/Aug/2022:01:54:41 +0000','dd/MMM/yyyy:HH:mm:ss Z'), '%Y/%m/%d %T')
result:
2022/08/15 01:54:41
date_parse accepts MySQL date format, try parse_datetime which accepts Java format (do not forget to add part for timezone offset - Z):
SELECT parse_datetime('15/Aug/2022:01:54:41 +0000', 'dd/MMM/yyyy:HH:mm:ss Z');
Output:
_col0
2022-08-15 01:54:41.000 UTC
In hive there is some data I have. Now I want to convert the start_timestamp into unix_timestamp in second. How to do that? Because the start_timestamp has two formats:
First format:
2018-03-22 02:54:35
Second format:
May 15 2018 5:15PM
First format is 'yyyy-MM-dd HH:mm:ss', second is 'MMM dd yyyy hh:mm:aa'. If the format is wrong, unix_timestamp function will return NULL. Try to convert using one format, if NULL, try to convert using the other format. This can be done using coalesce function:
select
coalesce(unix_timestamp(start_timestamp ,'yyyy-MM-dd HH:mm:ss'),
unix_timestamp(start_timestamp ,'MMM dd yyyy hh:mm:aa')
) as UnixTimestamp
from my_table;
Use from_unixtime() to convert it back to given format if necessary, like in this answer.
See patterns examples here: SimpleDateFormat
If I have dates that are in this format: Sep-29-07 13:45:00 PDT
How would I convert this into a Date object in oracle 11g? I have tried this:
SELECT TO_DATE(PublishDate, 'MON-DD-YY HH24:MI:SS')
FROM Order;
But I am getting an error saying:
ORA-01830: date format picture ends before converting entire input string
If you have only one time zone's dates, you can just ignore the timezone part using:
select to_date(PublishDate, 'MON-DD-YY HH24:MI:SS "PDT"')
from Orders;
If you plan to support multiple timezones, then store them with timestamp with time zone format. use correct timezone abbreviation (PST instead of PDT - See this) and convert the string, use to_timestamp_tz.
select to_timestamp_tz(PublishDate, 'MON-DD-YY HH24:MI:SS TZR')
from Orders;
or perhaps, convert them in one local time zone. See this answer for that
You can ignore the timezone by using:
SELECT TO_DATE(SUBSTR(PublishDate, 1, length(PublishDate) - 4), 'MON-DD-YY HH24:MI:SS')
However, if you want to handle different timezones, you need to store the standard format, something like 'America/Los_Angeles'.
For example:
SELECT TO_TIMESTAMP_TZ(PublishDate, 'MON-DD-YY HH24:MI:SS TZR')
from (select 'Sep-29-07 13:45:00 America/Los_Angeles' as PublishDate from dual) x;
You can this modify this with 'PDT', but that is not acceptable by itself.
I am writing this query, to find data between two dates. The time format I have is exactly like the one I am using in the query
select TO_CHAR(REC_NO),(FIRSTNAME ||' '|| LASTNAME) as NAME,
LOC_NAME,TO_CHAR(START_TIME,'yyyy/mm/dd/HH:MI:SS'),
TO_CHAR(END_TIME,'yyyy/mm/dd/HH:MI:SS'),
TT_NO,CUST_ID,CUST_MOB,MAC_ADDR,EMAIL_ID,s.STATUS
from vw_rtb_visit_assn v
left join vu_issue_status#jiradb s on v.TT_NO = s.TICKETNUMBER and TT_NO = '123'
and v.ASSN_TIME between to_date('Tue Dec 16 00:00:00 PKT 2014','yyyy/mm/dd')
and to_date('Wed Dec 17 00:00:00 PKT 2014','yyyy/mm/dd')
My query doesn't execute and gives me a format exception.
your date are not in actual format of that particular character you pass in where condition
to_date('Tue Dec 16 00:00:00 PKT 2014','yyyy/mm/dd')
it should be
to_date('Tue Dec 16 00:00:00 PKT 2014','DY MON DD HH24:MI:SS TZD YYYY')
the format of DATE_IN_CHAR and FORMAT in to_date('DATE_IN_CHAR','FORMAT') should match.
kindly try the below
and v.ASSN_TIME between to_date('2014/12/16','yyyy/mm/dd')
and to_date('2014/12/17','yyyy/mm/dd')
Dates do not have formats. Formats are used for parsing strings as dates or generating strings from dates. You don't have to do any of these, you simply need to specify the interval as a date literal, as described in the documentation, eg:
and v.ASSN_TIME between DATE '2014-12-16' AND DATE '2014-12-17'
Date literals are actual date values, not strings that have to be parsed using a specific format.
You can also specify TIMESTAMP literals with
TIMESTAMP '1997-01-31 09:26:50.124'
or
TIMESTAMP '1997-01-31 09:26:56.66 +02:00'
for a TIMESTAMP WITH TIMEZONE.
I currently have a timestamp in this format Tue Jun 03 17:17:05 +0000 2014 in one column in my table. I want to count the number of records happening in specific intervals (15 minutes). I have tried to follow the answer found in Group records by time. Although my timestamp is in a different format and I haven't seen any support function available in SQLite to convert this. Is this possible in SQL?
The SQLite date and time functions can be used to convert a timestring to a canonical format, or to a Julian Day Number. Unfortunately, the SQLite date and time functions only accept timestring in a limited number of formats:
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
If your timestring format has fixed field widths, you can use the substr function and the || string concatenation operator to convert it to a format SQLite understands. You'll have to use a case expression to convert the month names to numbers; here's an example.
You may use NEW_TIME in Oracle to convert the time to a specific timezone. Here is an example. This example is converting SYSDATE from PDT to GMT.
SELECT NEW_TIME (SYSDATE, 'PDT', 'GMT') FROM DUAL;
This thread is detailing how to add required minutes to your timestamp.