Select statement with condition.

  • Hi Team,

    I have below query:

    SELECT lname +'.' +fname AS emp_name FROM employees

    Result : First_Name.Last_Name

    If any one (lname or fname is NULL, then result is showing as NULL)

    My requirement is :

    If lname is NULL then result should be fname (without dot(.))

    If fname is NULL then result should be lname (should not end with dot(.))

    Please help

  • Use a CASE expression.

    John

  • pls use below code...

    declare @t1 table(fname varchar(50),lname varchar(20))

    insert into @t1(fname,lname) values('sql','server')

    insert into @t1(fname) values('ravi')

    insert into @t1(lname) values('kumar')

    select *,case when (fname is not null) and (lname is not null) then fname+'.'+lname

    else isnull(fname,lname)

    end from @t1

  • Thank u Subba Reddy.

    May i know Where r u from.

  • Hi,

    I want result column name should be "Employee_Name"

    select *,case when (fname is not null) and (lname is not null) then fname+'.'+lname

    AS employee_Name

    else isnull(fname,lname)

    It is not working,

    Can u please help...

  • select *,case when (fname is not null) and (lname is not null) then fname+'.'+lname

    else isnull(fname,lname)

    end AS employee_Name

  • SELECT COALESCE(lname + '.' + fname, lname, fname) AS emp_name

    FROM employees

    The SQL Guy @ blogspot[/url]

    @SeanPearceSQL

    About Me[/url]

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply