Hive date function to achieve day of week
Asked Answered
A

7

20

I'm looking for a workaround or hive date functions that gives day of the week ,

Sunday - 1
Monday - 2
Tuesday - 3
Wednesday - 4
Thursday - 5
Friday - 6
Saturday - 7

Requirement in detail : I'm looking for a function that takes date string (YYYYMMDD) as input and outputs the day of the week as per the above table.

Abohm answered 10/4, 2014 at 8:40 Comment(1)
You need to write UDF for this. Can you explain your requirement ? Also what is the data type of that column ?Bonheur
B
-8

As I said you need to write a UDF which will accept a string as parameter and return a string. Inside the UDF you need to do these steps:

1.) Parse the input string using SimpleDateFormat(YYYYMMDD)

2.) Use the Below code to get the day of week:

Calendar c = Calendar.getInstance();
c.setTime(yourDate);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);

3.) Use this dayOfWeek value in a case statement to get your weekday String and return that string.

Hope this helps...!!!

Bonheur answered 10/4, 2014 at 13:31 Comment(1)
Completely unnecessary to write your own UDF for this.Diatribe
I
46

Consider using from_unixtime(your date,'u') - this will return day number of week starting from Monday=1. If your date is not in unixtime format, you can use the following instead:

from_unixtime(unix_timestamp('20140112','yyyyMMdd'),'u')

see: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html for simple date format documentation.

Irita answered 11/6, 2014 at 21:59 Comment(5)
It works only from Java 7, on Java 6 it fails (no 'u' option in docs.oracle.com/javase/6/docs/api/java/text/…).Anus
Also, if you want the text version of the weekday (i.e. "Mon", "Tue" etc.), you can use 'E' in place of 'u'Congress
Monday is 1, Sunday is 7Mumps
greatful if you could share any thoughts on thisNaphtha
#68568502Naphtha
R
24

You can now use date_format (Hive 1.2):

hive> select date_format('2016-12-01' ,'u');
OK
4
Ramakrishna answered 10/1, 2017 at 21:51 Comment(0)
G
13

select pmod(datediff(your_date,'1900-01-07'),7) + 1 as WeekDay from your_table

  • arbitrary start date picked (1900-01-07)
  • calculates the mod 7 day of week (plus 1 to start at 1 instead of zero)
Gabriellegabrielli answered 28/5, 2014 at 17:16 Comment(0)
S
4

Expanding on iggy's answer, here is the query to get the days of the week. Adjust the query to set the first day of the week as necessary.

SELECT current_date AS `Date`,
       CASE date_format(current_date,'u')
           WHEN 1 THEN 'Mon'
           WHEN 2 THEN 'Tues'
           WHEN 3 THEN 'Wed'
           WHEN 4 THEN 'Thu'
           WHEN 5 THEN 'Fri'
           WHEN 6 THEN 'Sat'
           WHEN 7 THEN 'Sun'
END AS day_of_week
Sordino answered 24/3, 2019 at 1:56 Comment(0)
L
0

From Hive 2.2 there is another possibility:

hive> select extract(dayofweek FROM your_date) FROM your_table;
Laminate answered 8/1, 2019 at 15:13 Comment(0)
C
0
Select date_format(current_date, 'EEEE');

Which gives Sunday, Monday, Tuesday etc

Compulsory answered 10/5, 2023 at 13:28 Comment(0)
B
-8

As I said you need to write a UDF which will accept a string as parameter and return a string. Inside the UDF you need to do these steps:

1.) Parse the input string using SimpleDateFormat(YYYYMMDD)

2.) Use the Below code to get the day of week:

Calendar c = Calendar.getInstance();
c.setTime(yourDate);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);

3.) Use this dayOfWeek value in a case statement to get your weekday String and return that string.

Hope this helps...!!!

Bonheur answered 10/4, 2014 at 13:31 Comment(1)
Completely unnecessary to write your own UDF for this.Diatribe

© 2022 - 2024 — McMap. All rights reserved.