SQL Judging time condition

Source: Internet
Author: User

1. Determine whether the date is equal

Select *  from Table where cast (convert(varchar(asdatetime)='  2010-10-01'

Judgment logic: The Time field is now truncated by the CONVERT function, and then converted to datetime using the cast function.

2. Is the degree of interpretation equal to seconds?

Set @transfrom_date= CAST('1900-1-1'+ CONVERT(varchar( -),'Time Field',108) as datetime)Select *  from Table where  CAST('1900-1-1'+ CONVERT(varchar( -),'Time Field',108) as datetime)=@transfrom_date

Judgment logic: The time condition statement is now converted to degrees minutes and seconds, and then the query is executed and the time field of the comparison is converted to the same month or seconds of the day.

3. Determine whether the same year, month, day

Select *   from Table --yearwhere DateDiff( Year,'Time Field','Time Conditions')=0--Monthwhere DateDiff(Month,'Time Field','Time Conditions')=0
--date where DateDiff (day,' time field ',' time Condition ')=0


SQL Judging time condition

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.