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!

if statement MS SQL Newbie question

Status
Not open for further replies.

bullyboy69

Technical User
Mar 12, 2003
9
GB
Morning

Have a question. I have a webpage that gets generated with fields from a SQL database. I have 2 fields, firstname and lastname and use another field name that contains both called name. I want to write an if statement so that if the firstname is null then just the lastname is the name eg.

if firstname is null or = '' then
name = lastname
else
name = firstname + lastname

How do I do this in SQL as its proving to be a right pain!

Thanks
Nick
 
Don't use if, use CASE

ie

CASE
WHEN ISNULL(firstname,'') = '' THEN lastname
ELSE
firstname + lastname
END Name

Hope this helps
 
try
Code:
SELECT ISNULL(firstname+' ','')+LastName AS Name

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
MVP VFP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top