Solved

Simple Sql Conditional Select

Posted on 2004-09-04
3
864 Views
Last Modified: 2012-05-05
I have a table of the following type:

int Linkid
int LinkType
datetime LinkChangeddate
datetime LinkCheckeddate

How can I write a stored procedure which selects rows based on LinkType's Value.

For each row, it is returned if:

a. If Linktype = 1 and Today-LinkCheckeddate > 3 days
b. If Linktype = 2 and Today-LinkCheckeddate > 7 days
b. If Linktype = 3 and LinkChangeddate>LinkCheckeddate

Thanks!

L.Agmon.
0
Comment
Question by:l_agmon
[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
  • 2
3 Comments
 
LVL 15

Expert Comment

by:jdlambert1
ID: 11982179
You'll need to change the table name:

CREATE PROCEDURE up_LinkCheckDate @LinkType tinyint AS

IF @LinkType = 1 OR @LinkType = 2
 SELECT Linkid, LinkType, LinkChangeddate, LinkCheckeddate
 FROM LinkTable
 WHERE DateDiff(d,GetDate(),LinkCheckeddate) > CASE WHEN @LinkType = 1 THEN 3 ELSE 7 END
ELSE
 SELECT Linkid, LinkType, LinkChangeddate, LinkCheckeddate
 FROM LinkTable
 WHERE LinkChangeddate > LinkCheckeddate


--sample to use the procedure
--EXECUTE up_link 1
0
 
LVL 15

Expert Comment

by:jdlambert1
ID: 11982237
Oops. Error on my sample to execute it. Instead use:

EXEC up_LinkCheckDate 1
EXEC up_LinkCheckDate 2
or
EXEC up_LinkCheckDate 3
0
 
LVL 3

Accepted Solution

by:
alexpreston earned 50 total points
ID: 11983250
Surely you want to return them all at once, so don't want to pass the LinkType as a parameter?

CREATE PROCEDURE spSomething AS

SELECT
      Linkid, LinkType, LinkChangeddate, LinkCheckeddate
FROM
      [tablename]
WHERE
      ( LinkType = 1 AND DATEDIFF(d,LinkCheckedDate,getdate())>3 )
      OR ( LinkType = 2 AND DATEDIFF(d,LinkCheckedDate,getdate())>7 )
      OR ( LinkType = 3 AND LinkChangedDate > LinkCheckedDate )

GO
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

690 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