Solved

Search for a decimal and then remove it from string

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

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…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
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…

785 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