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.
| Author |
Topic |
|
kamal.A
Yak Posting Veteran
70 Posts |
Posted - 2008-08-25 : 17:17:31
|
| Hi All,I need the query for the beneath.One of the field in my table is as follows.List1-3,3-5,2-1,1-2,1-22,3-90,2-33,2-23,4-90,4-80Here for eg 1-3 is a set and 3-5 is a set and 2-1 is also a set.My requirement is In 1-2 ---> 1 is a id referred to another table and 2 is price.I want to display the beneath output for 1-2.persontype priceaaa 2------------------->1 is aaa and 2 is price.Kindly help me.kamal. |
|
|
markross
Starting Member
4 Posts |
Posted - 2008-08-25 : 17:43:57
|
| Are you saying that:1-3,3-5,2-1,1-2,1-22,3-90,2-33,2-23,4-90,4-80is 4 rows from one column which has pairs that repersent (tbl2ID,PriceCode)?If so, I think that the table needs to be redesigned with a another detail tableOtherWise, I would use a stroreeProcedure to write a bit of code to loop throught the sets |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-08-25 : 23:21:03
|
[code]DECLARE @sample TABLE( List varchar(20))INSERT INTO @sampleSELECT '1-3,3-5,2-1' UNION ALLSELECT '1-2' UNION ALLSELECT '1-22,3-90' UNION ALLSELECT '2-33,2-23,4-90,4-80'DECLARE @another TABLE( ID varchar(10), persontype varchar(10))INSERT INTO @anotherSELECT '1', 'aaa' UNION ALLSELECT '2', 'bbb'SELECT s.List, s.stringval, a.persontype, s.priceFROM( SELECT s.List, c.stringval, ID = CASE WHEN CHARINDEX('-', c.stringval) <> 0 THEN left(c.stringval, CHARINDEX('-', c.stringval) - 1) END, price = CASE WHEN CHARINDEX('-', c.stringval) <> 0 THEN right(c.stringval, LEN(c.stringval) - CHARINDEX('-', c.stringval)) END FROM @sample s CROSS apply CSVTable(s.List) c) sINNER JOIN @another a ON s.ID = a.ID/*List stringval persontype price -------------------- ----------- ---------- ------1-3,3-5,2-1 1-3 aaa 31-3,3-5,2-1 2-1 bbb 11-2 1-2 aaa 21-22,3-90 1-22 aaa 222-33,2-23,4-90,4-80 2-33 bbb 332-33,2-23,4-90,4-80 2-23 bbb 23(6 row(s) affected)*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
kamal.A
Yak Posting Veteran
70 Posts |
Posted - 2008-08-26 : 09:51:05
|
| Hi khtanI used your code but i met the following error."Incorrect syntax near 'apply'." I have sql server 2005 in my system.I need strored procedure or a query. Kinldy help me.kamal........ |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-08-26 : 09:56:30
|
make sure the compatibility level is set at 90 KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
kamal.A
Yak Posting Veteran
70 Posts |
Posted - 2008-08-26 : 10:05:22
|
| Thanks I have change the compatibility level 90.But now I faced the following error message."Invalid object name 'CSVTable'."What is 'CSVTable'?kamal.. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
|
|
|
|
|
|