?
Solved

Need help with SQL query

Posted on 2007-11-28
7
Medium Priority
?
202 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

 
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 2000 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

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

762 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