Show data in a-b format in sql server 2008 -
i using sql server 2008.
i have table has distinct numbers i.e int data type. need query shows data in increasing order in format cast(a varchar)+'-'cast(b varchar)
i.e a-b
a
smallest number has yet not been shown , b
subsequently next smallest number.
i know sql server 2012 has lead
function make question moot how do on sql server 2008?
create table #nr(nr int); insert #nr(nr)values(9),(7),(1),(2),(25),(33),(10),(3); select cast(n_o.nr varchar)+'-'+cast((select min(n_i.nr) #nr n_i n_i.nr>n_o.nr) varchar) #nr n_o (select min(n_i.nr) #nr n_i n_i.nr>n_o.nr) not null order nr; drop table #nr;
results in:
╔══════════════════╗ ║ (no column name) ║ ╠══════════════════╣ ║ 1-2 ║ ║ 2-3 ║ ║ 3-7 ║ ║ 7-9 ║ ║ 9-10 ║ ║ 10-25 ║ ║ 25-33 ║ ╚══════════════════╝
Comments
Post a Comment