How to do order by on alias column?

  • How to do order by on alias column?

    Select customerID,FirstName +'_'+ lastname as customerName from tblemployee.

    Here i want to do order by customername descending

    and also do the filteration by 'John%' on customername

  • Select customerID,FirstName +'_'+ lastname as customerName from tblemployee

    order by FirstName +'_'+ lastname desc

  • select id,firstname + ' ' + surname as custName from tblemployee

    order by custName desc

    works for me

  • prashant-507002 (10/24/2011)


    How to do order by on alias column?

    Select customerID,FirstName +'_'+ lastname as customerName from tblemployee.

    Here i want to do order by customername descending

    One more way is to specify the alias that was givven to the new column:

    select CustomerID, FirstName + '_' + lastname as CustomerName

    from tblemployee

    order by CustomerName

    Adi

    --------------------------------------------------------------
    To know how to ask questions and increase the chances of getting asnwers:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • [font="Tahoma"]

    I would like to add one more information to the others responses. You cannot use a column alias in the where condition.

    So you can use the firstname in the where condition to pull the records starting with John.

    select CustomerID, FirstName + '_' + lastname as CustomerName

    from tblemployee

    where FirstName like 'John%'

    order by CustomerName

    [/font]

  • or

    select * from (

    select firstname + ' ' + surname as name from tblEmployee

    )y

    where y.name like '%john%'

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

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