﻿<?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 2005 / SQL Server 2005 General Discussion  / Using the "IF" function in a column / 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>Thu, 23 May 2013 17:18:56 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Using the "IF" function in a column</title><link>http://www.sqlservercentral.com/Forums/Topic1378036-149-1.aspx</link><description>[quote][b]dwain.c (10/28/2012)[/b][hr][quote][b]ChrisM@home (10/28/2012)[/b][hr][quote][b]jrichards54 (10/28/2012)[/b][hr]Is it possible to use the "IF" function in a column at design time as the value for that field? I do it regularly in Excel with no problem but I am new to SQL Server and have been unable to figure this one out.  I have been using 3 of my own books plus the Online Books but to no avail. If I cannot do it in design, can I create a script (or a stored proceedure) that will do it at run time? Thanks, JRichards54 :-)[/quote]SQL Server only permits constants as default values. ...[/quote]True but... you can construct an IF-like syntax using CASE as a default as long as you don't reference any column names in the CASE:[code="sql"]CREATE TABLE #Temp    (ID INT IDENTITY    ,Value1 CHAR(1) DEFAULT ('A')    ,Value2 CHAR(1) DEFAULT (CASE WHEN GETDATE() &amp;gt; '2012-10-29' THEN 'B' END))INSERT INTO #Temp (Value1)SELECT NULL UNION ALL SELECT 'B'SELECT * FROM #TempDROP TABLE #Temp[/code][/quote]You wrote above: "as long as you don't reference any column names in the CASE:"Thank you but this will not work for me as the VALUE in that column does change and that VALUE is necessary to accurately compute the result of the IF statement. The VALUE of that column (QtyInPkg) can vary from 14, 30, 60 , 90, 180, and 365 and these are all Integers and represent days. The "If" statement works just fine if I use the any of the constants but does not work if I use the column name (QtyInPkg). Thanks again and have a good day. JRichards54 :-)</description><pubDate>Tue, 06 Nov 2012 08:33:48 GMT</pubDate><dc:creator>jrichards54</dc:creator></item><item><title>RE: Using the "IF" function in a column</title><link>http://www.sqlservercentral.com/Forums/Topic1378036-149-1.aspx</link><description>In addition to expressions, you can reference user defined functions or non-deterministic functions for a column default. However, a default can't reference other columns in the table, you would need a computed column or view for that.</description><pubDate>Mon, 29 Oct 2012 08:28:40 GMT</pubDate><dc:creator>Eric M Russell</dc:creator></item><item><title>RE: Using the "IF" function in a column</title><link>http://www.sqlservercentral.com/Forums/Topic1378036-149-1.aspx</link><description>[quote][b]ChrisM@home (10/28/2012)[/b][hr][quote][b]jrichards54 (10/28/2012)[/b][hr]Is it possible to use the "IF" function in a column at design time as the value for that field? I do it regularly in Excel with no problem but I am new to SQL Server and have been unable to figure this one out.  I have been using 3 of my own books plus the Online Books but to no avail. If I cannot do it in design, can I create a script (or a stored proceedure) that will do it at run time? Thanks, JRichards54 :-)[/quote]SQL Server only permits constants as default values. ...[/quote]True but... you can construct an IF-like syntax using CASE as a default as long as you don't reference any column names in the CASE:[code="sql"]CREATE TABLE #Temp    (ID INT IDENTITY    ,Value1 CHAR(1) DEFAULT ('A')    ,Value2 CHAR(1) DEFAULT (CASE WHEN GETDATE() &amp;gt; '2012-10-29' THEN 'B' END))INSERT INTO #Temp (Value1)SELECT NULL UNION ALL SELECT 'B'SELECT * FROM #TempDROP TABLE #Temp[/code]</description><pubDate>Sun, 28 Oct 2012 21:12:47 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>RE: Using the "IF" function in a column</title><link>http://www.sqlservercentral.com/Forums/Topic1378036-149-1.aspx</link><description>Thank you for your reply. I appreciate your insights and recommendations. Have a good day. JRichards54 :-)</description><pubDate>Sun, 28 Oct 2012 11:22:23 GMT</pubDate><dc:creator>jrichards54</dc:creator></item><item><title>RE: Using the "IF" function in a column</title><link>http://www.sqlservercentral.com/Forums/Topic1378036-149-1.aspx</link><description>[quote][b]jrichards54 (10/28/2012)[/b][hr]Is it possible to use the "IF" function in a column at design time as the value for that field? I do it regularly in Excel with no problem but I am new to SQL Server and have been unable to figure this one out.  I have been using 3 of my own books plus the Online Books but to no avail. If I cannot do it in design, can I create a script (or a stored proceedure) that will do it at run time? Thanks, JRichards54 :-)[/quote]SQL Server only permits constants as default values. You could use a computed column to generate (and contain) a value for you, or a check constraint to ensure that an INSERTED/UPDATEd value matches your criteria. In practice? I'd put the logic into the INSERT script.</description><pubDate>Sun, 28 Oct 2012 10:48:36 GMT</pubDate><dc:creator>ChrisM@home</dc:creator></item><item><title>Using the "IF" function in a column</title><link>http://www.sqlservercentral.com/Forums/Topic1378036-149-1.aspx</link><description>Is it possible to use the "IF" function in a column at design time as the value for that field? I do it regularly in Excel with no problem but I am new to SQL Server and have been unable to figure this one out.  I have been using 3 of my own books plus the Online Books but to no avail. If I cannot do it in design, can I create a script (or a stored proceedure) that will do it at run time? Thanks, JRichards54 :-)</description><pubDate>Sun, 28 Oct 2012 10:23:11 GMT</pubDate><dc:creator>jrichards54</dc:creator></item></channel></rss>