How can I convert a string quarter year to a timestamp in Databricks SQL - sql

In Databricks SQL, how can I convert a date in string format "2021Q2" to a timestamp with the last day of that quarter?

select
to_timestamp(
last_day(
to_date(
(left('2021Q4',4)||'-'||int(right('2021Q4',1)*3))||'-'||'1')))
from
my_table

It is a pity that Q is not working in formatting from string to date object (it is working only in reverse) - parsing Q with to_date(date, "YYYY'QQ") sadly will not work.
According to https://spark.apache.org/docs/latest/sql-ref-datetime-pattern.html:
Symbols of ‘E’, ‘F’, ‘q’ and ‘Q’ can only be used for datetime formatting, e.g. date_format. They are not allowed used for datetime parsing, e.g. to_timestamp.
Because of that we have to separate quarter and multiple by 4. Than convert it to date object (parse_ and take last_date of the month:
SELECT
last_day(
to_date(
concat(left("2021Q4", 4), int(right("2021Q4", 1))*3),
"yyyyMM")
) as last_day_of_quarter

Simple way :
select to_timestamp(last_day(concat('2021','-',0,4*3,'-01'))) as last_date_queter
Logic :
calculate the last month of quarter by using multiple with 3. example 4th quarter's last month calculated 12 (4*3)
concat (year,'-',-01) so that we can get the first day of respective month 2021-12-01
last_day we can use last date of the given date month.
finally , we can convert the date into timestamp to_timestamp

Related

How to extract date parts from text string in SQLite?

I have a column called event_time with the date & time in the following format:
2020-04-24 11:50:39 UTC
I'm trying to extract different parts of the date, such as year, using the following query:
SELECT event_time, strftime('%y',event_time) AS year
But the result is NULL. I've tried formatting the column as both TEXT and NUMERIC with no luck. Is the "UTC" preventing it from being recognized as a date? What is the best way to extract dates and times that I can work with?
You must remove the UTC suffix and use %Y instead of %y as the format of strftime():
strftime('%Y', SUBSTR('2020-04-24 11:50:39 UTC', 1, 19)) AS year
or:
strftime('%Y', REPLACE('2020-04-24 11:50:39 UTC', 'UTC', ''))
return 2020.
See the demo.

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'

How to fetch month from date where date column is in varchar datatype. FYI using snowflake tool

How to fetch month from date where date column is in varchar datatype. FYI using snowflake tool.
For example if i want data of june month ? how can i fetch ?
You can use the TO_DATE(…) function to treat the VARCHAR column as a formatted date type, and the EXTRACT(…) function to retrieve just the month out of the date.
If your date string is formatted in a well-known manner, TO_DATE's automatic parsing (or a direct cast using the :: operator) will suffice, and you can write your query this way:
SELECT * FROM table
WHERE
EXTRACT(month, TO_DATE(varcharCol)) = 6 -- June of every year
AND EXTRACT(year, varcharCol::DATE) = 2020; -- June of 2020 alone
Alternatively, if the date is in a non-standard format, use available formatting options to make TO_DATE(…) parse it properly:
-- Dates of custom format, such as: 'June # 02 # 2020'
SELECT
EXTRACT(month, TO_DATE(varcharCol, 'MMMM # DD # YYYY')) AS month
FROM table
WHERE
month = 6;
Note: You can also swap all DATE and TO_DATE above with TIMESTAMP and TO_TIMESTAMP if the data carries a whole timestamp value within it instead of only a date.
First of all, you shouldn't store dates as strings. But you probably know that.
If you do store dates as strings, you store them all in one particular format, say, 'mm/dd/yyyy'. So, use a substring function to get the month digits.
For 'mm/dd/yyyy':
where substring(date_string, 1, 2) = '06'
For 'yyyy-mm-dd':
where substring(date_string, 9, 2) = '06'
In many situations you can also use LIKE:
For 'mm/dd/yyyy':
where date_string like '06%'
For 'yyyy-mm-dd':
where date_string like '%-06-%'
You have to use to_date in snowflake to convert varchar datatype to date as following
select *
from yourTable
where to_date(yourDateColumn, 'YYYY-MM-DD') >= '2020-06-01'
and to_date(yourDateColumn, 'YYYY-MM-DD') <= '2020-06-30'

Oracle SQL WHERE MONTH = N

In my select I am using this
(TRUNC(TO_DATE(TIMESTAMP, 'dd.mm.yyyyHH24:mi'))) TIMESTAMP,
to get the following output in a date format: e.g. 22/04/2016
Now I want to add a statement in my WHERE-clause to show only dates in special months, for example only dates which are in MARCH and APRIL
I tried using this:
WHERE (TRUNC(TO_DATE(TIMESTAMP, 'mm'))) in (3,4)
which gives me an error.
Thanks for helping.
Just use EXTRACT() on the timestamp:
WHERE EXTRACT(MONTH FROM timestamp) IN (3, 4)
This would match records from March and April.
In my select I am using this
(TRUNC(TO_DATE(TIMESTAMP, 'dd.mm.yyyyHH24:mi'))) TIMESTAMP,
to get the following output in a date format: e.g. 22/04/2016
TO_DATE takes a string value so your "TIMESTAMP" column will be implicitly converted to a string and then back to a date... which is unnecessary (and relies on the value of the NLS_TIMESTAMP_FORMAT session parameter to format the implicit conversion - which, if changed, will break the query); you can just do:
TRUNC( "TIMESTAMP" ) AS "TIMESTAMP"
If you want to then filter on different months then you can do (as suggested by Tim Biegeleisen):
WHERE EXTRACT( MONTH FROM "TIMESTAMP" ) IN ( 3, 4 )
or if you want a particular year then
WHERE TRUNC( "TIMESTAMP", 'MM' ) IN ( DATE '2016-03-01', DATE '2016-04-01' )
or, so you can use an index on the column:
WHERE "TIMESTAMP" >= DATE '2016-03-01'
AND "TIMESTAMP" < DATE '2016-05-01'

How to extract week number in sql

I have a transdate column of varchar2 type which has the following entrees
01/02/2012
01/03/2012
etc.
I converted it in to date format in another column using to_date function. This is the format i got.
01-JAN-2012
03-APR-2012
When I'm trying to extract the weekno, i'm getting all null values.
select to_char(to_date(TRANSDATE), 'w') as weekno from tablename.
null
null
How to get weekno from date in the above format?
After converting your varchar2 date to a true date datatype, then convert back to varchar2 with the desired mask:
to_char(to_date('01/02/2012','MM/DD/YYYY'),'WW')
If you want the week number in a number datatype, you can wrap the statement in to_number():
to_number(to_char(to_date('01/02/2012','MM/DD/YYYY'),'WW'))
However, you have several week number options to consider:
WW Week of year (1-53) where week 1 starts on the first day of the year and continues to the seventh day of the year.
W Week of month (1-5) where week 1 starts on the first day of the month and ends on the seventh.
IW Week of year (1-52 or 1-53) based on the ISO standard.
Try to replace 'w' for 'iw'.
For example:
SELECT to_char(to_date(TRANSDATE, 'dd-mm-yyyy'), 'iw') as weeknumber from YOUR_TABLE;
Select last_name, round (sysdate-hire_date)/7,0) as tuner
from employees
Where department_id = 90
order by last_name;
Use 'dd-mon-yyyy' if you are using the 2nd date format specified in your answer. Ex:
to_date(<column name>,'dd-mon-yyyy')