Solved

Returning extra value with SQL SELECT

Posted on 2004-09-12
3
336 Views
Last Modified: 2012-08-14
I want to return an extra column with an SQL Statement called "Live" which is dependent on if ALL the following are true:

- I want to return "Live" as 1 IF column ExpiryDate > Today's date
- I want to return "Live" as 1 IF column CommenceByDate >= Today's date
- I want to return "Live" as 1 IF column AvailableOnWeb = 1

Here's the SPROC as it stands (many rows will be returned):

CREATE PROCEDURE dbo.pGetMortgageProducts

...

SELECT
      ProductUID As [Product Identifier],
      [Name] As [Product Name],
      ExpiryDate As [Expiry Date],
      LinkURL = '../ProductDetails.aspx?MRID={' + CAST(ProductUID AS varchar(80)) + '}',
      FROM tProduct
GO
0
Comment
Question by:SurfingHamster
3 Comments
 
LVL 17

Accepted Solution

by:
BillAn1 earned 50 total points
ID: 12039947
You use the CASE statment for this. To get today's date, you need to truncate getdate() to get rid of hte hrs/minutes . There are many ways to do this - incl convert to 10 char string, and back to datetime, or you could convert to integer and back to datetime .....

SELECT
     ProductUID As [Product Identifier],
     [Name] As [Product Name],
     ExpiryDate As [Expiry Date],
     LinkURL = '../ProductDetails.aspx?MRID={' + CAST(ProductUID AS varchar(80)) + '}',
     CASE WHEN  ExpiryDate > convert(datetime, convert(varchar(10),getdate(),120) ,120) AND CommenceByDate >= convert(datetime, convert(varchar(10),getdate(),120) ,120) AND AvailableOnWeb = 1 THEN 1 ELSE 0 END AS [Live]

     FROM tProduct
GO
0

Featured Post

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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
Viewers will learn how the fundamental information of how to create a table.

863 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

19 Experts available now in Live!

Get 1:1 Help Now