Mega Code Archive

 
Categories / MSSQL / Store Procedure Function
 

Define user function and use it in select statement

1> 2> CREATE FUNCTION dbo.ufnIsOdd (@n int) 3> RETURNS bit 4> AS 5> BEGIN 6>     RETURN (@n % 2) 7> END 8> GO 1> 2> DECLARE @numb int 3> SET @numb = 6 4> SELECT @numb 'Number', dbo.ufnIsOdd (@numb) 'Is_odd = 1' 5> SET @numb = 7 6> SELECT @numb 'Number', dbo.ufnIsOdd (@numb) 'Is_odd = 1' 7> 8> GO Number      Is_odd = 1 ----------- ----------           6          0 (1 rows affected) Number      Is_odd = 1 ----------- ----------           7          1 (1 rows affected) 1>