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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Replace logic is required

Author  Topic 

sql01
Starting Member

2 Posts

Posted - 2012-05-10 : 00:38:36
Hello,

The string column value looks like as below. Each value has a size of 15 withing a string

'2.2020 30 4.0000'

The column value should match with user input as below. The result should show equal when it is compared. Currently, it results not equal since it is a string comparision. The last digit '0' needs to be ignored for decimal values.

'2.202 30 4.0'


I need to handle the decimal values in such a way, if staring value with '.' and last digit is 0 then replace with space ''.
so, it should look like

'2 2 2 30 4 ' = '2 2 2 30 4 '
When this string is compared, it results in EQUAL.


I tried the below logic, which even replaces the interger value like 30 to 3 and 3000 to 3 and results in equal which is incorrect.
RTRIM(REPLACE(REPLACE(RT1.rate,'''+@DOT+''','''+@SPACE+'''), '''+@ZERO+''', '''+@SPACE+''')) = '''+REPLACE(REPLACE(@Rate,'.',' '), '0', ' ')+''' '

Ex:'2.2020 300 4.00' = '2.20200 30 4.0'

After replace, string looks like
Ex:'2 2 2 3 4 ' = '2 2 2 3 4 '

It results as EQUAL which is incorrect. :( I need only decimal value to be replaced not integer.

I am looking for a single string replace logic, any help would be greatly appreciated!!


Thank you





sql01

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2012-05-10 : 10:02:00
My first question would be, WHY would you store the data in this manner?

Second, it is unclear exactly what you are comparing.

if your "Values" are separated by spaces, then parse the strings by space and compare each value. I am guessing that you should convert each string "value" to a decimal for comparison.










How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

sql01
Starting Member

2 Posts

Posted - 2012-05-10 : 10:54:30
Thank yoy for the reply...

It is based on the structure, the entire value is getting stored as a string. :(

Each value size is 15 within a string.

I am trying to manage in a single statement.
Where this can be achieved using a loop by comparing each value in a string but there is a performance issue after using loop.

sql01
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2012-05-10 : 13:30:54
You may have no choice, unless visakh can come up with a nice cross apply fix for you.









How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-05-14 : 09:23:38
1 Read about normalization
2 Split the values for columns and variable value and compare
3 Another method, Try the below

declare @val1 varchar(100), @val2 varchar(100)
select @val1='2.2020 30 4.00000',@val2='2.20200 30 4.0'

select
case when substring(val1,1,len(val1)-patindex('%[^1-9]%',reverse(val1))+2) = substring(val2,1,len(val2)-patindex('%[^1-9]%',reverse(val2))+2)
then 'equal' else 'not equal' end from
(
select replace(replace(rtrim(replace(replace(@val1,' ','^'),'0',' ')),' ',''),'^',' ') as val1,
replace(replace(rtrim(replace(replace(@val2,' ','^'),'0',' ')),' ',''),'^',' ') as val2

) as t1


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -