Tuesday 29 November 2016

Find the prime number using sql script

With the help of below sql script we will find the number is prime number or not.
DECLARE @Number AS INT = 7

SELECT
    CASE WHEN COUNT(DISTINCT num1.number) > 0
                THEN cast(@Number as varchar(10))+  ' is Not a Prime Number'
                ELSE cast(@Number as varchar(10)) +  ' is Prime Number' END [Check Prime]
FROM MASTER..SPT_VALUES AS num1
WHERE num1.Number > 1
      AND  num1.Number < @Number
      AND  @Number % num1.Number = 0

See the output
  
See with another example





Popular Posts