SQL Server calculates a part of the time difference function "Go"

Source: Internet
Author: User

The DateDiff () function and the GETDATE () function are needed when you are doing SQL Server development and sometimes need to get the data in the table today, yesterday, this week, last week, this month, and last month.
DATEDIFF (DatePart, StartDate, EndDate)
Explanation: Calculating the time difference
Datepare Value: Year | Quarter | Month | Week | Day | Hour | Minute | Second | Millisecond
StartDate: Start date
EndDate: End Date
GetDate ()
Explanation: Get the current system date

In the following example, the table name is TableName and the condition field is named Inputdate
Query today

SELECT * FROM TableName where DATEDIFF (Day,inputdate,getdate ()) =0
Query yesterday

SELECT * FROM TableName where DATEDIFF (Day,inputdate,getdate ()) =1
Query this week

SELECT * FROM TableName where DATEDIFF (Week,inputdate,getdate ()) =0
Query last week

SELECT * FROM TableName where DATEDIFF (Week,inputdate,getdate ()) =1
Check this month

SELECT * FROM TableName where DATEDIFF (Month,inputdate,getdate ()) =0
Query last month

SELECT * FROM TableName where DATEDIFF (Month,inputdate,getdate ()) =1

Reprint Address: http://www.devdo.net/sql-server-query-date.html

Merge two tables simultaneously query

For example:

Table A has field ID name age

Table B has field ID sex address

Query Sql:select * FROM (select Id,name,age,null sex,null address from a UNION ALL select Id,null name,null age,sex,address From B)

Principle: Let two tables have the same field name, null sex==null as sex, because the a table has no sex column, so we define a null column. This will have the same column as the B table.

UNION ALL is a query for all data displayed for two tables.

SQL Server calculates a part of the time difference function "Go"

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.