1

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

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

Output:

5000
RandomUser
  • 1,843
  • 8
  • 33
  • 65
  • See http://stackoverflow.com/questions/13253259/sql-server-replace-command-with-wildcard and maybe http://stackoverflow.com/a/31693412/3832970. – Wiktor Stribiżew Oct 18 '16 at 07: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)` – LukStorms Oct 18 '16 at 07:49
  • @lukstorms thanks for a cleaner solution – RandomUser Oct 18 '16 at 08:34

1 Answers1

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
Meena
  • 685
  • 8
  • 31