SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Replace logic is required
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sql01
Starting Member

2 Posts

Posted - 05/10/2012 :  00:38:36  Show Profile  Reply with Quote
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

Edited by - sql01 on 05/10/2012 00:42:26

DonAtWork
Flowing Fount of Yak Knowledge

2111 Posts

Posted - 05/10/2012 :  10:02:00  Show Profile  Reply with Quote
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 - 05/10/2012 :  10:54:30  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

2111 Posts

Posted - 05/10/2012 :  13:30:54  Show Profile  Reply with Quote
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

India
22460 Posts

Posted - 05/14/2012 :  09:23:38  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000