代码之家  ›  专栏  ›  技术社区  ›  J S

如何确定数字是偶数?

  •  17
  • J S  · 技术社区  · 16 年前

    老sql有点生锈了。

    你能帮忙吗?

    给定一个数字,如1,2或4,我需要确定它是偶数还是奇数,并做一些计算

    如何在sql(sql server 2000)中检测到这一点 谢谢

    9 回复  |  直到 16 年前
        1
  •  49
  •   Mark Byers    16 年前

    使用模数运算符 n % 2 . 如果数字为偶数,则返回0;如果数字为奇数,则返回1。

        2
  •  7
  •   Adriaan Stander    16 年前

    还可以使用sql server逐位运算符

    DECLARE @Int INT
    
    SELECT @Int = 103
    
    SELECT @Int & 1, @Int % 2
    
        3
  •  6
  •   priyanka.sarkar    16 年前

    select 
        num
        ,case when num % 2 = 0 then 'Even' else 'Odd' end as Status
    from @t 
    

    输出:

    num状态

    1   Odd
    2   Even
    3   Odd
    4   Even
    

    e、 如果数字是偶数(乘以1)或奇数(乘以2),则除以10得到余数

    declare @myNumber int ,@result int
    set @myNumber = 16 
    select  
        Result = 
        (case when @myNumber % 2 = 0 then @myNumber * 1 else @myNumber * 2 end) %10     
    

    6
    

    什么时候 @myNumber = 11

    结果

    2
    

    希望这有帮助

        4
  •  4
  •   Milan    12 年前

    IF @current_number % 2 = 0 SET @something = 1 
    

    IF @current_number % 2 = 0 exec sp_whatever
    
        5
  •  2
  •   Artelius    16 年前

    使用 modulus operator (%) .

    x % 2
    

    会告诉你x是偶数还是奇数。

        6
  •  1
  •   GandRalph    8 年前

    不是一个严肃的回答…但它是有效的。

    请保留以下内容给那些你不喜欢的人,他们只是想要一个快速的答案,而不想了解解决方案(即想作弊的人)。

    --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
    
        7
  •  1
  •   Seth    6 年前

    比如说一个桌子站。

    架构:

    身份证号码

    瓦尔查尔市

    州VARCHAR

    -- You can use any of the mentioned criteria to fetch the even ID.
    
    -- 1. MOD() Fucntion
    
    select distinct CITY from STATION as st where MOD(st.id,  2) = 0
    
    -- 2. % function
    
    select distinct CITY from STATION as st where st.id % 2 = 0
    
        8
  •  0
  •   demongolem    13 年前

    DECLARE @Int INT
    
    SELECT CASE WHEN @Int&0x0001<>0 THEN 'ODD' ELSE 'EVEN' END
    
        9
  •  0
  •   Cleptus    6 年前

    %

    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
    
        10
  •  -1
  •   R. Oosterholt Alex Vayda    9 年前
    USE AdventureWorks;
    GO
    
    SELECT BusinessEntityID,
        CASE BusinessEntityID % 2
        WHEN 0 THEN 'Even' ELSE 'Odd' END AS "Status"
    FROM HumanResources.Employee;
    GO