判断数字是否为奇数或偶数
程序员文章站
2022-03-07 22:49:43
判断数字是否为奇数或偶数,可以参考下面2个自定义函数: 奇数: SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- -- Author: Insus.NET -- Blog: https://insus.cnblogs.com -- Create ......
判断数字是否为奇数或偶数,可以参考下面2个自定义函数:
奇数:
set ansi_nulls on go set quoted_identifier on go -- ============================================= -- author: insus.net -- blog: https://insus.cnblogs.com -- create date: 2019-06-02 -- update date: 2019-06-02 -- description: 是否为奇数 -- ============================================= create function [dbo].[svf_isodd] ( @number int ) returns bit as begin return case when @number % 2 <> 0 then 1 else 0 end end go
偶数:
set ansi_nulls on go set quoted_identifier on go -- ============================================= -- author: insus.net -- blog: https://insus.cnblogs.com -- create date: 2019-06-02 -- update date: 2019-06-02 -- description: 是否为偶数 -- ============================================= create function [dbo].[svf_iseven] ( @number int ) returns bit as begin return case when @number % 2 = 0 then 1 else 0 end end go
举例说明:
;with digits as ( select 0 as digit union all select digit + 1 from digits where digit < 9 ) select [digit],[dbo].[svf_iseven]([digit]) as [even],[dbo].[svf_isodd]([digit]) as [odd] from digits
运行结果: