The code is as follows: |
Copy code |
-- ===================================================== ====== -- Author: Insus. NET -- Create date: 2012-03-22 -- Description: determines whether the input date is today. The returned BIT data type. 1 is, 0 is not. -- ===================================================== ====== Create function [dbo]. [udf_IsToday] (@ datetime DATETIME) RETURNS BIT AS BEGIN -- Convert the DATE input by the user to the DATE data type DECLARE @ D1 as date = CAST (@ datetime as date) -- Convert the current time to the DATE data type DECLARE @ D2 as datetime = CAST (CURRENT_TIMESTAMP as date) DECLARE @ result INT = 0 -- Use another function to compare the date function. For more information about this function, see IF [dbo]. [DateTimeCompare] (@ D1, @ D2) = 0 SET @ result = 1 RETURN @ result END GO |
-- DateTimeCompare function ----
-- During SQL development, we often need to compare two dates. Therefore, Insus. NET writes a date comparison function to facilitate development. ---
The code is as follows: |
Copy code |
Create function DateTimeCompare ( @ DT1 DATETIME, @ DT2 DATETIME ) RETURNS INT AS BEGIN RETURN CASE WHEN @ DT1> @ DT2 THEN 1 WHEN @ DT2> @ DT1 THEN-1 ELSE 0 END END |
Demo
The code is as follows: |
Copy code |
SELECT [dbo]. [udf_IsToday] (CURRENT_TIMESTAMP) AS IsToday |