How do I exclude Weekend days in a SQL Server query? - sql-server

How do I exclude values in a DateTime column that are Saturdays or Sundays?
For example, given the following data:
date_created
'2009-11-26 09:00:00' -- Thursday
'2009-11-27 09:00:00' -- Friday
'2009-11-28 09:00:00' -- Saturday
'2009-11-29 09:00:00' -- Sunday
'2009-11-30 09:00:00' -- Monday
this is the result I'm looking for:
date_created
'2009-11-26 09:00:00' -- Thursday
'2009-11-27 09:00:00' -- Friday
'2009-11-30 09:00:00' -- Monday
Thanks!

When dealing with day-of-week calculations, it's important to take account of the current DATEFIRST settings. This query will always correctly exclude weekend days, using ##DATEFIRST to account for any possible setting for the first day of the week.
SELECT *
FROM your_table
WHERE ((DATEPART(dw, date_created) + ##DATEFIRST) % 7) NOT IN (0, 1)

SELECT date_created
FROM your_table
WHERE DATENAME(dw, date_created) NOT IN ('Saturday', 'Sunday')

Assuming you're using SQL Server, use DATEPART with dw:
SELECT date_created
FROM your_table
WHERE DATEPART(dw, date_created) NOT IN (1, 7);
EDIT: I should point out that the actual numeric value returned by DATEPART(dw) is determined by the value set by using SET DATEFIRST:
http://msdn.microsoft.com/en-us/library/ms181598.aspx

Try the DATENAME() function:
select [date_created]
from table
where DATENAME(WEEKDAY, [date_created]) <> 'Saturday'
and DATENAME(WEEKDAY, [date_created]) <> 'Sunday'

The answer depends on your server's week-start set up, so it's either
SELECT [date_created] FROM table WHERE DATEPART(w,[date_created]) NOT IN (7,1)
if Sunday is the first day of the week for your server
or
SELECT [date_created] FROM table WHERE DATEPART(w,[date_created]) NOT IN (6,7)
if Monday is the first day of the week for your server
Comment if you've got any questions :-)

Calculate Leave working days in a table column as a default value--updated
If you are using SQL here is the query which can help you: http://gallery.technet.microsoft.com/Calculate...

Try this code
select (DATEDIFF(DD,'2014-08-01','2014-08-14')+1)- (DATEDIFF(WK,'2014-08-01','2014-08-14')* 2)

Related

T-SQL: select date range from last Friday at 1230pm until Monday at 330am

I'm creating a query in SQL Server 2008, for an SSRS report that runs every Monday at 0330am, retrieving weekend sales details.
The report is ready, but I had to hard-code the date range, as I'm having trouble expressing that condition in the WHERE statement.
I need to retrieve data based on column [salestime] (of type datetime), between Friday at 1230pm and Monday at 330am.
I'd really appreciate your assistance with this.
How about something like:
WHERE SalesTime BETWEEN DATEADD(HH,-63,GETDATE()) AND GETDATE()
I believe the time values are 63 hours apart. This is if your report automatically runs at 3:30 AM, which is what it sounds like in your post.
If you want to run your report any time during the week to report on last weekend's sale
SET DATEFIRST 7
DECLARE #ThisMonday date = DATEADD(DAY, 2 - DATEPART(WEEKDAY, GETDATE()), GETDATE())
DECLARE #LastFriday date = DATEADD(DAY, -3, #ThisMonday)
DECLARE #StartTime datetime = CAST(#LastFriday AS datetime) + CAST('12:30' AS datetime)
DECLARE #EndTime datetime = CAST(#ThisMonday AS datetime) + CAST('03:30' AS datetime)
SELECT #ThisMonday, #LastFriday, #StartTime, #EndTime
Now you can filter your report with salestime BETWEEN #StartTime AND #EndTime.

How to retrieve all rows with a date in this and the next week? SQL Server

I have a Tasks table that contains Date, Description and WorkerID columns. I want to write a stored procedure that selects all tasks from this week and the following week. Can someone help me get to a solution please? I am not so good at this.
If indexing (performance) isn't a concern then it's a simple expression if your week coincides with the SQL Server setup:
where datediff(week, getdate(), [Date]) between 0 and 1
The snippet below is another option that works with both dates and datetimes. It's possible to adapt it to work with any ##datefirst setting but I'm just going to assume that the setting matches with the start of week you're looking to report on.
where
[Date] >= dateadd(day, 1 - datepart(weekday, getdate()), cast(getdate() as date))
and [Date] < dateadd(day, 15 - datepart(weekday, getdate()), cast(getdate() as date))
The following should give you what you want (assuming Monday is the first day of the week, and that the DBMS is SQL Server):-
select *
from Tasks
where [Date]
between
dateadd(dd,-(datepart(dw,[Date])-2),[Date]) --Monday of this week
and
dateadd(dd,13,dateadd(dd,-(datepart(dw,[Date])-2),[Date])) --Sunday of next week

Understanding SQL Server code, in order to translate it

I have the following SQL Server code, that I need to translate to PL/SQL.
I'm trying to understand what this code does, can someone help me?
--STEP 0a:
Declare prior MONDAY and prior SUNDAY
DECLARE #ENDINGSUNDAY DATE
SET #ENDINGSUNDAY = --'2013-06-23' --PUT ENDING DATE FOR WEEK HERE(the sunday ending the week)
--/*
(SELECT MAX(DATE)
FROM DIM.DATE
WHERE DAY_OF_WEEK_DESC LIKE 'SUNDAY' AND DATE <=CAST(GETDATE()) AS DATE))
SELECT MAX(DATE) FROM DIM.DATE WHERE DAY_OV_WEEK_DESC LIKE 'SUNDAY' AND DATA <= SYSDATE

How to get one month ago from today in SQL Server 2008?

I´m writing a query where i get the last month, but with the time in zeros (if today is 2013-05-21 then i want to get 2013-04-21 00:00:00.000).
So I tried:
select (dateadd(month,datediff(month,(0),getdate())-1,(0)));
But I get the first day of the previous month.
Then I tried:
select dateadd(month, -1, GETDATE());
I get the right day, but I also get the current time (2013-04-21 11:41:31.090), and I want the time in zeros.
So how should my query be in order to get something like: 2013-04-21 00:00:00.000
Thanks in advance.
In SQL Server 2008 there is the date data type, which has no time attached. You can thus remove the time portion quite easily simply by converting, then performing the DateAdd.
SELECT DateAdd(month, -1, Convert(date, GetDate()));
This will return a date data type. To force it to be datetime again, you can simply add one more Convert:
SELECT Convert(datetime, DateAdd(month, -1, Convert(date, GetDate())));
You may not need the explicit conversion to datetime, though.
Note: "One month ago from today" could be defined in many different ways. The way it works in SQL server is to return the day from the previous month that is the closest to the same day number as the current month. This means that the result of this expression when run on March 31 will be February 28. So, you may not get expected results in certain scenarios if you don't think clearly about the ramifications of this, such as if you performed the one-month calculation multiple times, expecting to get the same day in a different month (such as doing March -> February -> January).
See a live demo at SQL Fiddle
The demo shows the values and resulting data types of each expression.
Try like this..
select Cast(Cast(dateadd(month, -1, GETDATE()) as Date) as Datetime);
You can use this , it's pretty simple and worked for me -
SELECT SUM( amount ) AS total FROM expenses WHERE MONTH( date ) = MONTH( curdate() ) -1
To get the previous month start date and end date
DECLARE #StartDate date;
DECLARE #EndDate date;
select #StartDate= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0)
select #EndDate= DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, -1)
Here are many common dates you may need to pull with logic
SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0) -- Today Midnight
SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE()),-1) -- Yesterday Midnight
SELECT DATEADD(d,0,DATEADD(mm, DATEDIFF(m,0,GETDATE())-1,0)) -- First of Last Month
SELECT DATEADD(d,DATEPART(DD,GETDATE()-1),DATEADD(mm, DATEDIFF(m,0,GETDATE())-1,0)) -- Same Day Last Month
SELECT DATEADD(d,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)) -- Last of Last Month
SELECT DATEADD(d,0,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)) -- First of this month
SELECT DATEADD(d,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0)) -- Last of this month
SELECT DATEADD(d,0,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0)) -- First of next month
SELECT DATEADD(d,DATEPART(DD,GETDATE()-1),DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0)) -- Same Day Next Month
SELECT DATEADD(d,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+2,0)) -- Last of next month
SELECT DATEADD(d,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+12,0)) -- Last of prior month one year from now
SELECT DATEADD(dd,DATEDIFF(dd,0,DATEADD(DAY, 13-(##DATEFIRST + (DATEPART(WEEKDAY,GETDATE()) %7)), GETDATE())),0) -- Next Friday Midnight

How to select last three month all dates in SQL Server

I want to select all last 3 month dates from the table in SQL Server.
Data is like this:
Sunday 20-05-2012
Sunday 27-05-2012
Sunday 10-06-2012
Sunday 24-06-2012
Sunday 08-07-2012
Sunday 22-07-2012
Sunday 12-08-2012
Sunday 19-08-2012
Sunday 09-09-2012
Sunday 16-09-2012
Saving date column as varchar with day of week - bad idea. You can't write faster query, because you must convert all of time your field for using it in where clause. Also your query can't use indexes of date_column and each query will be use scan index.
With datetime column, query should be:
select date_column
from table_name
where date_column between dateadd(m, -3, getdate()) and getdate()
Try using this query:
select date_column from table_name
where datepart(m,date_column) > datepart(m,getdate())-3 and datepart(yy,date_column) >= datepart(yy,getdate())

Resources