SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseSQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old November 3rd, 2009, 07:00 AM
iand109 iand109 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 16 iand109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 8 m 45 sec
Reputation Power: 0
Query - General - Using IF in calculation statement

Hi,
I have a statement that is calculating days that an employee has been working for the company. This is the statement so far:

Code:
(SELECT     SUM(DATEDIFF(dd, EmpStart, EmpEnd)) AS Expr1
                            FROM          dbo.Employment AS Employment_2
                            WHERE      (dbo.STAFF.AppNo = AppNo)
                            GROUP BY AppNo) AS TimeServed


The bit I need help with is the DATEDIFF bit. It calculates OK if there is an end date. But if end date is null, OR end date is > today, it should use today's date for the calculation.
So I tried something like this:


Code:
SUM(DATEDIFF(dd, EmpStart, (IF EmpEnd IS NULL OR EmpEnd > GetDate() THEN GetDate() ELSE EmpEnd)))


But it didn't work - please can anyone help?
Many thanks

Reply With Quote
  #2  
Old November 3rd, 2009, 07:03 AM
sync_or_swim's Avatar
sync_or_swim sync_or_swim is offline
Moderator
Click here for more information.
 
Join Date: Mar 2006
Location: South Wales
Posts: 3,461 sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 1 Day 16 h 50 m 26 sec
Reputation Power: 1806
Hi,

This is not tested but you should be able to use ISNull to substitute the current date if the enddate is null:
Code:
SELECT     SUM(DATEDIFF(dd, EmpStart, ISNULL(EmpEnd, getDate()))) AS Expr1
                            FROM          dbo.Employment AS Employment_2
                            WHERE      (dbo.STAFF.AppNo = AppNo)
                            GROUP BY AppNo) AS TimeServed

Reply With Quote
  #3  
Old November 3rd, 2009, 07:23 AM
iand109 iand109 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 16 iand109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 8 m 45 sec
Reputation Power: 0
Quote:
Originally Posted by sync_or_swim
This is not tested but you should be able to use ISNull to substitute the current date if the enddate is null:
Code:
SELECT     SUM(DATEDIFF(dd, EmpStart, ISNULL(EmpEnd, getDate()))) AS Expr1
                            FROM          dbo.Employment AS Employment_2
                            WHERE      (dbo.STAFF.AppNo = AppNo)
                            GROUP BY AppNo) AS TimeServed


Yes that did work, you're right. But I also need a bit of conditional code which says that if EmpEnd > Today, then use today's date; sometimes people have given notice, which creates an end date in the future. How could I include that?
Many thanks indeed

Reply With Quote
  #4  
Old November 3rd, 2009, 07:38 AM
sync_or_swim's Avatar
sync_or_swim sync_or_swim is offline
Moderator
Click here for more information.
 
Join Date: Mar 2006
Location: South Wales
Posts: 3,461 sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 1 Day 16 h 50 m 26 sec
Reputation Power: 1806
You should be able to encorporate a case statement, not tested:
Code:
SELECT     SUM(DATEDIFF(dd, EmpStart, (CASE WHEN DATEDIFF(day, getDate(), EmpEnd) >0 THEN
getDate() WHEN EmpEnd IS NULL THEN getDate() ELSE EmpEnd END)  )) AS Expr1
                            FROM          dbo.Employment AS Employment_2
                            WHERE      (dbo.STAFF.AppNo = AppNo)
                            GROUP BY AppNo) AS TimeServed

Reply With Quote
  #5  
Old November 3rd, 2009, 07:48 AM
iand109 iand109 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 16 iand109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 8 m 45 sec
Reputation Power: 0
Quote:
Originally Posted by sync_or_swim
You should be able to encorporate a case statement, not tested:
Code:
SELECT     SUM(DATEDIFF(dd, EmpStart, (CASE WHEN DATEDIFF(day, getDate(), EmpEnd) >0 THEN
getDate() WHEN EmpEnd IS NULL THEN getDate() ELSE EmpEnd END)  )) AS Expr1
                            FROM          dbo.Employment AS Employment_2
                            WHERE      (dbo.STAFF.AppNo = AppNo)
                            GROUP BY AppNo) AS TimeServed


Oh yes yes, it works, you're a total star. I did try a case statement but got it totally wrong. Many thanks indeed!

Reply With Quote
  #6  
Old November 3rd, 2009, 08:00 AM
sync_or_swim's Avatar
sync_or_swim sync_or_swim is offline
Moderator
Click here for more information.
 
Join Date: Mar 2006
Location: South Wales
Posts: 3,461 sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level)sync_or_swim User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 1 Day 16 h 50 m 26 sec
Reputation Power: 1806
Quote:
Originally Posted by iand109
Oh yes yes, it works, you're a total star. I did try a case statement but got it totally wrong. Many thanks indeed!

You're welcome, I'm glad it helped!!

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseSQL Development > Query - General - Using IF in calculation statement


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump





 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek