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 |
|
flamblaster
Constraint Violating Yak Guru
384 Posts |
Posted - 2008-11-29 : 16:18:18
|
| Hey folks,Vis just answered my issue yesterday on parsename; If you see the post, I actually wound up using Charindex by Vis' suggestion. Vis (or anyone else who might like to input), I was wondering, would something like the Substring(Reverse(Charindex... syntax work for the following? I'm getting partial results, but it's filtering a lot out that I don't want to filter:DATASET:Table: ClientsFields: ClientID, Disability EDIT: (Disability is varchar(30) )When I run a basic select query:SELECT ClientID, DisabilityFROM ClientsThe sample results would be:ClientID***Disability1 *** 1,B,C,F2 *** 3,G,H3 *** 1,D,EMy desired output is:ClientID***Disability1 ***11 ***B1 ***C1 ***F2 ***32 ***G2 ***H3 ***13 ***D3 ***EI don't think the Charindex will work because the only separators will ever be a comma or a space. And again, I'm not able to declare the cursor, create stored procedures, drop table, create view or anything like that in the interface that I have.Thanks!Craig |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2008-11-29 : 19:47:27
|
| Take a look here:http://www.sqlteam.com/article/parsing-csv-values-into-multiple-rowsIf you can't create a tally/numbers table, you'll have to include something like this in your query:FROM (select 1 n union all select 2 union all select 3 union all select 4....) TallyAs an aside, why are people asking you to write these things without the proper tools? |
 |
|
|
flamblaster
Constraint Violating Yak Guru
384 Posts |
Posted - 2008-11-30 : 00:03:00
|
| Hi Rob, thanks for the reply. I read the arcticle you pointed me to and was able to create a query using the examples without errors, but it did not separate the values. I tried a couple examples using your note above, but I"m not sure if I understand it correctly. Does the Tally column need to be joined with the table in my database? I again just received the data back the same way from the original example.SELECT Clientid, disabilityFROM Clients,(select 1 n union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 'a' union all select 'b' union all select 'c' union all select 'd' union all select 'e' union all select 'f') TallyThanks again!Craig |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-30 : 01:00:56
|
quote: Originally posted by flamblaster Hey folks,Vis just answered my issue yesterday on parsename; If you see the post, I actually wound up using Charindex by Vis' suggestion. Vis (or anyone else who might like to input), I was wondering, would something like the Substring(Reverse(Charindex... syntax work for the following? I'm getting partial results, but it's filtering a lot out that I don't want to filter:DATASET:Table: ClientsFields: ClientID, Disability EDIT: (Disability is varchar(30) )When I run a basic select query:SELECT ClientID, DisabilityFROM ClientsThe sample results would be:ClientID***Disability1 *** 1,B,C,F2 *** 3,G,H3 *** 1,D,EMy desired output is:ClientID***Disability1 ***11 ***B1 ***C1 ***F2 ***32 ***G2 ***H3 ***13 ***D3 ***EI don't think the Charindex will work because the only separators will ever be a comma or a space. And again, I'm not able to declare the cursor, create stored procedures, drop table, create view or anything like that in the interface that I have.Thanks!Craig
Craig the best way to deal this is to use a udf that parses the comma seperated string and gets you the individual values. see below UDF which does this (it uses CHARINDEX internally)CREATE FUNCTION ParseValues (@String varchar(8000),@Delimiter char(1) ) RETURNS @RESULTS TABLE (ID int identity(1,1), Val varchar(100) ) AS BEGIN DECLARE @Value varchar(100) WHILE @String is not null BEGIN SELECT @Value=CASE WHEN CHARINDEX(@Delimiter,@String) >0 THEN LEFT(@String,CHARINDEX(@Delimiter,@String)-1) ELSE @String END, @String=CASE WHEN CHARINDEX(@Delimiter,@String) >0 THEN SUBSTRING(@String,CHARINDEX(@Delimiter,@String)+1,LEN(@String)) ELSE NULL END INSERT INTO @RESULTS (Val) SELECT @Value END RETURN END then use it in your code as belowSELECT c.ClientID,f.ValFROM Clients cCROSS APPLY dbo.ParseValues(c.Disability) fORDER BY f.ID |
 |
|
|
flamblaster
Constraint Violating Yak Guru
384 Posts |
Posted - 2008-11-30 : 01:08:59
|
| Hi Vis, As always, thanks for the reply. I understand the post, but am unable to create that function...Suppose there's not a work around usinga Select statement alone. That's ok though...I thought maybe there was similar logic from the example you answered yesterday.Thanks again!Craig |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-30 : 01:32:58
|
quote: Originally posted by flamblaster Hi Vis, As always, thanks for the reply. I understand the post, but am unable to create that function...Suppose there's not a work around usinga Select statement alone. That's ok though...I thought maybe there was similar logic from the example you answered yesterday.Thanks again!Craig
whats the error you got? or is it that you dont have permissions to create UDF? |
 |
|
|
flamblaster
Constraint Violating Yak Guru
384 Posts |
Posted - 2008-11-30 : 01:48:31
|
| The latter...I don't have permission to create the udf...the error is, "please enter a SELECT statement" (that's a function of the interface that I use.This issue, while helpful, isn't one of my biggest operational concerns...it would be good to be able to sort this info out, but I was really trying to learn the procedure for parsing that type of data for future reference. If you have a work around that's wonderful...if not...I can use my fingers to count :) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-30 : 02:33:30
|
| I think in that case you could use method Robvolk suggested. the tally table will contain numbers from 1 to 8000 and you take join with that as shown in article. |
 |
|
|
flamblaster
Constraint Violating Yak Guru
384 Posts |
Posted - 2008-11-30 : 03:34:16
|
| Vis and Rob,Thanks again...that worked perfectly...I was getting hung up on the tally table...I kept trying to put the clientid where the ID was...here's the way it looks now and gives the intended results:SELECT Clientid,NullIf(SubString(',' + Disability+ ',' , ID , CharIndex(',' , ',' + Disability + ',' , ID) - ID) , '') AS 'Disability'FROM (SELECT 1 ID UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12 UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL SELECT 15 UNION ALL SELECT 16 UNION ALL SELECT 17 UNION ALL SELECT 18 UNION ALL SELECT 19 UNION ALL SELECT 20 UNION ALL SELECT 21 UNION ALL SELECT 22 UNION ALL SELECT 23 UNION ALL SELECT 24 UNION ALL SELECT 25) Tally, ClientsWHERE ID <= Len(',' + Disability + ',') AND SubString(',' + Disability + ',' , ID - 1, 1) = ',' AND CharIndex(',' , ',' + Disability + ',' , ID) - ID > 0ORDER BY Clientid ASCI appreciate it guys! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-30 : 06:43:43
|
You're welcome |
 |
|
|
|
|
|
|
|