home
training courses
why choose us?
solutions
support
company
LearnItFirst User Forum
Welcome Guest
Search
|
New Posts
|
Members
|
Log In
|
Register
SQL Server Forum - LearnItFirst.com
»
Transact-SQL Programming
»
Stored Procedures, Triggers, & Functions
»
results based on column value
results based on column value
Options
Previous Topic
·
Next Topic
mguser
Posted:
Tuesday, March 18, 2008 4:53:17 PM
Rank: Newbie
Joined: 3/18/2008
Posts: 2
Points: -144
I have a stored procedure which selects results based on some date calculations.
In my table I have a status column and two date fields (Approval Date and Signature Date)
If the value in the status column says approved I want to select results where approval date - signature date is less than a certain number of days.
If the status is naything other than approved i want to select results where sysdate - signature date is less than the given number of days.
How can i achieve this?
Back to top
Scott Whigham
Posted:
Thursday, March 20, 2008 1:25:40 PM
Rank: Super Mod
Joined: 3/20/2006
Posts: 476
Points: 1,053
Where do you live?: Dallas, TX
Hi there - I deleted your cross post so that we could talk about this in just one location.
You need to use the DATEDIFF() function with CASE. Something like
Code:
DECLARE @NumberOfDays INT
SET @NumberOfDays = 30
SELECT *
FROM MyTable
WHERE @NumberOfDays <=
CASE
WHEN Status='Approved' THEN DATEDIFF(dd, ApprovalDate, SignatureDate)
ELSE DATEDIFF(dd, GETDATE(), SignatureDate)
ENDp
That's off the top of my head but it should be close-enough-to-right so that you could find the right answer.
Back to top
Users browsing this topic
Guest
Forum Jump
SQL Server Database Administration
- General SQL Database Question & Answer
- Backup, Recovery and Disaster Recovery
- SQL Server Security
- Integration Services (SSIS) and DTS
Transact-SQL Programming
- DML (SELECT, INSERT, UPDATE, DELETE) Questions
- Stored Procedures, Triggers, & Functions
SQL Server Scripts, Code Samples and SSMS Custom Reports
- All SQL Server Versions
- SQL Server 2005/2008
- SQL Server Management Studio Custom Reports
Customer Service
- Video Requests
You
cannot
post new topics in this forum.
You
cannot
reply to topics in this forum.
You
cannot
delete your posts in this forum.
You
cannot
edit your posts in this forum.
You
cannot
create polls in this forum.
You
cannot
vote in polls in this forum.
SQL Server 2005 DBA Training Videos
SQL Server 2008 DBA Training Videos
Email this topic
RSS Feed
Watch this topic
Print this topic
Normal
Threaded