Define user function and use it in select statement : Create Function « Store Procedure Function « SQL Server / T-SQL






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>
           
       








Related examples in the same category

1.Multi-Statement Table-Valued Functions
2.Inline Table-Valued Functions
3.Function Returning a Scalar Value
4.Define function to get last name