SQL Compare Time Size

Source: Internet
Author: User
Tags string format time and date

Compare the time size of a string type

The time in the database is varchar type, MySQL uses curdate () to get the current date, and SQL Server gets the current date by GETDATE ()

1. Use strings directly to compare

Note: To ensure that the two data types are exactly the same, an exception

For example a: "2016-09-01", if the B data is: "2016-9-2", then can not be compared

2. Through the type conversion function convert (),

Note: To ensure that the time value of the string is correct, otherwise exception,

For example, "2016-2-30", February no 30th, so when the conversion will be abnormal

CONVERT (<data_ type>[length], <expression> [, Style])

1) data_type The data type defined for the SQL Server system, the user-defined data type cannot be used here.
2) length is used to specify how long the data is, and the default value is 30.
3) Use the style option of the CONVERT () function to display the date and time in a different format. The style is when you convert Datatime and smalldatetime data to strings

The selected conversion style numbers provided by the SQL Server system have different output formats for different style numbers. If you use this method to judge a field,

The string format of the date field that is deposited is as long as it is possible for SQL Server to complete the conversion of the date, not necessarily as strict as in the first method.

or Adddate field, such as to compare whether it is greater than the current date, you can write this: where (convert (varchar, adddate) >= convert (varchar, GETDATE ())).

      1. Select Convert (varchar), GETDATE (), +) --Get the current date
      2. SELECT CONVERT (date,getdate (), $) --Get the current date
      3. SELECT CONVERT (Datetime,getdate (), $) --Get the current time and date
      4. SELECT CONVERT (VARCHAR (), GETDATE (), 108) --Get the current moment

SQL Compare Time Size

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.