Solved

Search for a decimal and then remove it from string

Posted on 2012-04-06
3
513 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

932 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

11 Experts available now in Live!

Get 1:1 Help Now