Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Select JOIN table 2 22
SQL Login 17 40
What is format f12.8 for a CSV file 6 41
MS SQL Server - Looking to filter rows based on column value 3 6
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.

920 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

17 Experts available now in Live!

Get 1:1 Help Now