I'm sure you are all interested in the performance side of this issue.
Stress testing of cast-as-money-then-as-bigint approach.
Filling test data in a quick-and-dirty way:
;with ids as
(
select cast(abs(checksum(newid())%1000) as varchar) [1]
,cast(abs(checksum(newid())%1000) as varchar) [2]
,cast(abs(checksum(newid())%1000) as varchar) [3]
union all
select cast(abs(checksum(newid())%1000) as varchar)
,cast(abs(checksum(newid())%1000) as varchar)
,cast(abs(checksum(newid())%1000) as varchar)
from ids
)
, enum as (
select [1]+','+[2]+','+[3] n, row_number() over(order by (select 0)) rn
from ids
where len([1]) = 3 and len([2]) = 3 and len([3]) = 3
)
select rn, n
into temp.[money]
from enum
where rn <= 1000000
option (maxrecursion 0)
Now it's time to stress test our initial approach when we want to remove all the commas from a string:
select min(mn), max(mn), avg(mn)
from (
select cast( replace(n,',','') as bigint) mn
from temp.money
) m
It takes on average 10.6 seconds to complete!
And, finally, this via-money approach:
select min(mn), max(mn), avg(mn)
from (
select cast(cast(n as money) as bigint) mn
from temp.money
) m
It takes on average 1.5 seconds to complete!