0

Created simple function to remove few special character but it is returning with junk values:

ALTER FUNCTION [dbo].[fnRemoveInvalidChars] (@Temp VARCHAR(300))
RETURNS VARCHAR(300)
BEGIN

   SELECT @Temp = REPLACE(REPLACE(@Temp, ',', ''), '.', '') /* removing , and .*/
   return @Temp;
END

Returning values are like below:

58,871,300.00   => 5.88713e+007
55,146,000.00   => 5.5146e+007
8,296,000.00    => 8.296e+006

Important to note that, the value i am passing can be Varchar, decimal,int or float and because of that it is not working correctly.

6
  • 2
    Why you're using set @Temp = CONVERT(VARCHAR,@Temp);? Do you just want to remove '.' and ',' from a string? Commented Sep 10, 2019 at 7:28
  • @Temp is can be a Varchar, decimal or float... we can remove that if needed Commented Sep 10, 2019 at 7:29
  • 2
    Nope, you already declare it as a varchar parameter. Commented Sep 10, 2019 at 7:31
  • 1
    Your function is fine. It must be how you are passing the value to the function. How is the function called ? Commented Sep 10, 2019 at 8:20
  • 1
    share your sample input which is not working.. Commented Sep 10, 2019 at 10:33

1 Answer 1

1

First, alter your function as (although your function is working just fine):

ALTER FUNCTION [dbo].[fnRemoveInvalidChars] (@Temp VARCHAR(300))
RETURNS VARCHAR(300)
BEGIN
  DECLARE @Buff VARCHAR(300);
  SELECT @Buff = REPLACE(REPLACE(@Temp, ',', ''), '.', '');
  RETURN @Buff;
END

Then you can use it as

SELECT [dbo].[fnRemoveInvalidChars]('58,871,300.00')
UNION
SELECT [dbo].[fnRemoveInvalidChars]('55,146,000.00')
UNION
SELECT [dbo].[fnRemoveInvalidChars]('8,296,000.00');

The results is:

5514600000
5887130000
829600000

Demo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.