SQL SERVER Monthly, quarterly, yearly statistics and common query statements Summary

Source: Internet
Author: User
Tags getdate

First, SQL SERVER Monthly, quarterly, annual statistical query

--This day

SELECT *from dbo. TableName WHERE DATEDIFF (day,timefield,getdate ()) = 0;

--This week

SELECT *from dbo. TableName WHERE DATEDIFF (week,timefield,getdate ()) = 0;

--This month

SELECT *from dbo. TableName WHERE DATEDIFF (month,timefield,getdate ()) = 0;

--This quarter

SELECT *from dbo. TableName WHERE DATEPART (QQ, Timefield) = DATEPART (QQ, GETDATE ()) and DATEPART (yy, Timefield) = DATEPART (yy, GETDATE ());

--this year

SELECT *from dbo. TableName where DATEDIFF (year, timefield,getdate ()) = 0;

Note: TableName is the table name for the query, timefiled is the Time field for the query

Ii. SQL SERVER Determines whether tables, fields, stored procedures, triggers exist

--Determine if the table exists

SELECT *from sys.objects WHERE object_id = object_id (' table name ') and OBJECTPROPERTY (object_id, N ' isusertable ') = 1

-OR

SELECT *from sys.objects WHERE object_id = object_id (' table name ') and type= ' U '

--Determine if a field exists

SELECT *from sys.columns WHERE object_id = object_id (' field name ') and Name = ' No '

--Determine if the stored procedure exists

SELECT *from sys.objects WHERE object_id = object_id (' Stored procedure name ') and

OBJECTPROPERTY (object_id, N ' isprocedure ') = 1

-OR

SELECT *from sys.objects WHERE object_id = object_id (' Stored procedure name ') and type= ' P '

--Determine if the trigger exists

SELECT *from sys.objects WHERE object_id = object_id (' Trigger name ') and OBJECTPROPERTY (object_id, N ' istrigger ') = 1

-OR

SELECT *from sys.objects WHERE object_id = object_id (' Trigger name ') and type= ' TR '

Third, table copy statement

--Requires that table 2 does not exist and is automatically created when queried

Select field1,field2 from Table1 to Table2

--Requires that table 2 must exist and can be inserted into constants

Insert into table2 (field1,field2) Select Field1,field2 from table1

Iv. Related Updates

UPDATE Table1 SET t1f1 = t2f1, t1f2 = t2f2, t1f3 = t2f3 from Table1, Table2 WHERE table1.key = Table2.key

SQL SERVER Monthly, quarterly, yearly statistics and common query statements Summary

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.