Solved

Returning extra value with SQL SELECT

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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

705 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

18 Experts available now in Live!

Get 1:1 Help Now