Solved

Returning extra value with SQL SELECT

Posted on 2004-09-12
3
342 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
[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
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

622 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