Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 344
  • Last Modified:

Returning extra value with SQL SELECT

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
SurfingHamster
Asked:
SurfingHamster
1 Solution
 
BillAn1Commented:
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

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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now