Results 1 to 4 of 4
  1. #1
    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. #2
    Join Date
    Nov 2004
    Posts
    1,427
    Provided Answers: 4
    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.
    With kind regards . . . . . SQL Server 2000/2005/2012
    Wim

    Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
    Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages

  3. #3
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    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.

    You'll be glad you did.
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  4. #4
    Join Date
    May 2012
    Posts
    1
    Quote Originally Posted by blindman View Post
    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.

    You'll be glad you did.
    agree with you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •