1

How can perform below shown JavaScript RegEx replace in SQL Server?

var str = "$5,000";
console.log(str.replace(/[$,]/g, ""));

Output:

5000
3
  • See stackoverflow.com/questions/13253259/… and maybe stackoverflow.com/a/31693412/3832970. Commented Oct 18, 2016 at 7:31
  • 2
    No regex needed in this case. SQL server has the money type, and you can cast that to an int or a bigint for large numbers. cast(cast('$5,000' as money) as int) Commented Oct 18, 2016 at 7:49
  • @lukstorms thanks for a cleaner solution Commented Oct 18, 2016 at 8:34

1 Answer 1

1

Try this

declare @str money
set @str= cast(cast('$5,000' as money) as int)

Or else if you especially want to use regular expression, you can try the below,

DECLARE @Str varchar(100)
SET @Str = '$5,000' 
set @Str = STUFF(@Str, PATINDEX('%[$,]%', @Str),1, '')
select @str
Sign up to request clarification or add additional context in comments.

2 Comments

the result is '5,000' and I want the result as '5000'
Just do a double replace replace(replace(@Str,'$',''),',','') then. PATINDEX only gives the first occurrence of a match.

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.