Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


ordering dynamic column string


ordering dynamic column string

Author
Message
spin
spin
SSC-Enthusiastic
SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)

Group: General Forum Members
Points: 142 Visits: 534
hi

i am trying to create a set of dynamic date columns (year - monthname). When i create the temp table of distinct dates and sort it's all fine until i then create the dynamic string in which case it sorts alphabetically.

i need the columns to be sorted by year, month but it's sorting by year, monthname. so the 'select @cols' is not sorting correctly.

here's some code to use as an example:


if object_id('tempdb..#dateTable') is not null
drop table #dateTable
if object_id('tempdb..#distinctDate') is not null
drop table #distinctDate

create table #dateTable(
Year int,
MonthNumber int,
MonthName Varchar(3)
)

insert into #dateTable
select 2013, 1, 'Jan' union
select 2013, 2, 'Feb' union
select 2013, 3, 'Mar' union
select 2013, 4, 'Apr' union
select 2013, 5, 'May' union
select 2013, 6, 'Jun' union
select 2013, 7, 'Jul' union
select 2013, 8, 'Aug' union
select 2013, 9, 'Sep' union
select 2013, 10, 'Oct' union
select 2013, 11, 'Nov' union
select 2013, 12, 'Dec' union
select 2014, 1, 'Jan' union
select 2014, 2, 'Feb' union
select 2014, 3, 'Mar' union
select 2014, 4, 'Apr' union
select 2014, 5, 'May' union
select 2014, 6, 'Jun' union
select 2014, 7, 'Jul' union
select 2014, 8, 'Aug' union
select 2014, 9, 'Sep' union
select 2014, 10, 'Oct' union
select 2014, 11, 'Nov' union
select 2014, 12, 'Dec'

-- create dynamic date range columns
select distinct Year, MonthNumber, convert(varchar(4), Year) + ' - ' + MonthName as Date
into #distinctDate
from #dateTable
order by Year, MonthNumber

select * from #distinctDate

--convert(CHAR(10), Date, 120)
-- create the dynamic date columns. (weeks of the year)
declare @cols AS NVARCHAR(MAX)
select @cols = STUFF((SELECT distinct ',' + QUOTENAME(Date)
FROM #distinctDate
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'')

select @cols




can i sort in this way? thanks
twin.devil
twin.devil
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1376 Visits: 2596
Use the following query


select @cols = STUFF((SELECT ',' + QUOTENAME(Date)
FROM #distinctDate
Order by [year], MonthNumber
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,''
)



I have remove the distinct, if you want to distinct the record to it when you create the #distinctDate

after that i have added the order by clause, because there is no such thing as Natural Order :-D
spin
spin
SSC-Enthusiastic
SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)

Group: General Forum Members
Points: 142 Visits: 534
perfect thanks
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search