In SQL server, sometimes when querying data, it is necessary to limit the query time range. At this time, you need to calculate the time as follows:
For example, to get the time of the previous day, you can use the following SQL statement.
SELECT GETDATE() –1, GETDATE();
As for why to write such SQL, first of all, it is very simple and convenient to write in this way, which is a little easier than using DateAdd function. In addition, it is often written in this way for temporary query.If the time granularity of adding and subtracting getdate is hours or minutes, you should pay special attention。 Put two SQL, you carefully taste the difference between the following two SQL
SELECT GETDATE() –1/24, GETDATE();
SELECT GETDATE() –1.0/24, GETDATE();
If you don’t pay attention to this detail, you will step on some pits. Only because 1 / 24 is an integer operation in SQL server, and the calculation result is 0, it must be converted to floating-point operation. Although there is no technical content, sometimes a lot of people accidentally dig pits, especially those who often switch between different types of databases, because Oracle database is different from SQL server. As shown below