Solved

Search for a decimal and then remove it from string

Posted on 2012-04-06
3
511 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
Comment Utility
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
Comment Utility
<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
Comment Utility
thanks for the information and decided to leave it as it is and it took out the decimal, as stated, thanks
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

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…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now