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 ())).
- Select Convert (varchar), GETDATE (), +) --Get the current date
- SELECT CONVERT (date,getdate (), $) --Get the current date
- SELECT CONVERT (Datetime,getdate (), $) --Get the current time and date
- SELECT CONVERT (VARCHAR (), GETDATE (), 108) --Get the current moment
SQL Compare Time Size