Method for obtaining partial time in SQL: Sometimes, we may want to make some data statistics by month, by day, or by year. However, the actual data we save may be a very accurate occurrence time, which may be seconds. How to intercept a part of a time becomes a problem. There are two solutions: the most direct idea to use DatePart or Yea
Method for obtaining partial time in SQL: Sometimes, we may want to make some data statistics by month, by day, or by year. However, the actual data we save may be a very accurate occurrence time, which may be seconds. How to intercept a part of a time becomes a problem. There are two solutions: the most direct idea to use DatePart or Yea
Method for obtaining partial time in SQL:
Sometimes, we may want to make some data statistics on a monthly, daily, or yearly basis. However, the actual data we save may be a very accurate occurrence time, probably in seconds. How to intercept a part of a time becomes a problem.
There are two solutions:
The most direct idea is to use the DatePart or Year, Month, and Day functions.
CAST (
(
STR (YEAR (GETDATE () + '/' +
STR (MONTH (GETDATE () + '/' +
STR (DAY (GETDATE ()))
)
AS DATETIME
)
If you only need one year or one month, the previous one will be saved.
Method 2: Use time computing functions
For example, obtaining a month
DateAdd (month, DateDiff (month, 0, Coltime), 0)
The principle is that the database Time Value Coltime is reduced by 1900-1 to obtain the number of months in this period, and then the system adds-1-1 again.