# Thread: Replace logic is required

1. Registered User
Join Date
Mar 2009
Posts
5

## Unanswered: Replace logic is required

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+''','''+@S PACE+'''), '''+@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

2. Registered User
Join Date
Nov 2004
Posts
1,428
You are experiencing the denormalisation hell.

What I would do is convert the string to a DECIMAL, INT and DECIMAL and compare those with the DECIMAL, INT and DECIMAL from another record.

This may seem like this is more work, but I think resolving this problem with string manipulations in SQL will take A LOT longer to get it 100% right.

3. World Class Flame Warrior
Join Date
Jun 2003
Location
Ohio
Posts
12,595
Since you have to handle repeating zeros right of the decimal (4.02000....) you'll need to create a user-defined function to handle this parsing.

Before you start coding, however, spend a day tracking down the person who designed the system and castrate them with a pair of needle-nosed pliers.

4. Registered User
Join Date
May 2012
Posts
1
Originally Posted by blindman
Since you have to handle repeating zeros right of the decimal (4.02000....) you'll need to create a user-defined function to handle this parsing.

Before you start coding, however, spend a day tracking down the person who designed the system and castrate them with a pair of needle-nosed pliers.