Date Format Issue, ORACLE - sql

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.

Related

How to insert DD MON YYYY format value into a date column in a snowflake table

My csv file has a date column having values in DD MON YYYY format eg: 28 Nov 2022.
When i tried inserting it into a date column(datatype= DATE) it is showing the below error. I have also tried using TO_DATE , TO_VARCHAR but getting the same error.
Kindly help me to resolve this.
Error: Date '28 Nov 2022' is not recognized
I want to insert the value in the same format (DD MON YYYY) into a column of date data type ,without changing the format i.e '28 Nov 2022'.
I was reading documentation (https://docs.snowflake.com/en/sql-reference/data-types-datetime.html#date) and i read: "DATE accepts dates in the most common forms (YYYY-MM-DD, DD-MON-YYYY, etc.)."
So i think the format you are trying to write is a no-supported date format.
you can:
format your date in a supported date format before write field in db.
write in a varchar datatype field, but in this case you'll lose all tools on date type.
I don't see other ways!

How to convert string with GMT to date in Athena

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.

Date_Trunc and To_Date Questions SQL

Can we use date_trunc for a date (not date-time) that we are trying to "truncate" (not sure if the term can be applied here) to e.g. the start of the week? So if I have date_trunc(week, 28/10/2020) and I want to get the start of the week that 28th of October lies in (so 26th of October)? I tried this in my SQL command line but I get error messages.
If I am doing: SELECT to_date ('02 Oct 2001', 'DD Mon YYYY'); How can I ensure the resulting format is in a date format I specify (rather than the default date format)? For example if I want it in format DD-MM-YYYY?
select to_char(date '2017-06-02', 'MM') < in this example, why do we need "date" for this to work? The general format for to_char should be TO_CHAR (timestamp_expression, 'format'). I don't see in this general format that we need "day".
if I have a WHERE filter like to_char(order_date, '20-10-2020'), and there are indeed some rows with this order date, will these rows still show in my results (after running query) if these rows are in DATE format (so 20 Oct is in date format) as opposed to string (which is what I am filtering by as I am doing to_char). I know there would be no need to use to_char in this case but just asking..
yes, you can use date in text form but you have to cast it to a correct type
these queries will work
select date_trunc('week', '2020-10-28'::date);
select date_trunc('week', '10/28/2020'::date);
-- as well as
select date_trunc('week', '2020-10-28'::datetime);
and return timestamp 2020-10-26 00:00:00.000000
note, next query
select date_trunc('week', '28/10/2020'::date);
will fail with error date/time field value out of range: "28/10/2020";
You can use to_char, it returns text, if you need a date format you have to case it again
select to_char( to_date ('02 Oct 2001', 'DD Mon YYYY'), 'DD-MM-YYYY')::date;
select to_char('02 Oct 2001'::date, 'DD-MM-YYYY')::date;
'2017-06-02' is a text and it can't be automatically converted to timestamp. Actually I don't know a text format which can.
No, you need to explicitly cast into date type to use it as a filter
where order_date = date_stored_as_a_text::date
I am answering the questions in a different order as there are some wrong assumptions:
Question 3
There is a general difference between '2017-06-02' and date '2017-06-02' - the first one is just a varchar, a string, NOT handled as a date by Redshift, the 2nd one tells Redshift to handle the string as date and therefore works.
Question 2
A date data type column has no format - you may an sql client that can display date columns in different formats, however, this is not a functionality of redshift. SELECT to_date ('02 Oct 2001', 'DD Mon YYYY'); tells redshift to convert the string '02 Oct 2001' to date.
Question 1
DATE_TRUNC('datepart', timestamp) also supports week as datepart - see Date parts for date or timestamp function (Also shown in the example of AWS). You should also be able to provide a date instead of a timestamp.
Question 4
to_char(order_date, '20-10-2020')is not a filter and you are using it wrong.
AWS TO_CHAR
TO_CHAR converts a timestamp or numeric expression to a character-string data format.
I guess you are rather looking for:
where to_char(order_date, 'YYYY-MM-DD') = '20-10-2020'

varchar2 string to Date format

I am trying to convert the below string value to TO_DATE but oracle is not
recognizing the date format. Where am I going wrong.
SELECT TO_DATE('Wed Oct 10 23:50:00 2018 UTC','Dy Mon DD HH24:MI:SS YYYY TZR','NLS_DATE_LANGUAGE = American')
FROM dual;
ERROR at line 1:
ORA-01821: date format not recognized
Use:
SELECT TO_TIMESTAMP_TZ('Wed Oct 10 23:50:00 2018 UTC','DY MON DD HH24:MI:SS YYYY TZR','NLS_DATE_LANGUAGE=American')
FROM dual;
TO_DATE function returns DATE datatype, which does not support timezones. You are using TZR format specifier in your query (time zone region), and this generates the error because DATE does not support this.
This documentation - Time Zones shows which datatypes supports timezones, and states that only TIMESTAMP WITH TIME ZONE and TIMESTAMP WITH LOCAL TIME ZONE do. So you must convert to one of this datatype instead of converting to DATE.
TO_TIMESTAMP_TZ function converts a literal to TIMESTAMP WITH TIME ZONE datatype.
If (and I realise it's a big 'if') the string values always contain UTC and not any other time zone values, then you could just treat that as a character literal. You would do that by changing your format model from TZR, which isn't recognised by to_date(), to "UTC" - including the double quotes:
SELECT TO_DATE('Wed Oct 10 23:50:00 2018 UTC',
'Dy Mon DD HH24:MI:SS YYYY "UTC"',
'NLS_DATE_LANGUAGE = American')
FROM dual;
TO_DATE('WEDOCT1023
-------------------
2018-10-10 23:50:00
Of course, as that is a plain date it still has no time zone information, but if you want to retain that then you need a timestamp with [local] time zone data type anyway.

SQLite Data Time Function

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.