Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

747 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

13 Experts available now in Live!

Get 1:1 Help Now