Getting day of week from date column in prestosql? - sql

I have a date column called day such as 2019/07/22 if I want to create a custom field that translates that date to the actual day of week it is such as Sunday or Monday how is this possible? I cant seem to find a method that works for presto sql.
Thanks for looking

You can use the format_datetime function to extract the day of week from a date or timestamp:
SELECT format_datetime(day, 'E')
FROM (
VALUES DATE '2019-07-22'
) t(day)
produces:
_col0
-------
Mon
If you want the full name of the day, use format_datetime(day, 'EEEE'):
_col0
-------
Monday

You can try extract('day' from day ) as day_name.

Related

How can I convert a string quarter year to a timestamp in Databricks 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

Getting Week Of Date (Week Beginning) in Hive

I currently have a code that takes a date and returns a Sunday Start date. What I want is to get a Monday start date instead like the weekofyear() function.
Below is my current code where evt_time is my datetime variable:
date_sub(evt_time,pmod(datediff(to_date(evt_time),'1900-01-07'),7))
For instance, I would want 6/4/2018-6/10/2018 to be group into 6/4/2018.
Get the weekday with u argument and then use arithmetic to get the week start date as Monday.
select date_add(to_date(evt_time)
,1-cast(from_unixtime(unix_timestamp(to_date(evt_time),'yyyy-MM-dd'),'u') as int))

Default Day using to_Date with time only

I was trying to get a date in oracle with today's date plus a provided time (hours and minutes)
The query was:
select TO_DATE ('02:03', 'hh24:mi') from dual
I was surprised by the result:
01/08/2017 02:03:00
It seems that the day is defaulted to the 1st of the month (I tried this on 3rd of the month) but the month and year are preserved.
Is that something expected or documented anywhere?
I think you can find the answer here
If you specify a date value without a date, then the default date is
the first day of the current month.

Get name of weekday in netezza

I can get week number using extract dow function in netezza for a date.
Select extract(Dow from date) from table
How can I get name of the weekday?
I haven't tried with a date datatype, but to get the day name from a timestamp you would use
select to_char(date, 'Day') from table
That should give results of Sunday, Monday, Tuesday, etc. Try it with your date column and please let us know if it works.

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')