SQL statement to determine whether the statement is today

Source: Internet
Author: User
Tags current time datetime
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.