Solved

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

Posted on 2004-09-08
2
170 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 143

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
I have a large data set and a SSIS package. How can I load this file in multi threading?
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…
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.

730 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