判斷數字是否爲奇數或偶數,能夠參考下面2個自定義函數:git
奇數:ide
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
舉例說明:spa
;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
運行結果:code