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 |
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2010-04-20 : 08:16:44
|
| I have a different codes and its description available in table.e.g. ---- table test1 and column is codesKOE2M LSE1F ZZE3Rin above code.. KOE2M.. "KOE2" is code available in database with its description and "M" is identifier..there are four identifiers only.. M is for Missing, F for failure and R for Removed..so i want result like..--------- outputKiosk desc Missing LSE1 desc Failure ZZZE3 Removed---in above output i will fetch description from code and last character for its identifier desc and so one.. |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2010-04-20 : 08:59:45
|
[code]SELECT SUBSTRING(String, 0, 5) + CASE SUBSTRING(String, 5, 1) WHEN 'M' THEN ' Missing' WHEN 'F' THEN ' Failure' WHEN 'R' THEN ' Removed' END, SUBSTRING(String, 7, 4) + CASE SUBSTRING(String, 11, 1) WHEN 'M' THEN ' Missing' WHEN 'F' THEN ' Failure' WHEN 'R' THEN ' Removed' END, SUBSTRING(String, 13, 4) + CASE SUBSTRING(String, 17, 1) WHEN 'M' THEN ' Missing' WHEN 'F' THEN ' Failure' WHEN 'R' THEN ' Removed' ENDFROM ( SELECT 'KOE2M LSE1F ZZE3R' as [String])a[/code] Hope can help...but advise to wait pros with confirmation... |
 |
|
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2010-04-20 : 09:04:20
|
| That is fine waterduck but i need it in one column and also From Code i need descrip which comes from DB..i have done something like... but still in confusion.. is there any other way?THIS GIVES TABLE OUTPUT but I REQUIRED it in one column only with concatenationdeclare @strcode varchar(500)set @strcode = 'A20M A21R KKSER3M '--select charindex(' ', @strcode, 6)--select substring(@strcode, charindex(' ', @strcode, 6) - 1, 1)--select substring(@strcode, 0, charindex(' ',@strcode) - 1)declare @coel varchar(3000);WITH CTE (startpos, posit) as( select 1 as startpos, charindex(' ',@strcode,1) as posit union all select posit + 1 as startpos, charindex(' ', @strcode, posit + 1) as posit from cte where charindex(' ', @strcode, posit + 1) <> 0)select substring(@strcode, startpos, posit-startpos-1) + ' ' + substring(@strcode, posit-1, 1), * from cte |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2010-04-20 : 09:31:56
|
[code]SELECT SUBSTRING(String1, 0, LEN(String1))+CASE SUBSTRING(REVERSE(String1), 1, 1) WHEN 'M' THEN ' Missing ' WHEN 'F' THEN ' Failure ' WHEN 'R' THEN ' Removed ' END + SUBSTRING(String2, 0, LEN(String2))+CASE SUBSTRING(REVERSE(String2), 1, 1) WHEN 'M' THEN ' Missing ' WHEN 'F' THEN ' Failure ' WHEN 'R' THEN ' Removed ' END + SUBSTRING(String3, 0, LEN(String3))+CASE SUBSTRING(REVERSE(String3), 1, 1) WHEN 'M' THEN ' Missing ' WHEN 'F' THEN ' Failure ' WHEN 'R' THEN ' Removed ' ENDFROM ( SELECT SUBSTRING(String, 0, CHARINDEX(' ', String))String1, SUBSTRING(String, CHARINDEX(' ', String) + 1, CHARINDEX(' ', String) - 1)String2, REVERSE(SUBSTRING(REVERSE(String), 0, CHARINDEX(' ', REVERSE(String))))String3 FROM ( SELECT 'KOE2M LSE1F ZZE3R' as [String] )a )b[/code] Hope can help...but advise to wait pros with confirmation... |
 |
|
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2010-04-20 : 09:38:34
|
| hi waterduck..thanks for your reply.. |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2010-04-20 : 09:40:38
|
not quite sure correct or not...but if wrong please ask sql pros >"< Hope can help...but advise to wait pros with confirmation... |
 |
|
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2010-04-20 : 09:42:39
|
| :)yes waterduckyou are correct per your sidebut you can also run my CTE query too..i can easily operate database operation to get description from code too..thanks again.. |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2010-04-20 : 09:52:22
|
| Visakh will come around shortly to show how this can be done with a cross apply, but I think this works for what you wantDECLARE @Code varchar(50)SET @Code = 'KOE2M LSE1F ZZE3R'SET @Code = REPLACE(@Code,' ','.')SELECT PARSENAME(REPLACE(@Code,' ','.'),3) + CASE RIGHT(PARSENAME(REPLACE(@Code,' ','.'),3),1) WHEN 'M' THEN ' MISSING' WHEN 'R' THEN ' REMOVED' WHEN 'F' THEN 'FAILURE' END as Code ,PARSENAME(REPLACE(@Code,' ','.'),2) + CASE RIGHT(PARSENAME(REPLACE(@Code,' ','.'),2),1) WHEN 'M' THEN ' MISSING' WHEN 'R' THEN ' REMOVED' WHEN 'F' THEN ' FAILURE' END ,PARSENAME(REPLACE(@Code,' ','.'),1) + CASE RIGHT(PARSENAME(REPLACE(@Code,' ','.'),1),1) WHEN 'M' THEN ' MISSING' WHEN 'R' THEN ' REMOVED' WHEN 'F' THEN 'FAILURE' ENDEveryday I learn something that somebody else already knew |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-20 : 10:01:47
|
| how will the values be stored in db? are codes and identifiers both stored in same table with description?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
xpandre
Posting Yak Master
212 Posts |
Posted - 2010-04-20 : 10:08:59
|
| [code]declare @strcode varchar(500)set @strcode = 'A20M A21R KKSER3M abcR 'declare @coel varchar(3000);WITH CTE (startpos, posit) as(select 1 as startpos, charindex(' ',@strcode,1) as positunion allselect posit + 1 as startpos, charindex(' ', @strcode, posit + 1) as positfrom cte where charindex(' ', @strcode, posit + 1) <> 0), cte2 as(select substring(substring(@strcode,startpos,posit-startpos),1,len(substring(@strcode,startpos,posit-startpos))-1) a1,case when substring(substring(@strcode,startpos,posit-startpos),len(substring(@strcode,startpos,posit-startpos)),1) = 'M' then ' Missing'when substring(substring(@strcode,startpos,posit-startpos),len(substring(@strcode,startpos,posit-startpos)),1) = 'R' then ' Removed'end a2from cte ),cte3 as(select distinct bb from cte2 tt2 cross apply (select a1 + a2+' ' from cte2 for xml path('')) as c(bb))select * from cte3[/code] |
 |
|
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2010-04-21 : 01:13:43
|
quote: Originally posted by visakh16 how will the values be stored in db? are codes and identifiers both stored in same table with description?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Yes visakh my whole code string will be stored in one column as: "KOE2M LSE1F ZZE3R" and code is in one table with its descriptionAND code can be of any length.. suppose KOE2 is first code in previous string and M is identifier.. then other code can be short or long.. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-21 : 01:17:04
|
| will format be always consistent? 4 characters of code followed by 1 char identifier in each word?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2010-04-21 : 01:19:12
|
| yes.. code can be of any lengh but 1 char identifier is fix..likeKOOS2MKOOMKOO332MKOO33M |
 |
|
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2010-04-21 : 02:12:00
|
quote: Originally posted by xpandre
declare @strcode varchar(500)set @strcode = 'A20M A21R KKSER3M abcR 'declare @coel varchar(3000);WITH CTE (startpos, posit) as(select 1 as startpos, charindex(' ',@strcode,1) as positunion allselect posit + 1 as startpos, charindex(' ', @strcode, posit + 1) as positfrom cte where charindex(' ', @strcode, posit + 1) <> 0), cte2 as(select substring(substring(@strcode,startpos,posit-startpos),1,len(substring(@strcode,startpos,posit-startpos))-1) a1,case when substring(substring(@strcode,startpos,posit-startpos),len(substring(@strcode,startpos,posit-startpos)),1) = 'M' then ' Missing'when substring(substring(@strcode,startpos,posit-startpos),len(substring(@strcode,startpos,posit-startpos)),1) = 'R' then ' Removed'end a2from cte ),cte3 as(select distinct bb from cte2 tt2 cross apply (select a1 + a2+' ' from cte2 for xml path('')) as c(bb))select * from cte3
Thanks.. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-21 : 06:03:28
|
quote: Originally posted by keyursoni85 yes.. code can be of any lengh but 1 char identifier is fix..likeKOOS2MKOOMKOO332MKOO33M
you can use a function to parse the string and then use join to lookup values based on individual values. something likeSELECT n.Description + ' ' + o.DescriptionFROM(SELECT LEFT(f.Val,LEN(f.Val)-1) AS code,RIGHT(f.Val,1) AS IdFROM YourTable tCROSS APPLY dbo.ParseValues(t.codes,' ')f)mCROSS APPLY (SELECT TOP 1 Description FROM YourlookupTable l WHERE code LIKE m.code + '%' ORDER BY LEN(code) DESC) nCROSS APPLY (SELECT Description FROM YourlookupTable l WHERE code =m.ID ) o ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2010-04-21 : 06:15:06
|
quote: Originally posted by visakh16
quote: Originally posted by keyursoni85 yes.. code can be of any lengh but 1 char identifier is fix..likeKOOS2MKOOMKOO332MKOO33M
you can use a function to parse the string and then use join to lookup values based on individual values. something likeSELECT n.Description + ' ' + o.DescriptionFROM(SELECT LEFT(f.Val,LEN(f.Val)-1) AS code,RIGHT(f.Val,1) AS IdFROM YourTable tCROSS APPLY dbo.ParseValues(t.codes,' ')f)mCROSS APPLY (SELECT TOP 1 Description FROM YourlookupTable l WHERE code LIKE m.code + '%' ORDER BY LEN(code) DESC) nCROSS APPLY (SELECT Description FROM YourlookupTable l WHERE code =m.ID ) o ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Thanks visakh.. the last cross apply which you have used above.. i doesnt need it as my last character is static identifier like Missing.. Failure etc..But above thing give me a tabled rows for each description code..didnt getttng above query.. currently i have used below one..declare @strcode varchar(500)set @strcode = 'A20M A21R KKSER3M abcR 'declare @coel varchar(3000);WITH CTE (startpos, posit) as(select 1 as startpos, charindex(' ',@strcode,1) as positunion allselect posit + 1 as startpos, charindex(' ', @strcode, posit + 1) as positfrom cte where charindex(' ', @strcode, posit + 1) <> 0), cte2 as(select substring(substring(@strcode,startpos,posit-startpos),1,len(substring(@strcode,startpos,posit-startpos))-1) a1,case when substring(substring(@strcode,startpos,posit-startpos),len(substring(@strcode,startpos,posit-startpos)),1) = 'M' then ' Missing'when substring(substring(@strcode,startpos,posit-startpos),len(substring(@strcode,startpos,posit-startpos)),1) = 'R' then ' Removed'end a2from cte ),cte3 as(select distinct bb from cte2 tt2 cross apply (select a1 + a2+' ' from cte2 for xml path('')) as c(bb))select * from cte3Because i need it in one row for my series of codes.. |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2010-04-21 : 08:17:45
|
| DECLARE @Code varchar(50)SET @Code = 'KOE2M LSE1F ZZE3R'SET @Code = REPLACE(@Code,' ','.')SELECT PARSENAME(REPLACE(@Code,' ','.'),3) + t1.CodeDesc +' '+ PARSENAME(REPLACE(@Code,' ','.'),2) + t1.CodeDesc +' '+ PARSENAME(REPLACE(@Code,' ','.'),1) + t1.CodeDescFROM(SELECT [Code] = 'M',[CodeDesc] = ' Missing' UNIONSELECT 'R',' Removed' UNIONSELECT 'F',' Failure')t1WHERE RIGHT( PARSENAME(REPLACE(@Code,' ','.'),3),1) = t1.[code]Everyday I learn something that somebody else already knew |
 |
|
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2010-04-22 : 05:18:49
|
quote: Originally posted by jimf DECLARE @Code varchar(50)SET @Code = 'KOE2M LSE1F ZZE3R'SET @Code = REPLACE(@Code,' ','.')SELECT PARSENAME(REPLACE(@Code,' ','.'),3) + t1.CodeDesc +' '+ PARSENAME(REPLACE(@Code,' ','.'),2) + t1.CodeDesc +' '+ PARSENAME(REPLACE(@Code,' ','.'),1) + t1.CodeDescFROM(SELECT [Code] = 'M',[CodeDesc] = ' Missing' UNIONSELECT 'R',' Removed' UNIONSELECT 'F',' Failure')t1WHERE RIGHT( PARSENAME(REPLACE(@Code,' ','.'),3),1) = t1.[code]Everyday I learn something that somebody else already knew
Above will give me.. below invalid output---------------------------------KOE2M Missing LSE1F Missing ZZE3R Missing |
 |
|
|
|
|
|
|
|