Getting Week Of Date (Week Beginning) in Hive - 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))

Related

Convert week of the year to date in oracle sql

How can I get a date using the week of the year in Oracle SQL?
I need to search for entries created after the beginning of the current week. Currently, I get the week of the year by doing select to_char(sysdate,'WW') from dual;, but then I can't do
select * from table where date > to_date(to_char(sysdate,'WW'), 'WW') because I get
ORA-01820: format code cannot appear in date input format
01820. 00000 - "format code cannot appear in date input format"
*Cause:
*Action:
You don't need to convert to a string and back, you can use truncate:
select * from table where date > trunc(sysdate,'WW')
Read more about the trunc() function and how the format model is applied.
Notice that WW gives you the same day as the first day of the year, so right now that would give 2020-09-02, which is a Wednesday - possibly not what you'd expect. It depends on your requirements of course, but you might want to work with IW which always starts from Monday, and would give 2020-09-07. If you have a different start day you can add or subtract a day, e.g. if your week starts on Sunday.
According to ORA-doc:
ORA-01820: format code cannot appear in date input format
Cause: A date specification contained an invalid format code. Only the following may > be specified when entering a date: year, month, day, hours, minutes, seconds, Julian day, > A.M./P.M. and B.C./A.D.
Action: Remove the invalid format code from the date specification.
You can't pass the weeknum to to_date() function. What you can do is e.g., the following
select * from table where date > (next_day(trunc(sysdate), 'SUNDAY') - 7)
Basically, next_day returns first date that meets specified weekday. Let's assume it's Monday 2020-09-07, next_day will return you the closest SUNDAY in the future, that is 2020-09-13, so you need to substract 7 to get date of the current week beginning. You can read more about it here

Getting day of week from date column in prestosql?

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.

Hive start of week on Sunday?

I'm trying to get the start of the week (Sundays, as a date) for a given date. This works except on Sundays since the day of the week origin begins on Monday:
SELECT DATE_SUB(FROM_UNIXTIME(UNIX_TIMESTAMP(CURRENT_DATE(), 'yyyy-MM-dd')), CAST(FROM_UNIXTIME(UNIX_TIMESTAMP(CURRENT_DATE(), 'yyyy-MM-dd'), 'u') AS INT))
The function above would return '2018-04-15' for a supplied date of '2018-04-22' whereas I want '2018-04-22'. Is the only recourse to write an case statement to offset for Sundays? I was hoping there was a nice parameter to FROM_UNIXTIME() that would have the weeks start on Sundays. I didn't find them in these docs:
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF
https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
You could get the result with a simple trick without case statements.
Calculate the modulus value of the weekday with 7 and you should get your result.
SELECT DATE_SUB(CURRENT_DATE(), CAST(DATE_FORMAT(CURRENT_DATE(),'u')%7 AS INT));

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 find previous month's start, end dates in epoch integer format

I am using below query which is giving me correct result for finding the last day of the previous month in DATETIME format.
SELECT (DATEADD(dd,-(DAY(GETDATE())),GETDATE()))
However, I want the result in the epoch integer DATETIME format. Anyone knows how to get it?
I am on MSSQL - 2012 DBMS
You can use the DATEDIFF() function to get number of days from one date to another:
SELECT DATEDIFF(day,CAST(0 AS DATETIME),(DATEADD(dd,-(DAY(GETDATE())),GETDATE())))
Also, you can use the EOMONTH() function to get the last day of a month in SQL 2012:
SELECT DATEDIFF(day,CAST(0 AS DATETIME),EOMONTH(DATEADD(month,-1,GETDATE())))
Your epoch start point can be adjusted, but 0 is a natural jumping off point in SQL Server.
Update: If you want '1970-01-01' to be your start date and you want it to be in seconds instead of days:
SELECT DATEDIFF(second,'19700101',EOMONTH(DATEADD(month,-1,GETDATE())))