Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conditionally select and format one of two columns

Status
Not open for further replies.

PeDa

Technical User
Oct 10, 2002
227
NL
In SQL Server 2005 R2, I want to select:
IF column_1 IS NULL THEN MyUserDefinedFunction_2(column_2) ELSE MyUserDefinedFunction_1(column_1)
Any suggestions on how to achieve this would be greatly appreciated.

 
There are several types of user defined functions based on the data they return. If both functions are "Scalar Valued Functions", then you should be able to do this:

Code:
Select  ColumnA,
        ColumnB,
        Case When Column_1 Is NULL
             Then dbo.MyUserDefinedFunction_2(column_2)
             Else dbo.MyUserDefinedFunction_1(column_1)
             End As ColumnNameAlias
From    YourTableNameHere


-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Thank you both. This does indeed work:
Code:
SELECT 
    CASE WHEN dbo.tbArcStudies.gStudienr IS NULL 
        THEN 'p'+dbo.udf_FormatProjectNumber(dbo.tbArcStudies.gProjnr) 
        ELSE dbo.udf_FormatStudyNumber(dbo.tbArcStudies.gStudienr)
    END
FROM dbo.tbArcStudies
Peter D.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top