Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

execution modulo on a large number in t-sql

I need to calculate the remainder of one number divided by another. for instance with these numbers:

271011240311350356232122 % 97

When I just want to do that in a sql statement, it works like a charm:

select 271011240311350356232122 % 97;

But when I have that large number in a varchar variable, I can't seem to get the job done. I can't convert it into a int or even bigint, because it's too large. I can't convert it into a real because you can't use the modulo operator on a real number.

Any ideas...?

like image 211
ErikL Avatar asked Mar 22 '23 11:03

ErikL


1 Answers

If it is too big for bigint you can use NUMERIC(38,0)

DECLARE @Num VARCHAR(38) = '271011240311350356232122'
SELECT CAST(@Num AS NUMERIC(38,0)) % 97
like image 59
Martin Smith Avatar answered Mar 31 '23 22:03

Martin Smith