Our Community is getting an upgrade! To get everything ready for the relaunch, we’ll be placing the site in read-only mode starting September 21st.
We really appreciate your understanding while we get things set up behind the scenes. Catch up on all the exciting details about the move here.
Need help or have questions? Drop us a line at [email protected]
Created 09-25-2018 10:14 AM
Created 09-25-2018 03:25 PM
Using SQL
-- First Day of the week
select date_sub('2018-09-25',pmod(datediff('2018-09-25','1900-01-07'),7));
+-------------+--+
| _c0 |
+-------------+--+
| 2018-09-23 |
+-------------+--+
-- Last Day of the week
select date_add('2018-09-25',6 - pmod(datediff('2018-09-25','1900-01-07'),7));
+-------------+--+
| _c0 |
+-------------+--+
| 2018-09-29 |
+-------------+--+If my answer helped to solve your problem, accept the answer. It might help others in the community.
Created 09-25-2018 11:05 AM
You can write a custom UDF in Hive to pick any day of the week. You can refer lastDay UDF code as an example
// Code in Custom UDF FirstDayOfWeek
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.setTime(formatter.parse("2018-09-16")); // Actual Date string column here
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
System.out.println(calendar.getTime());
Created 09-25-2018 03:25 PM
Using SQL
-- First Day of the week
select date_sub('2018-09-25',pmod(datediff('2018-09-25','1900-01-07'),7));
+-------------+--+
| _c0 |
+-------------+--+
| 2018-09-23 |
+-------------+--+
-- Last Day of the week
select date_add('2018-09-25',6 - pmod(datediff('2018-09-25','1900-01-07'),7));
+-------------+--+
| _c0 |
+-------------+--+
| 2018-09-29 |
+-------------+--+If my answer helped to solve your problem, accept the answer. It might help others in the community.
Created 09-28-2018 07:04 AM
Hi
I tried for my scenario I am getting
Also week must start with monday and end with sunday.
select date_sub('2018-09-12',pmod(datediff('2018-09-12','1900-01-07),7))
2018-09-09 I am getting sep 9 th which is Sunday. but for my scenario the week must start with sep 10 th monday
select date_add('2018-09-12',7 - pmod(datediff('2018-09-12','1900-01-07'),7));
2018-09-16
Created 09-28-2018 07:21 AM
If First day of week should start from Monday, change the subtraction/addition date to 1900-01-08
--First day of the week as Monday
select date_sub('2018-09-12',pmod(datediff('2018-09-12','1900-01-08'),7));
+-------------+--+
| _c0 |
+-------------+--+
| 2018-09-10 |
+-------------+--+
--Last day of the week as Sunday
select date_add('2018-09-12',6 - pmod(datediff('2018-09-12','1900-01-08'),7));
+-------------+--+
| _c0 |
+-------------+--+
| 2018-09-16 |
+-------------+--+