locked
order by conditions, alphabet RRS feed

  • Question

  • User932259438 posted

    Hi.

    I have A,B,C,D,E,F,G,H

    How can I used order by first D, and then like that: A,B,C,E,F,G,H

    First row is always D then order by alphabet?

    Please help

    Friday, August 5, 2016 10:11 AM

Answers

  • User-2017229834 posted

    CREATE TABLE [dbo].[ABC](
    [Name] [varchar](50) NULL
    ) ON [PRIMARY]

    INSERT INTO ABC(Name) VALUES('A')
    INSERT INTO ABC(Name) VALUES('B')
    INSERT INTO ABC(Name) VALUES('C')
    INSERT INTO ABC(Name) VALUES('D')
    INSERT INTO ABC(Name) VALUES('E')
    INSERT INTO ABC(Name) VALUES('F')
    INSERT INTO ABC(Name) VALUES('G')
    INSERT INTO ABC(Name) VALUES('H')

    if OBJECT_ID('tempdb..#abc') is not null
    drop table #abc

    select Name into #abc from ABC where Name like 'D%' order by Name
    insert into #abc
    select Name from ABC where Name not like 'D%' order by Name

    select Name  from #abc

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 5, 2016 1:01 PM
  • User77042963 posted

    select col from yourtable

    order by Case when col ='D'   then 0 else 1 end, col

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 5, 2016 1:45 PM
  • User-2057865890 posted

    Hi progy85,

    You could use the CASE expression in an ORDER BY clause to conditionally determine the sort order of the rows based on a given column value.

    Specifying a conditional order

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, August 6, 2016 11:58 AM

All replies

  • User-2017229834 posted

    CREATE TABLE [dbo].[ABC](
    [Name] [varchar](50) NULL
    ) ON [PRIMARY]

    INSERT INTO ABC(Name) VALUES('A')
    INSERT INTO ABC(Name) VALUES('B')
    INSERT INTO ABC(Name) VALUES('C')
    INSERT INTO ABC(Name) VALUES('D')
    INSERT INTO ABC(Name) VALUES('E')
    INSERT INTO ABC(Name) VALUES('F')
    INSERT INTO ABC(Name) VALUES('G')
    INSERT INTO ABC(Name) VALUES('H')

    if OBJECT_ID('tempdb..#abc') is not null
    drop table #abc

    select Name into #abc from ABC where Name like 'D%' order by Name
    insert into #abc
    select Name from ABC where Name not like 'D%' order by Name

    select Name  from #abc

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 5, 2016 1:01 PM
  • User77042963 posted

    select col from yourtable

    order by Case when col ='D'   then 0 else 1 end, col

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 5, 2016 1:45 PM
  • User-2057865890 posted

    Hi progy85,

    You could use the CASE expression in an ORDER BY clause to conditionally determine the sort order of the rows based on a given column value.

    Specifying a conditional order

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, August 6, 2016 11:58 AM