SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Using the "IF" function in a column


Using the "IF" function in a column

Author
Message
jrichards54
jrichards54
Valued Member
Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)

Group: General Forum Members
Points: 73 Visits: 107
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 :-)
ChrisM@home
ChrisM@home
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2016 Visits: 10339
jrichards54 (10/28/2012)
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 :-)


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.


Low-hanging fruit picker and defender of the moggies





For better assistance in answering your questions, please read this.




Understanding and using APPLY, (I) and (II) Paul White

Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
jrichards54
jrichards54
Valued Member
Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)

Group: General Forum Members
Points: 73 Visits: 107
Thank you for your reply. I appreciate your insights and recommendations. Have a good day. JRichards54 :-)
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)

Group: General Forum Members
Points: 7177 Visits: 6431
ChrisM@home (10/28/2012)
jrichards54 (10/28/2012)
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 :-)


SQL Server only permits constants as default values. ...


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:


CREATE TABLE #Temp
(ID INT IDENTITY
,Value1 CHAR(1) DEFAULT ('A')
,Value2 CHAR(1) DEFAULT (CASE WHEN GETDATE() > '2012-10-29' THEN 'B' END))

INSERT INTO #Temp (Value1)
SELECT NULL UNION ALL SELECT 'B'

SELECT * FROM #Temp

DROP TABLE #Temp






My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Eric M Russell
Eric M Russell
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11966 Visits: 10622
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.


"The universe is complicated and for the most part beyond your control, but your life is only as complicated as you choose it to be."
jrichards54
jrichards54
Valued Member
Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)

Group: General Forum Members
Points: 73 Visits: 107
dwain.c (10/28/2012)
ChrisM@home (10/28/2012)
jrichards54 (10/28/2012)
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 :-)


SQL Server only permits constants as default values. ...


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:


CREATE TABLE #Temp
(ID INT IDENTITY
,Value1 CHAR(1) DEFAULT ('A')
,Value2 CHAR(1) DEFAULT (CASE WHEN GETDATE() > '2012-10-29' THEN 'B' END))

INSERT INTO #Temp (Value1)
SELECT NULL UNION ALL SELECT 'B'

SELECT * FROM #Temp

DROP TABLE #Temp





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 :-)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search