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 |
|
ankitshah2003
Starting Member
5 Posts |
Posted - 2008-08-27 : 06:10:21
|
| hi all i stuck up with one problemi want to fire one query where i want to check the string like@lc='1,9,150'with the one field f2 of table which has number of rows contain data likefield value=============f1 f2== ==1 1,337,2 337,150,3 150,9,4 111,235 9,23as result i want to get the set like1 1,337,2 337,150,if anybody knows plz help me thanksAnkit shah |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-08-27 : 06:17:44
|
[code]DECLARE @lc varchar(10)DECLARE @sample TABLE( f1 int, f2 varchar(10))INSERT INTO @sampleSELECT 1, '1,337,' UNION ALLSELECT 2, '337,150,' UNION ALLSELECT 3, '150,9,' UNION ALLSELECT 4, '111,23' UNION ALLSELECT 5, '9,23'SELECT @lc ='1,9,150'SELECT DISTINCT s.f1, s.f2FROM @sample s CROSS apply CSVTable(s.f2) f2 INNER JOIN CSVTable(@lc) lc ON f2.stringval = lc.stringval/*f1 f2 ----------- ---------- 1 1,337, 2 337,150, 3 150,9, 5 9,23(4 row(s) affected)*/[/code]http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=25830&SearchTerms=CSVTable KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-27 : 06:29:29
|
Why are you excluding3 150,9,5 9,23??? E 12°55'05.25"N 56°04'39.16" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-27 : 06:46:18
|
quote: Originally posted by Peso Why are you excluding3 150,9,5 9,23??? E 12°55'05.25"N 56°04'39.16"
seems like OP gave only part of result |
 |
|
|
ankitshah2003
Starting Member
5 Posts |
Posted - 2008-08-27 : 06:49:11
|
| hi khtan,u understand perfectlly but that csvtable is not working in sql server2005 and another thing that i want to check the @lc variable value to the feild of table which has numbers of rows and columns so this solution can works for it?thanksAnkit shah |
 |
|
|
ankitshah2003
Starting Member
5 Posts |
Posted - 2008-08-27 : 06:54:50
|
| hi Peso i also want that two rows into my result set only 4 '111,23'is not in the resultif u have solution then tell me.thanksAnkit shah |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-27 : 07:51:40
|
Try this then:-declare @Test table(f1 int,f2 varchar(100))declare @amount varchar(100)set @amount='1,9,23'insert into @testselect 1, '1,337,' union allselect 2, '337,150,' union allselect 3, '150,9,' union allselect 4, '111,23' union allselect 5, '9,23'select DISTINCT t.* from @test tcross apply dbo.ParseValues(t.f2,',') bcross apply dbo.ParseValues(@amount,',')cwhere b.Val=c.Val output---------------------------f1 f2----------- -------------------1 1,337,3 150,9,4 111,235 9,23 the function parsevalues can be given belowhttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=104485&SearchTerms=parsevalues |
 |
|
|
ankitshah2003
Starting Member
5 Posts |
Posted - 2008-08-27 : 08:04:17
|
| hi Vishakh16 this seems to not works for that any other solution thanksAnkit shah |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-27 : 08:06:06
|
quote: Originally posted by ankitshah2003 hi Vishakh16 this seems to not works for that any other solution thanksAnkit shah
what's the error obtained? give some sample data of where this does not work. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-08-27 : 08:11:08
|
quote: Originally posted by ankitshah2003 hi khtan,u understand perfectlly but that csvtable is not working in sql server2005 and another thing that i want to check the @lc variable value to the feild of table which has numbers of rows and columns so this solution can works for it?thanksAnkit shah
What do you mean CSVTable not working in SQL Server 2005 ? Do you get any error ? The query i posted is running on SQL 2005Can you also explain what do you mean by "i want to check the @lc variable value to the feild of table which has numbers of rows and columns" ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-08-27 : 08:33:40
|
| Read about NormalizationMadhivananFailing to plan is Planning to fail |
 |
|
|
ankitshah2003
Starting Member
5 Posts |
Posted - 2008-08-27 : 09:36:05
|
| hi khsorry i don't know abt CSVTable functionbut after that i tried that link and get the CSVTable function and now it is working thanks a lotAnkit shah |
 |
|
|
|
|
|
|
|