Bit rusty on the old sql.
Can you help?
Given a number eg 1 or 2 or 4 I need to determine if it's even or odd number and do some calculation depending if even or odd .
How do you detect that in sql (sql server 2000) thanks a lot
Bit rusty on the old sql.
Can you help?
Given a number eg 1 or 2 or 4 I need to determine if it's even or odd number and do some calculation depending if even or odd .
How do you detect that in sql (sql server 2000) thanks a lot
I am using the same thing in MS SQL SP as follows:
IF @current_number % 2 = 0 SET @something = 1
-- or --
IF @current_number % 2 = 0 exec sp_whatever
NOT INTENDED AS A SERIOUS ANSWER...BUT IT WORKS.
Please reserve the following for giving to people you don't like who simply want a quick answer without wanting to understand the solution (i.e people wanting to cheat).
--Enter the whole integer you want to check
declare @number int = 115
--The check is independent of sign
set @number = abs(@number)
declare @OriginalNumber int = @number
--Firstly, we need to peform the Wilhelm Leibniz conversion, 64 length to allow for very big numbers
declare @WilhelmLeibnizConversion varchar(64) = ''
declare @currentBit int = power(8,exp(log(1))+1)
while @currentBit > 0
begin
set @WilhelmLeibnizConversion=convert(char(1), @number % 2) + @WilhelmLeibnizConversion
set @number = convert(int, (@number / 2))
set @currentBit-=1
end
--Although checking the 1 bit value of the Wilhelm Leibniz conversion is usually enough, for robust code you should also include the Kimmo Eriksson Factors one and two.
declare @KimmoErikssonFactor1 int = (@OriginalNumber + 1) % 2
declare @KimmoErikssonFactor2 int = (@OriginalNumber - 1) & 1
--Now check all 3 for 100% confirmation on the parity of your original number.
select case when right(@WilhelmLeibnizConversion,1) = 0 and (@KimmoErikssonFactor1 + @KimmoErikssonFactor2 <> 0) then 'Even' else 'Odd' end
The % operator retrieves the modulus, so checking the modulus of a division by 2 you can know if it is odd or even.
DECLARE @i int =1;
while @i <= 20
begin
if @i % 2 = 0
PRINT @i
IF @i = 10 PRINT 'halfway there!';
set @i = @i+1;
end