Solved

Logical expressions in sql that will return a bit value??

Posted on 2004-09-08
2
168 Views
Last Modified: 2012-06-27
I have the following table:

CREATE TABLE [dbo].[tblGiftCertificates] (
      [id] [int] IDENTITY (1, 1) NOT NULL ,
      [publishDate] [smalldatetime] NOT NULL ,
      [expiryDate] [smalldatetime] NOT NULL ,
      [name] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
      [description] [varchar] (4000) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
      [price] [money] NOT NULL
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[tblGiftCertificates] ADD
      CONSTRAINT [DF_tblGiftCertificates_publishDate] DEFAULT (getdate()) FOR [publishDate],
      CONSTRAINT [DF_tblGiftCertificates_expiryDate] DEFAULT (getdate() + 1) FOR [expiryDate],
      CONSTRAINT [PK_tblGiftCertificates] PRIMARY KEY  CLUSTERED
      (
            [id]
      )  ON [PRIMARY]
GO

-------------------------------------

Is there any way to return a bit value in sql that will represent whether the expiryDate has passed.
Something like:

  isExpired = (expiryDate > GETDATE() )

Cheers.
0
Comment
Question by:smacca
2 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 12013565
Hi

I believe you can use DateDiff for that. Something like

DateDiff('d',expiryDate,getDate()) > 0 AS isExpired        //Tests if not expired

However, the only way I know to return a boolean is to do a SELECT CASE statement with the DateDiff being the case
and then check if any values are returned. If they are, then voila, good for you, if not, then it's expired, boo

Regards,
Zyloch
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 total points
ID: 12013920
You have to cast the expression result to bit data type:

isExpired = CAST(expiryDate > GETDATE()  AS BIT)

if that doesn't give the expected values, you should do like this:
isExpired = CAST( CASE WHEN expiryDate > GETDATE() THEN 1 ELSE 0 END  AS BIT )

CHeers
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

785 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