﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / SQL Server Newbies  / Check Constraint with Case Statement / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Mon, 20 May 2013 20:30:05 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Check Constraint with Case Statement</title><link>http://www.sqlservercentral.com/Forums/Topic964790-1292-1.aspx</link><description>&amp;gt;&amp;gt; I need to check the column and if the value entered is 1 digit, I need to make it two digits like 01. If the value is two digit already, just leave it alone. &amp;lt;&amp;lt;We have no idea what your column looks like, since you did not bother with DDL. You are still thinking of procedural code and do not know that DDl is declarative. Use string functions to constrain string data elements. Here is an way to keep out the bad data:CREATE TABLE Foobar(.. vague_column CHAR(2) NOT NULL    CHECK (vague_column LIKE '[0-9][0-9]'), ..);&amp;gt;&amp;gt; I am using check constraint to accomplish this. But this is not working..Any help would be appreciated.CONSTRAINT CheckColLength  CHECK (CASE WHEN LEN(col) &amp;lt; 2              THEN '0' + col              ELSE col END = 1))Since this is a fixed length attribute, it should be in a fixed length column. From your narrative, CHAR(2) NOT NULL is the best guess. You then use the CASE expression (not CASE statement) to return a string, which you compare to an integer. That makes no sense. </description><pubDate>Fri, 06 Aug 2010 12:27:35 GMT</pubDate><dc:creator>CELKO</dc:creator></item><item><title>RE: Check Constraint with Case Statement</title><link>http://www.sqlservercentral.com/Forums/Topic964790-1292-1.aspx</link><description>HiYou can't use a check constraint to alter values - it can only check the values and generate an error if they are incorrect.You can however use a trigger to do what you want to do. You'd want a FOR UPDATE, INSERT trigger which checks the column value and changes it if necessary. Have a look at trigger syntax and get back if you have any problems.There may of course be other ways of doing this, so let's see what else people come up with!Duncan</description><pubDate>Fri, 06 Aug 2010 02:58:48 GMT</pubDate><dc:creator>Duncan Pryde</dc:creator></item><item><title>Check Constraint with Case Statement</title><link>http://www.sqlservercentral.com/Forums/Topic964790-1292-1.aspx</link><description>I need to check the column and if the value entered is 1 digit, I need to make it two digits like 01. If the value is two digit already, just leave it alone.I am using check constraint to accomplish this. But this is not working..Any help would be appreciated.ALTER TABLE [dbo].TableName  WITH CHECK ADD  CONSTRAINT [CheckColLength]  CHECK  (case when len(col) &amp;lt; 2 THEN '0' + col else col end=1))GOCan we use Case statement in check constraint?Thanks for your help.</description><pubDate>Thu, 05 Aug 2010 19:12:36 GMT</pubDate><dc:creator>SqlSavvy</dc:creator></item></channel></rss>