Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
761 views
in Technique[技术] by (71.8m points)

tsql - Mod negative numbers in SQL just like excel

I'm having trouble replicating the mod function in SQL sever.

In excel, mod (-3, 7) = 4. But in SQL, -3 % 7 = -3

Am I using % wrong, or does SQL do mod differently?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This will give a result between 0 and n - 1 for both positive and negative values of x:

((x % n) + n) % n

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...