Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 query help

Author  Topic 

yopy_yogie
Starting Member

1 Post

Posted - 2005-09-09 : 00:04:12
i have the following row on the table

Item Year
-------------------------------
A 2007|2009|2013|2017

i want to query the table to have a result like below:

Item Year
-------------------------------
A 2007
A 2009
A 2013
A 2017

I have no idea how to solve it, please help !

thank you


madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-09-09 : 00:55:17
I think thats not the best way to store years in a single row
You need to use split functions as suggested here
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

VladRUS.ca
Starting Member

23 Posts

Posted - 2005-09-09 : 10:19:31
Hi yopy_yogie,

If your Year-string is not too long you can try something like this:
set nocount on
declare @t table(Item char(5), Year varchar(1000))
insert into @t
select 'A', '2007|2009|2013|2017'
union all
select 'B', '2001|2002|2003|2004|2005'

select * from @t


create table #t(Year int)
declare @t2 table(Item char(5), Year int)
declare @Item char(5), @strYear varchar(8000), @intYear int

select @Item = Item, @strYear = Year from @t where Item = (select min(Item) from @t)
while @@rowcount = 1
begin
set @strYear = 'insert into #t select ' + replace(@strYear, '|', 'union select ')
delete #t
exec(@strYear)
insert into @t2 select @Item, Year from #t
select @Item = Item, @strYear = Year from @t where Item = (select min(Item) from @t where Item > @Item)
end

select * from @t2
drop table #t

Best Regards,
Vlad
Go to Top of Page
   

- Advertisement -