| Author |
Topic |
|
ismailc
Constraint Violating Yak Guru
290 Posts |
Posted - 2009-11-02 : 03:24:52
|
| Good day, I need help please. I have a string value: Agent-North West [2F] Agent-North East [2P] I would like to split the string on find "]" create new row. output: Agent-North West [2F] Agent-North East [2P]Please Help |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-11-02 : 03:43:28
|
| try like thisdeclare @str1 varchar(max)set @str1= 'Agent-North West [2F] Agent-North East [2P]Agent-North North [2P]Agent-North South [2P]'SELECT * FROM (SELECT SUBSTRING(@str1,charindex(']',@str1,v.number)+1,abs(charindex(']',@str1,charindex(']',@str1,v.number)+1)-charindex(']',@str1,v.number))) as valueFROM master..spt_values AS v WHERE v.Type = 'P' AND v.number > 0 AND substring(']' + @str1, v.number, 1) = ']')s WHERE [VALUE] <> '' |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
|
|
ismailc
Constraint Violating Yak Guru
290 Posts |
Posted - 2009-11-02 : 03:58:53
|
| Unfortunately, I'm not winning on both methods. On the link i can't find the function: fnParseListPlease Assist! |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-11-02 : 04:07:25
|
here fnParseList KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-11-02 : 04:08:57
|
quote: Originally posted by ismailc Unfortunately, I'm not winning on both methods. On the link i can't find the function: fnParseListPlease Assist!
Post what you have tried with first replyMadhivananFailing to plan is Planning to fail |
 |
|
|
ismailc
Constraint Violating Yak Guru
290 Posts |
Posted - 2009-11-02 : 04:22:19
|
| Hi, I'm winning a bit, but I get a space in front of some Values on the new row even when i'm doing a ltrim:eg: Test1 [1] Test2 [2] Test3 [3] Test4 [4]declare @str1 varchar(1000)select @str1 = MAX(v.ControlValue) from dbo.fcEventArchive as e SELECT distinct ltrim(rtrim(value)) as Sites FROM (SELECT SUBSTRING(@str1,charindex(']',@str1,v.number)+1,abs(charindex(']',@str1,charindex(']',@str1,v.number)+1)-charindex(']',@str1,v.number))) as valueFROM master..spt_values AS v WHERE v.Type = 'P'AND v.number > 0AND substring(']' + @str1, v.number, 1) = ']')s WHERE [VALUE] <> ' |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-11-02 : 04:26:46
|
| You would get space if you use ltrimMadhivananFailing to plan is Planning to fail |
 |
|
|
ismailc
Constraint Violating Yak Guru
290 Posts |
Posted - 2009-11-02 : 04:33:11
|
| I don't understand = i het space using ltrim " test3 [3]" - and i want "test3 [3]"ltrim(value)? |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-11-02 : 04:36:28
|
[code]declare @str varchar(max)set @str= 'Agent-North West [2F] Agent-North East [2P]Agent-North North [2P]Agent-North South [2P]'select ltrim(Data) + ']'from fnParseList(']', @str)where Data <> ''[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
ismailc
Constraint Violating Yak Guru
290 Posts |
Posted - 2009-11-02 : 04:44:10
|
| Thank You - I created the function & tried. but unfortunatley it also returns rows with a space in frontRegards |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-11-02 : 04:46:39
|
example ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
ismailc
Constraint Violating Yak Guru
290 Posts |
Posted - 2009-11-02 : 04:49:22
|
| I'm also struggling to create the stored procedure: ALTER PROCEDURE [dbo].[FC0_RP_VendorSite] @ProcessNo int, @str1 varchar(1000)ASBEGIN SET NOCOUNT ON;IF EXISTS (select * from FC_LIVE.Flowcentric.dbo.fcEventArchive where ProcessID = @ProcessNo)select @str1 = MAX(v.ControlValue) from FC_LIVE.Flowcentric.dbo.fcEventArchive as e where e.ProcessID = @ProcessNoSELECT distinct ltrim(value) as Sites FROM (SELECT SUBSTRING(@str1,charindex(']',@str1,v.number)+1,abs(charindex(']',@str1,charindex(']',@str1,v.number)+1)-charindex(']',@str1,v.number))) as valueFROM master..spt_values AS v WHERE v.Type = 'P'AND v.number > 0AND substring(']' + @str1, v.number, 1) = ']')s WHERE [VALUE] <> ''Elseselect @str1 = MAX(v.ControlValue) from FC_LIVE.Flowcentric.dbo.fcEventHist as e where e.ProcessID = @ProcessNoSELECT distinct ltrim(value) as Sites FROM (SELECT SUBSTRING(@str1,charindex(']',@str1,v.number)+1,abs(charindex(']',@str1,charindex(']',@str1,v.number)+1)-charindex(']',@str1,v.number))) as valueFROM master..spt_values AS v WHERE v.Type = 'P'AND v.number > 0AND substring(']' + @str1, v.number, 1) = ']')s WHERE [VALUE] <> '' ENDThe If else works when using normal queryBut i get the error: Incorrect syntax near the keyword 'Else'Please Assist! |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-11-02 : 04:50:33
|
| use if()beginselect...........endelse beginselect..........end |
 |
|
|
ismailc
Constraint Violating Yak Guru
290 Posts |
Posted - 2009-11-02 : 04:51:51
|
| values reurned:"test1 [1]""test2 [2]"" test3 [3]" |
 |
|
|
ismailc
Constraint Violating Yak Guru
290 Posts |
Posted - 2009-11-02 : 04:55:40
|
| Thank You :) I got the stored procedure working. I must only get rid of the space in front of the value |
 |
|
|
ismailc
Constraint Violating Yak Guru
290 Posts |
Posted - 2009-11-02 : 05:06:34
|
| actually all of them has a space in front some has 2 spaces |
 |
|
|
ismailc
Constraint Violating Yak Guru
290 Posts |
Posted - 2009-11-02 : 05:25:25
|
| Thank You All for helping - this is great :) |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-11-02 : 05:49:53
|
welcome |
 |
|
|
|