2009-11-26 13 views
17

Bit arrugginito sul vecchio sql.Come posso determinare è cifra Numero pari?

Potete essere d'aiuto?

Dato un numero, ad esempio 1 o 2 o 4, devo determinare se è pari o dispari e fare qualche calcolo a seconda se pari o dispari.

Come si rileva che in SQL (SQL Server 2000) grazie mille

+1

c'è una 'funzione di mod' di qualche tipo in TSQL? – Jason

+1

@ Jason: Tsql supporta l'operatore modulo% – peacedog

risposta

43

Utilizzare l'operatore modulo n % 2. Restituisce 0 se il numero è pari e 1 se il numero è dispari.

+2

fai qualcosa come Dichiarare @myNumber int set @ myNumber = 1 se Select (@ myNumber% 2) = 1 \t \t print 'anche' \t \t altro \t \t stampa 'dispari' –

+1

Sì, o se si desidera farlo all'interno di una selezione, utilizzare il "caso ... quando ... quindi" costruire. –

7

È inoltre possibile utilizzare il server sql BIT operatori WISE

DECLARE @Int INT 

SELECT @Int = 103 

SELECT @Int & 1, @Int % 2 
+0

per qualche motivo non riesco a far funzionare quello che devo calcolare se un numero è pari o dispari quindi dividere per 10 e ottenere solo il promemoria DECLARE @myNumber INT, @ result int SELECT @myNumber = 16 SELECT @myNumber & 1, @myNumber% 2 se @ myNumber = 0-- anche set di risultati @ = @ myNumber x 1-- moltiplicare per 1 altro set risultato @ = @ myNumber x 2-- moltiplicare per 2 - - poi dividere per 10 e --example se il risultato = 5.2 basta prendere il promemoria (in questo caso 2) selezionare risultato –

+0

Prova questo dichiarare @myNumber int, int @ risultato set @myNumber = 16 selezionare \t \t Risultato = \t (caso quando @myNumber% 2 = 0 allora @myNumber * 1 altro @myNumber * 2 estremità)% 10 –

+0

DECLARE @myNumber INT, @ int risultato SELEZIONATE @myNumber = 16 SELEZIONA @myNumber & 1, @myNumber% 2 se @ myNumber = 0-- anche set di risultati @ = @ myNumber * 1-- moltiplicare per 1 altro impostato @result = @ mio numero * 2-- moltiplicare per 2 selezionare @result% 10 –

4

tabella declare @t (num INT) inserto in @t selezionare 1 union all select 2 union all select 3 union all select 4

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

uscita:

num Stato

1 Odd 
2 Even 
3 Odd 
4 Even 

ad es. Se il numero è pari (moltiplicare per 1) o dispari (moltiplicare per 2) poi dividere per 10 ed ottenere il resto

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

Risultato

6 

quando @myNumber = 11 poi

Risultato

2 

Spero che questo aiuti

0

È possibile controllare il 1-bit del valore esadecimale del numero. Se quel bit è attivo, allora è strano.

DECLARE @Int INT 

SELECT CASE WHEN @Int&0x0001<>0 THEN 'ODD' ELSE 'EVEN' END 
4

Sto usando la stessa cosa in MS SQL SP come segue:

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

- o -

IF @current_number % 2 = 0 exec sp_whatever 
-1
USE AdventureWorks; 
GO 

SELECT BusinessEntityID, 
    CASE BusinessEntityID % 2 
    WHEN 0 THEN 'Even' ELSE 'Odd' END AS "Status" 
FROM HumanResources.Employee; 
GO 
0

Diciamo che per una stazione tavolo.

schema:

NUMERO ID

CITY VARCHAR

STATO 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 
Problemi correlati