if an int odd : Utility function « Procedure Function « SQL Server / T-SQL Tutorial






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

(1 rows affected)
Number      Is_odd = 1
----------- ----------
       NULL       NULL

(1 rows affected)
Number      Is_odd = 1
----------- ----------
          6          0

(1 rows affected)
1>
2> drop FUNCTION dbo.ufnIsOdd;
3> GO
1>
2> drop PROCEDURE usp_NumberIsOdd;
3> GO








21.6.Utility function
21.6.1.Scalar Functions
21.6.2.Format a date
21.6.3.linear_max() Scalar Function
21.6.4.The linear_min Scalar Function
21.6.5.Changing Character Values to Proper Case with user-defined functions
21.6.6.if an int odd