| Author |
Topic |
|
namasteall2000
Starting Member
20 Posts |
Posted - 2005-11-10 : 14:24:40
|
| Hi – I have a column named as ‘Explain’ and data type as ‘’Text’Explain--#24# Medicare Crossover Claim Pays at Zero Dollars #N36# Invalid Lifetime Reserve Days #N34# Invalid Bill TypeNow my problem is – as this is a text column – there is very few function which works. I want where ever we get # sign we need to pick up code from ##.Like output should beExplain --24N36N34Could anyone suggest some solution? |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-11-10 : 16:05:21
|
| Well, a few functions you can use with text datatypes are:substring, charindex, patindex, and datalengthThose would be pretty helpfull for this.you can also convert the column value (or chunks-at-a-time ) to varchar and work with that.What is the max(datalength) of this column?You'll also have to define some rules. Like what should be returned for these values:'he is my #2 man in charge''#please ignore these comments#''###''#N34#24#''#ignore this# but use this: #20# ######'Be One with the OptimizerTG |
 |
|
|
namasteall2000
Starting Member
20 Posts |
Posted - 2005-11-10 : 16:07:50
|
| No we can define the length of the this column.But yes rule is defined that everytime ruleid is going to be in ## e.g #12# or #N34#.Do you think we can have some solution for that?Thanks |
 |
|
|
namasteall2000
Starting Member
20 Posts |
Posted - 2005-11-11 : 08:03:18
|
| Does anyone have any solution for the problem? |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-11-11 : 08:29:37
|
1. still waiting to hear the rules.quote: You'll also have to define some rules. Like what should be returned for these values:'he is my #2 man in charge''#please ignore these comments#''###''#N34#24#''#ignore this# but use this: #20# ######'
2. Is it an option to convert the entire contents to varchar?quote: What is the max(datalength) of this column?
3. Do you have a table of acceptable values? ie: must be in ('24', 'N36', 'N34')Be One with the OptimizerTG |
 |
|
|
namasteall2000
Starting Member
20 Posts |
Posted - 2005-11-11 : 08:45:17
|
| 1 - Rule is already defined - coulmn will always have code in ## e.g #N30#abcdede#30# like this.2- Well we can define max length of this field say Varchar(2000).3 - No - I want to create a temp table to have codes in it.Let me know if you have any questions. |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-11-11 : 10:12:12
|
Here is one approach that could work. It assumes the rules that a code is defined by any value BETWEEN 2 hash <#> characters, that the explain column values is not more than 2000 characters, and that the code values are <= 50 characters.Just paste this entire code block into a QA window to see it work:set nocount oncreate table #junk (rowid int identity(1,1) primary key clustered, explain text)goinsert #junk (explain)select '#24# Medicare Crossover Claim Pays at Zero Dollars #N36# Invalid Lifetime Reserve Days #N34# Invalid Bill Type' union allselect 'he is my #2 man in charge' union allselect '#please ignore these comments#' union allselect '###' union allselect '#N34#24#' union allselect '#ignore this# but use this: #20# ######'go--create a function to return the codes as a tablecreate function dbo.fn_ExplainCodes(@explain varchar(2000))returns @codes table (code varchar(50))asbegin /* this function extracts values BETWEEN hash <#> characters assumes: input is <= 2000 characters values between hash chars is <= 50 */ declare @idx int ,@inCode bit ,@len int ,@c char(1) ,@val varchar(50) select @idx = 0 ,@inCode = 0 ,@val = '' set @len = len(@explain) while @idx <= @len begin select @idx = @idx + 1 ,@c = substring(@explain, @idx, 1) if @c = '#' and @inCode = 0 begin select @val = '' ,@incode = 1 end else if @c = '#' and @inCode = 1 begin select @inCode = 0 insert @codes select @val where @val <> '' end else if @inCode = 1 begin set @val = @val + @c end end returnendgo--use the function to populate a table of codesdeclare @codes table (rowid int identity(7,-1), ExplainCode varchar(50))declare @explain varchar(2000) ,@i intselect @i = max(rowid) from #junkwhile @i > 0begin select @explain = substring(explain,1,2000) from #junk where rowid = @i insert @codes select code from dbo.fn_ExplainCodes(@explain) select @i = @i - 1endselect * from @codes order by 1go--clean updrop function dbo.fn_ExplainCodesdrop table #junk Be One with the OptimizerTG |
 |
|
|
namasteall2000
Starting Member
20 Posts |
Posted - 2005-11-11 : 10:27:03
|
| Thanks for the solution. I haven't tried to work on it - but will do that soon. I will let you know if it works fine.Thanks |
 |
|
|
namasteall2000
Starting Member
20 Posts |
Posted - 2005-11-11 : 10:43:18
|
| Hi TG- Code is working absolutely fine. I am so thankful to you.I have another question - is it possible to have output in a temp bariable rather than in a table.Like instead of showingN34N20N21Can we show it like N34 N20 N21?I tried to change the last part of the code but it did n't work - could you check it?declare @codes table (rowid int identity(7,-1), ExplainCode varchar(50))declare @explain varchar(2000) ,@i int ,@FinalREsults Varchar(200)Set @FinalResults =''select @i = max(rowid) from #junkwhile @i > 0begin select @explain = substring(explain,1,2000) from #junk where rowid = @i --insert @codes Set @FinalResults = @FinalResults + /* Select code from*/ Select dbo.fn_ExplainCodes('#12#') select @i = @i - 1endPrint @FinalResultsThanks in advance.... |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-11-11 : 11:03:43
|
oh, changing the requirements half way throught the project, huh? I'm going to have to charge you double for that :)set nocount oncreate table #junk (rowid int identity(1,1) primary key clustered, explain text)goinsert #junk (explain)select '#24# Medicare Crossover Claim Pays at Zero Dollars #N36# Invalid Lifetime Reserve Days #N34# Invalid Bill Type' union allselect 'he is my #2 man in charge' union allselect '#please ignore these comments#' union allselect '###' union allselect '#N34#24#' union allselect '#ignore this# but use this: #20# ######'go--create a function to return the codes as a tablecreate function dbo.fn_ExplainCodes(@explain varchar(2000))returns varchar(2000)asbegin /* this function extracts values BETWEEN hash <#> characters assumes: input is <= 2000 characters values between hash chars is <= 50 */ declare @codes table (rowid int identity(1,1), code varchar(50)) declare @codesCSV varchar(2000) declare @idx int ,@inCode bit ,@len int ,@c char(1) ,@val varchar(50) select @idx = 0 ,@inCode = 0 ,@val = '' set @len = len(@explain) while @idx <= @len begin select @idx = @idx + 1 ,@c = substring(@explain, @idx, 1) if @c = '#' and @inCode = 0 begin select @val = '' ,@incode = 1 end else if @c = '#' and @inCode = 1 begin select @inCode = 0 insert @codes select @val where @val <> '' end else if @inCode = 1 begin set @val = @val + @c end end select @codesCSV = coalesce(@codesCSV + ',' + code, code) from @codes return @codesCSVendgo--use the function to return Code comma seperated values (CSV)select rowid, dbo.fn_ExplainCodes(explain) CodeCSV from #junkgo--clean updrop function dbo.fn_ExplainCodesdrop table #junk Be One with the OptimizerTG |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2005-11-11 : 11:07:44
|
quote: Originally posted by namasteall2000 No we can define the length of the this column.But yes rule is defined that everytime ruleid is going to be in ## e.g #12# or #N34#.Do you think we can have some solution for that?Thanks
You could store your data in a normalized manner and then you wouldn't have these issues. You should never stuff multiple entries into a single row/column in a table -- it should be broken out into related tables. see: http://www.datamodel.org/NormalizationRules.htmlfor more info. |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-11-11 : 11:11:16
|
quote: You could store your data in a normalized manner and then you wouldn't have these issues. You should never stuff multiple entries into a single row/column in a table -- it should be broken out into related tables. see: http://www.datamodel.org/NormalizationRules.html
That's what I meant to say Also, if the max value is less than 8000 characters you shouldn't be using TEXT.Be One with the OptimizerTG |
 |
|
|
namasteall2000
Starting Member
20 Posts |
Posted - 2005-11-11 : 11:45:45
|
| Well - you are absolutely right - never change requirment in the middle of the project. But thanks for taking care of it. I appreciate your help.Thanks Again.... |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-11-14 : 01:00:37
|
quote: Originally posted by namasteall2000 Hi – I have a column named as ‘Explain’ and data type as ‘’Text’Explain--#24# Medicare Crossover Claim Pays at Zero Dollars #N36# Invalid Lifetime Reserve Days #N34# Invalid Bill TypeNow my problem is – as this is a text column – there is very few function which works. I want where ever we get # sign we need to pick up code from ##.Like output should beExplain --24N36N34Could anyone suggest some solution?
Use Seventhnight's split function herehttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648Then write this querySelect data from split('#24# Medicare Crossover Claim Pays at Zero Dollars #N36# Invalid Lifetime Reserve Days #N34# Invalid Bill Type','#') where id%2 =0MadhivananFailing to plan is Planning to fail |
 |
|
|
namasteall2000
Starting Member
20 Posts |
Posted - 2005-11-23 : 09:52:22
|
| Hey TG - I am facing a problem with this code. Its pulling up duplicates too - and in my codeing I don't want to pull up the duplicates - Could you let me know if this problem can with solved in the code itself.If you run first line - then it should not show 24 twice.set nocount oncreate table #junk (rowid int identity(1,1) primary key clustered, explain text)goinsert #junk (explain)select '#24# Medicare Crossover Claim Pays at Zero Dollars #N36# Invalid Lifetime Reserve Days #N34# Invalid Bill Type #24#' union allselect 'he is my #2 man in charge' union allselect '#please ignore these comments#' union allselect '###' union allselect '#N34#24#' union allselect '#ignore this# but use this: #20# ######'go--create a function to return the codes as a tablecreate function dbo.fn_ExplainCodes(@explain varchar(2000))returns varchar(2000)asbegin /* this function extracts values BETWEEN hash <#> characters assumes: input is <= 2000 characters values between hash chars is <= 50 */ declare @codes table (rowid int identity(1,1), code varchar(50)) declare @codesCSV varchar(2000) declare @idx int ,@inCode bit ,@len int ,@c char(1) ,@val varchar(50) select @idx = 0 ,@inCode = 0 ,@val = '' set @len = len(@explain) while @idx <= @len begin select @idx = @idx + 1 ,@c = substring(@explain, @idx, 1) if @c = '#' and @inCode = 0 begin select @val = '' ,@incode = 1 end else if @c = '#' and @inCode = 1 begin select @inCode = 0 insert @codes select @val where @val <> '' end else if @inCode = 1 begin set @val = @val + @c end end select @codesCSV = coalesce(@codesCSV + ',' + code, code) from @codes return @codesCSVendgo--use the function to return Code comma seperated values (CSV)select rowid, dbo.fn_ExplainCodes(explain) CodeCSV from #junkgo--clean updrop function dbo.fn_ExplainCodesdrop table #junk |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-11-23 : 13:31:29
|
In the function, replace this: insert @codes select @val where @val <> '' with this: if not exists(select 'tg' from @codes where code = @val) insert @codes (code) select @val where @val <> '' btw, Its not nice to email contributors directly with your sql requests.Be One with the OptimizerTG |
 |
|
|
namasteall2000
Starting Member
20 Posts |
Posted - 2005-11-30 : 15:34:45
|
| Sorry about the email.I appreciate your help.Thanks |
 |
|
|
|