判断数字是否为奇数或偶数,可以参考下面2个自定义函数:
奇数:
SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- 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 ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- 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
运行结果:
原文链接:http://www.cnblogs.com/insus/p/10965118.html
本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728