Solved

Need help with SQL query

Posted on 2007-11-28
7
168 Views
Last Modified: 2010-03-20
Hello,

I am trying to build a SQL query that only count working days, I am able to make count all 365 days, but I only need specified working days.

Thanks,
0
Comment
Question by:ametrade
7 Comments
 
LVL 8

Expert Comment

by:digital_thoughts
ID: 20367992
Try this:

SELECT
      DateVar
FROM
      TableName
WHERE
      DATEPART(DW, DateVar) BETWEEN 2 AND 6


DATEPART with the argument "DW" returns a numeric value for the day of the week, 1 being Sunday and 7 being Saturday...
0
 

Author Comment

by:ametrade
ID: 20368033
digital_thoughts I am testing it, will let you know, thx.
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20370245
to be more precise

SELECT DateVar
FROM TableName
WHERE ((DATEPART(DW, DateVar) + @@datefirst) % 7)  BETWEEN 2 AND 6

check BOL for @@datefirst
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 5

Expert Comment

by:ursangel
ID: 20372091
DATEPART(DW, DateVar) BETWEEN 2 AND 6
this alone will pick up the dates for all the working days, unless otherwise its a specific holiday.
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20372137
ametrade: it's worth your while reading up on DatePart and @@datefirst, if your solution is for more than one small customer in one specific country
0
 

Author Comment

by:ametrade
ID: 20373376
Hi imitchie sorry for not specifying that, this is for Venezuela, thank you.
0
 
LVL 25

Accepted Solution

by:
imitchie earned 500 total points
ID: 20376882
Hi ametrade,
What I mean is that DatePart depends on the @@datefirst setting, which can change depending on the OS regional/SQL Server setting.  using

WHERE ((DATEPART(DW, DateVar) + @@datefirst) % 7)  BETWEEN 2 AND 6

will make it work correctly for any setting and country
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
encyps queries mssql 15 37
Oracle - Stored Procedure Privilge access 3 38
How does this SELECT query work 11 99
Using OPENQUERY in a SELECT statement 6 31
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now