Execution modulo on a large number in t-sql

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

271011240311350356232122% 97

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

select 271011240311350356232122% 97;

But when I have such a large number in the varchar variable, I cannot do this work. I can not convert it to int or even bigint because it is too big. I cannot convert it to real, because you cannot use the modulo operator on a real number.

Any ideas ...?

+4
source share
1 answer

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

DECLARE @Num VARCHAR(38) = '271011240311350356232122'
SELECT CAST(@Num AS NUMERIC(38,0)) % 97
+7

All Articles