Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2005 > SQL Server 2005
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 2nd, 2007, 03:33 PM
SQLScott's Avatar
Wrox Author
 
Join Date: Dec 2004
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
========================
 
Old October 3rd, 2007, 04:05 AM
Friend of Wrox
 
Join Date: May 2006
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
 
Old October 3rd, 2007, 05:58 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Or use standard ISO datetime format?

[ColumnName] < '2000101'








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





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