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 |
|
acdacd
Yak Posting Veteran
63 Posts |
Posted - 2005-07-06 : 13:59:58
|
| Dear all ,I have two table. Both part have interest field. All interest field are entered in csv format. I would like to use this function in normal select queryselect * from CSVTOVarChar((@query))or evenselect * from CSVTOVarChar((select Info2 from Testtable5 where Id = 29))//Error MessageServer: Msg 170, Level 15, State 1, Line 2Line 2: Incorrect syntax near '('.Server: Msg 170, Level 15, State 1, Line 2Line 2: Incorrect syntax near ')'.Any suggestion?Thx!!/******************/dbo.CsvToVarchar ( @Array varchar(1000)) returns @StringTable table (StringValue varchar(50))ASbegin declare @separator char(1) set @separator = ',' declare @separator_position int declare @array_value varchar(1000) set @array = @array + ',' while patindex('%,%' , @array) <> 0 begin select @separator_position = patindex('%,%' , @array) select @array_value = left(@array, @separator_position - 1) select @array_value = RTrim(@array_value) select @array_value = LTrim(@array_value) Insert @StringTable Values (Cast(@array_value as varchar(50))) select @array = stuff(@array, 1, @separator_position, '') end returnend |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-07-06 : 14:06:53
|
Here's 2 ways to use it:declare @csv varchar(50)set @csv = '29,30,31'select * from dbo.CsvToVarchar(@csv)select a.myColfrom myTable ajoin dbo.CsvToVarchar(@csv) b on a.myIDCol = b.StringValue Be One with the OptimizerTG |
 |
|
|
|
|
|
|
|