A table with 7 rows having all weekdays as value. I just want a simple sql query to get all the values from the weekdays except weekends.
I hope this can help:
SELECT *
FROM [YourTableName] t
WHERE DATENAME(WEEKDAY,t.[YourDateField]) NOT IN ('Saturday','Sunday');
Simple, Try this :
SELECT *
FROM [YourTable]
WHERE DATENAME(WEEKDAY,[DateField]) <> 'Saturday'
AND DATENAME(WEEKDAY,[DateField]) <> 'Sunday'
There is a Similar question with solutions.
MySQL WEEKDAY() function returns the index of the day in a week for a given date (0 for Monday, 1 for Tuesday through to 6 for Sunday).
Syntax:
WEEKDAY(date) Where date is a date.
Related
I would like to know if we can got values from date if the date is in the current month
For example, If I have :
date value
28/06/2021 50
02/07/2021 100
05/07/2021 18
Then I search to have :
02/07/2021 100
05/07/2021 18
because 28/06/2021 is not in the current month
thanks in advance !
In Mysql you can use now() and month() functions to get the current date ang extract the month.
Select *
from table
where <your filters>
having MONTH(NOW()) = MONTH( table.date_field)
Different databases like oracle , postrest change the name of this functions , so you must find it
TSQL
select * from tbl
where MONTH(date)=month(getdate())
You'll want to match the YEAR along with the MONTH if you will have data that spans more than a year.
SELECT date, value
FROM table
WHERE YEAR(date) = YEAR(GETDATE()) AND MONTH(date) = MONTH(GETDATE())
I need a simple sql script where i can grab the rows with a date between sunday 2 weeks ago and saturday the previous week.
I need the query to return the elements no matter what day of this week I run the query.
Lets say I run the query today: Thursday 12. dec 2016. (12-08-2016)
I need to get this interval:
SELECT * FROM table WHERE date BETWEEN '11-27-2016' AND '12-03-2016'
DECLARE #StartInterval DATE,
#EndInterval DATE,
#Today = GETDATE()
SET #EndInterval = DATEADD(dd,-1,DATEADD(dd,-1*(DATEPART(dw,#Today)-1),#Today))
SET #StartInterval = DATEADD(dd,-6,#EndInterval)
SELECT *
FROM table
WHERE date BETWEEN #StartInterval AND #EndInterval
You can use:
select * from table where date between next_day(date, 'SUN')-21 AND
next_day(date, 'SAT')-14
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.
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.
You can now use date_format (Hive 1.2):
hive> select date_format('2016-12-01' ,'u');
OK
4
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)
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
From Hive 2.2 there is another possibility:
hive> select extract(dayofweek FROM your_date) FROM your_table;
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...!!!
I am looking for function that give me the right week-number.
The first day of the year should always be on week number 1.
The first saturday of a year belongs to the first week.
In another words: here is what i am looking for, but not in PostgreSQL, but in sql-server.
I realy want to do that using a function that i will call later.
Thank you in advance for your help.
In SQL Server there's no need for a custom function; just use the built in DATEPART function with week as the first argument:
-- first day
select datepart(week, '2013-01-01');
Output: 1
-- first Saturday
select datepart(week, '2013-01-05');
Output: 1
-- first Sunday
select datepart(week, '2013-01-06');
Output: 2
-- today
select datepart(week, '2013-04-01');
Output: 14
Here's a live demo: http://www.sqlfiddle.com/#!3/d41d8/11802
select datepart(week, getdate()) as CurrentWeekofYear
This should be doable, but how can I extract the day of the week from a field containing data in date format with Netezza SQL? I can write the following query:
SELECT date_part('day',a.report_dt) as report_dt
FROM table as a
but that gives me the day of the month.
thanks for any help
The below queries give day numbers for any week,month,year for a particular date.
--Day of Week
SELECT EXTRACT(dow FROM report_dt) FROM table;
--Day of Month
SELECT DATE_PART('day', report_dt) FROM table;
--Day of Year
SELECT EXTRACT(doy FROM report_dt) FROM table;
Netezza is just ANSI SQL, originally derived from PostgreSQL. I'd expect this to work.
select extract(dow from a.report_dt) as report_dt
from table as a
Returns values should range from 0 to 6; 0 is Sunday. You might expect that to be an integer, but in PostgreSQL at least, the returned value is a double-precision floating point.
If you want to extract directly the day name :
Select to_char(date, 'Day') as Day_Name From table;
In Netezza SQL, SELECT EXTRACT(dow FROM report_dt) would return values 1 to 7. 1 is Sunday, 7 is Saturday.