Solved

Search for a decimal and then remove it from string

Posted on 2012-04-06
3
516 Views
Last Modified: 2012-04-13
Hello,
I need a T-SQL Statement to search a string variable for a decimal and
then remove it.

@string = '1234.5'

I want @string to equal '12345'

If I do it this way:
Set @string = replace(@string, '.', '')

I would assume it would remove the decimal in the string.

What if @string = '12345', with no decimal?

Would it not work and set @string = NULL, due to not finding a decimal within the
replace statement?

Thanks.
0
Comment
Question by:Machinegunner
3 Comments
 
LVL 3

Accepted Solution

by:
Chris__W earned 250 total points
ID: 37817743
Hello,

No, it will work as you listed and will NOT nullify the string just because the searched for item is not there.

If @string is null it can still cause other issues, so if you haven't already accounted for that elsewhere here's the line:

Set @string = ISNULL(replace(@string, '.', ''),'');

Thanks,
Chris
0
 
LVL 42

Assisted Solution

by:EugeneZ
EugeneZ earned 250 total points
ID: 37821212
<What if @string = '12345', with no decimal?>
it will keep the same number: if it has something replace - it will replace

<Would it not work and set @string = NULL, due to not finding a decimal within the
replace statement?>
No, unless you wish -- it is extra code
0
 

Author Closing Comment

by:Machinegunner
ID: 37844031
thanks for the information and decided to leave it as it is and it took out the decimal, as stated, thanks
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question