When designing a database tutorial, you must note that the time field is int (11). In this way, a digital date timestamp is stored in the database, we can use the mktime function to find the timestamp of the current date and add or subtract it. The following figure shows the instance.
// One month
CopyCode The Code is as follows: $ lastmonth = mktime (
SQL Server Date Calculation
Usually, you need to get the current date and calculate some other date, for example, your program may need to judge the first or last day of one months. Most of you probably know how to split dates (year, month, day, etc.), and then just use the
Time turns into years, days and secondsSelect Date_format (now (), '%y%m%d%h%i%s ')Time turns into month and daySelect Date_format (now (), '%y%m%d ')This time last yearSelect Date_add (now (), Interval-1 year)This time last monthSelect Date_add (now (), Interval-1 month)Yesterday at this timeSelect Date_add (now (), Interval-1 Day)After 7 daysSelect Date_add (now (), Interval 7 day)//an hour ago Select Da
Label:SQL DateWhen we work on dates, the hardest task is to ensure that the date you insert is formatted to match the format of the date column in the database. As long as the data contains only the date parts, running the query will not be a problem. However, if time is involved, the situation is a little more complicated. Before we discuss the complexity of the
SQL SERVER datetime type is accurate to 3 milliseconds.7) Last day of the monthSELECT DATEADD (Ms,-3,dateadd (Month,datediff (Month,0,getdate ()) +1,0)8) Number of days of the monthi) SELECT Day (DATEADD (Ms,-3,dateadd (Month,datediff (Month,0,getdate ()) +1,0)))II) SELECT
When designing a database tutorial, you must note that the time field is int (11). In this way, a digital date timestamp is stored in the database, we can use the mktime function to find the timestamp of the current date and add or subtract it. The following figure shows the instance.// One monthCopy codeThe Code is as follows:$ LastMonth = mktime (date ('H'),
In this article, the date obtained by getdate () is composed of two parts: today's date and then time: Select getdate ()
Use datename () to obtain the corresponding year, month, and day, and then connect them:
Select datename (year, getdate () + '-' + datename
(Month, getdate () + '-' + datename (day, getdate ())
In ad
days that will be met after the date.ROUND (date, 'month/year') is rounded to get a new date. The retention period is Month and year.SESSIONTIMEZONE returns the time zone of the current session. The return type is a character string of the time zone offset or time zone title. If specified, the format is the same as th
(Dd,6-datepart (day,getdate)), getdate)), 0) 13. Last day of the yearSELECT DateAdd (Ms,-3,dateadd (yy, DATEDIFF (Yy,0,getdate ()) +1, 0)). Date and time functions in SQL Server1. Current system date, timeSelect GETDATE () 2. DateAdd returns a new datetime value based on adding a period of time to the specified dateExample: Add 2 days to the dateSelect DATEADD (
subtracting 3 milliseconds from this example on the last day of one months. One thing to keep in mind is that the time in SQL Server is exactly 3 milliseconds. That's why I need to subtract 3 milliseconds to get the date and time I want.SELECT DateAdd (Ms,-3,dateadd (mm, DATEDIFF (Mm,0,getdate ()), 0))The time portion of the calculated date contains the time at
The number of SQL statements on the first and last day of the month is taken online, either through character interception, or through functions, individuals or rather by using built-in functions to handle,But look at the use of the online function to take the first day and the last day of the SQL statement almost like the following, in fact, there is a problem,
I have added SQL time and date functions to my favorites. If you need these functions, please refer to the following section.
I have added SQL time and date functions to my favorites. If you need these functions, please refer to the following section.
① Getdate (): returns the current system
Date and time functions in SQL Server
1. Current system date, time
Select GETDATE ()
2. DateAdd returns a new datetime value on the basis of adding a period of time to the specified date
For example: Add 2 days to date
Select DATEADD (day,2, ' 2004-10-15 ')--back: 2004-1
Tags: ar strong sp C on r time EF BSTime comparison in SQL ServerExample:Select COUNT (*) from table where DATEDIFF ([second], ' 2004-09-18 00:00:18 ', ' 2004-09-18 00:00:19 ') > 0Description Select DATEDIFF (Day, time1, time2) corresponds to the following example statementSelect DATEDIFF (Day, ' 2010-07-23 0:41:18 ', ' 2010-07-23 23:41:18 ') time1 > time2 is negative; Time1 time2 are positive; [Day]: will only compare 2010-07-23 ignored 0:4
The field type in the database is datetime.
1. Use the java. util. Date class
Import java. SQL. connection;
Import java. util. date;
Import java. SQL. resultset;
Import java. SQL. sqlexception;
Import java. SQL. statement;
the current date to the last day of the month. Please note: This example and the other examples in this article will only use the DateDiff and DATEADD functions to calculate the date we want. Each example will get the date you want to calculate by calculating the previous time interval and then adding and reducing it.
to the last day of the month. Please note: This example and the other examples in this article will only use the DateDiff and DATEADD functions to calculate the date we want. Each example will get the date you want to calculate by calculating the previous time interval and then adding and reducing it.
This is the SQL
, getdate), 0)
13. Last day of the yearSelect dateadd (MS,-3, dateadd (YY, datediff (YY, 0, getdate () + 1, 0 )).
Date and time functions in SQL Server1. Current System Date and TimeSelect getdate ()
2. dateadd returns a new datetime value based on a period of time added to the specified date.For example, add 2 days to the dateSelect dateadd (day, 2, '20
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service