Use the Convert function in SQL to check the date query

xiaoxiao2021-03-06  72

I have encountered this situation, there is a PublishTime (DateTime, 8) field in the Meeting table of the database, used to store a meeting time, due to the specific time to find out, the format is YYYY-MM-DD HH: MM: SS, and our query is made through YYYY-MM-DD, that is, query all meetings of a certain day, if you pass Select * from meeting where publishtime = @publishtime is YYYY-MM-DD The format) statement will not be able to get the correct result, such as the meeting information we have to query on December 1, 2004, you can't get the results of the query in the input 2004-12-01, and this inquiry can not request the specific HH : mm: ss.

At this point we need to use the Convert function, which can convert an expression of a data type into another data type. This will first convert the DateTime type within the database to a CHAR type, because the input parameters are 10 Bit, so write

SELECT *, Publish, 121) = @ PublishTime, so that the database gets the information in the database to the 10-bit character in the YYYY-MM-DD format when the database gets the parameters, as long as You can return to the query result as the parameter. 121 in Convert refers to the 4-digit year including the number of bits when converting the DateTime type to a CHAR type.

The above is a summary of the small difficulties I have encountered in the development. If you write an incorrect place, you are welcome!

转载请注明原文地址:https://www.9cbs.com/read-92112.html

New Post(0)