DateTime data type in SQL Server

zhaozj2021-02-16  62

I may laugh at this title. You will laugh, because you think this thing is too small, but also uses a title? ! I will laugh, because I am sure that you will change your ideas after you finish.

First of all, I introduce a question: How to compare with a field value of a DateTime type? In fact, this problem seems to be easy, but there is a problem: 1, in general, the date value entered by the user is YYYY / MM / DD, without time, minute, seconds; 2, the date value stored in the database is tape Time, minute, second.

So I must assume that we call two days, the year, month, day is equal.

Two methods: 1. With this assumption, you will have this idea: take the two days, months, and the day, it will be compared, and the three are equal (you can also make it according to the unified format. A string is more comparison). 2, the above method is feasible. But you will find almost a date function! You may wish to press the following ideas: If you only specify the date, the time is default 12:00 AM (midnight) (from: SQL Server online help). Assuming the date we have to check is 2004/7/10, it is actually between 00:00:00 to 23:59:59 this day. Between mentioning, we will think of Between ... and! ! As long as BetWeen 2004/7/10 and 2004/7/11 can be. Specifically: Between Cast ('2004-7-10' as datetime) and dateadd (Day, 1, Cast ('2004-7-10' as datetime)). But this method has a small problem, which will also find 7/11 0 things, and this is actually not counted for 7/10. If you still want to seek fine, you can only use ...> = ... and ... <.... (There is no need to in general)

After discussion, think about it, we can naturally use Between ... and then demonstrate the versatility of the above method. Note: With the above discussion, you must understand that the date will be added one day later, otherwise it will not be found.

Ok, today's "small matter masterpiece" is here. Good luck !! 7/10

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

New Post(0)