Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2005 > SQL Server 2005
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 2nd, 2007, 03:33 PM
SQLScott's Avatar
Wrox Author
 
Join Date: Dec 2004
Location: Wellington, FL , USA.
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
Default Error adding a check constraint

I am trying to add a simple check constraint and for the life of me I cannot figure out why SQL isn't accepting it. All i want to do is something like the following:

[ColumnName] < '1/1/2000'

But SQL won't accept this. I have even tried many different variations, such as

([ColumnName] < CONVERT([smalldatetime], '1/1/2000', (0)))

What am I completely brain farting on with this?

Thanks...



Scott Klein
Author - Professional SQL Server 2005 XML
http://www.wrox.com/WileyCDA/WroxTit...764597922.html
__________________
========================
Scott Klein
Author of:
Professional SQL Server 2005 XML
Professional WCF Programming: .NET Development with the Windows Communication Foundation
Professional LINQ
========================
Reply With Quote
  #2 (permalink)  
Old October 3rd, 2007, 04:05 AM
Friend of Wrox
 
Join Date: May 2006
Location: Helsingborg, , Sweden.
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think you already have data in the column that violates the check contraint.
Run this example code statement by statement
Code:
-- Create temporary table
CREATE TABLE    #Sample
        (
            dt DATETIME
        )

-- Insert some valid data
INSERT    #Sample
SELECT    '20071003'

-- Verify table data
SELECT * FROM #Sample

-- Create a "valid" check constraint
ALTER TABLE #Sample ADD CHECK (dt < '20080101')

-- Try to insert some invalid data
INSERT    #Sample
SELECT    '20080229'

-- Verify table data
SELECT * FROM #Sample

-- Try to create an "invalid" check constraint
ALTER TABLE #Sample ADD CHECK (dt <= '20061231')

-- Try to force an "invalid" check constraint!
ALTER TABLE #Sample WITH NOCHECK ADD CHECK (dt <= '20061231') 

-- Verify table data
SELECT * FROM #Sample

-- Clean up
DROP TABLE #Sample
Reply With Quote
  #3 (permalink)  
Old October 3rd, 2007, 05:58 AM
Friend of Wrox
 
Join Date: May 2006
Location: Helsingborg, , Sweden.
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Or use standard ISO datetime format?

[ColumnName] < '2000101'



Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
You have an error in your SQL syntax; check the ma JimZippy BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 1 December 5th, 2007 12:34 PM
check box data insert error mateenmohd Classic ASP Basics 3 May 28th, 2007 05:29 AM
Adding check box in datagrid balesh ASP.NET 1.0 and 1.1 Basics 2 June 7th, 2006 03:30 AM
CHECK Constraint Error Kep SQL Server 2000 5 April 11th, 2006 08:46 PM
Check box Error surendran BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 2 July 25th, 2004 11:36 PM



All times are GMT -4. The time now is 12:37 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.