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!

IIF in SQL Server

Status
Not open for further replies.

rodeomount

Programmer
Sep 25, 2007
50
US


How can I translate the following to its equivalent in TSQL?

Code:
IIF(CONVERT(age, int) < 18 AND ltrim(rtrim(age)) <> '', CONVERT(1, SQL_BIT), CONVERT(0, SQL_BIT)) AS IsJuvenile
 
look at the CASE Statement in Books online

"NOTHING is more important in a database than integrity." ESquared
 
[tt][blue]
Case When CONVERT(int, age) < 18
Then CONVERT(Bit, 1)
Else CONVERT(Bit, 0)
End AS IsJuvenile
[/blue][/tt]

You don't need the additional check for empty string because SQL Server converts an empty string to 0.

See...

Select Convert(int, '')

You will need to be a little careful about strings that can't be converted to int, like....

Select Convert(int, 'Older than dirt.')
[red][tt]Syntax error converting the varchar value 'Old enough to know better' to a column of data type int.[/tt][/red]

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top