Note: copy a table (only copy structure, source table name: a new table name: B)Select * into B from a where 1
Description: copy a table (copy data, source table name: A target table name: B)Insert into B (a, B, c) Select D, E, F from B;
Note: DisplayArticle, Submitted and last reply timeSelect a. Title, A. username, B. adddate from Table A, (select max (adddate) adddate from table where table. Title = A. Title) B
Description: External join query (table name 1: Table A name 2: B)Select a.
Note: copy a table (only copy structure, source table name: a new table name: B)Select * into B from a where 1
Description: copy a table (copy data, source table name: A target table name: B)Insert into B (a, B, c) Select D, E, F from B;
Note: DisplayArticle, Submitted and last reply timeSelect a. Title, A. username, B. adddate from Table A, (select max (adddate) adddate from table where table. Title = A. Title) B
Description: External join query (table name 1: Table A name 2: B)Select a.
To add fields automatically, rewrite them. The automatically numbered field that is frequently used in access. After being imported to MSSQL, it is not an auto-incrementing int and needs to be set manually, change the "no" of the Imported Automatic number field to "yes", and the "Seed" and "incremental" are both "1" to be automatically numbered.
All default values are lost. Mainly numeric and date types
All now (), time (), date () must be changed to getdate ()
Change all
Note: copy a table (only copy structure, source table name: a new table name: B)Select * into B from a where 1
Description: copy a table (copy data, source table name: A target table name: B)Insert into B (a, B, c) Select D, E, F from B;
Description: displays the article, Submitter, and last reply time.Select a. Title, A. username, B. adddate from Table A, (select max (adddate) adddate from table where table. Title = A. Title) B
Description: External join query (table name 1: Table A name 2:
')
-- B. The last day of the monthSelect dateadd (day,-1, convert (char (8), dateadd (month, 1, @ DT), 120) + '1 ')
-- C. The last day of the month (the easy-to-use error method)Select dateadd (month, 1, dateadd (day,-day (@ DT), @ DT ))
-- 4. Specify any day of the week of the dateSelect dateadd (day, @ number-datepart (weekday, @ DT), @ DT)
-- 5. Specify any day of the week of the date-- A. Sunday is the 1st day of a week.Select dateadd (day, @ number-(datepart (weekday, @ DT) + @ DAT
1. The first day of a monthSelect dateadd (mm, datediff (mm, 0, getdate (), 0)
2. Monday of the weekSelect dateadd (wk, datediff (wk, 0, getdate (), 0)
3. The first day of the yearSelect dateadd (YY, datediff (YY, 0, getdate (), 0)
4. The first day of the quarterSelect dateadd (QQ, datediff (QQ, 0, getdate (), 0)
5
convert the consumption time into a discount and then calculate the consumption amount. The formula is changed as follows:
Consumption amount = consumption time * discount/unit time * unit price
There are some differences between the two algorithms, depending on the amount of time, because there is a conversion in the middle, this conversion can be ignored in my business, because the unit time is not very large, the data center is usually used as a settlement unit within 15 minutes. I tested i
DateDiff function
Returns a Variant (long) that represents the number of time intervals between two specified dates.
Grammar
DateDiff (interval, date1, date2[, firstdayofweek[, FirstWeekOfYear])
The following named arguments are in the DateDiff function syntax:
Partial description
Interval necessary. A string expression that represents the interval us
Tags: Type field BSP Date datetime AST first time-----restore content starts--- All data for Today: SELECT * FROM table name where DateDiff (dd,datetime Type field, GETDATE ()) =0 All data yesterday: SELECT * from table name where DateDiff (dd,datetime Type field, GETDATE ()) =1 All data within 7 days: SELECT * from table name where DateDiff (dd,datetime Type fie
Tags: arc varchar Current time server how many pre HTML VAR first dayRecords reprinted from: Http://www.cnblogs.com/suruozhong/p/5974595.html all data for today: SELECT * FROM table name where DateDiff (dd,datetime Type field, GETDATE ()) =0 all data for yesterday: SELECT * from table name where DateDiff (dd,datet IME Type field, GETDATE ()) =1 all data in 7 days: SELECT * from table name where
Label:One, Time function When you use stored procedures, SQL functions, you encounter some processing of time. such as the acquisition of time and the addition and subtraction. In this case, the SQL comes with the time function. Below I list these functions, easy to remember later, use. --getdate Get current time
Select getdate()
--DateAdd Original time add: 2013-02-17 13:20:16 this time plus 12 months
Select DateAdd(MONTH, A,'2013-02-17 13:20:16')--return: 2014-02-17 13:20:16.000 (paramete
Label:All data for Today: SELECT * FROM table name where DateDiff (dd,datetime Type field, GETDATE ()) =0 All data yesterday: SELECT * from table name where DateDiff (dd,datetime Type field, GETDATE ()) =1 All data within 7 days: SELECT * from table name where DateDiff (dd,datetime Type field, GETDATE ()) All data within 30 days: SELECT * from table name where
number field to "yes", and "Seed" and "incremental" to "1" to be automatically numbered.
2. All default values are lost. It mainly belongs to the numeric type and date type. In the past, some fields do not need to be manually assigned by the program, such as the date. now you need to add another rs ("newsDate") = now.
3. All now (), time (), and date () must be changed to getdate (). This is what we say on the Internet, but after I change it, the program runs abnormally. You can do it without c
Obtain the current system time selectgetdate (); selectcast (getdate () asdatetime); compare select * fromtable_namewhere (cast (adddateasdatetime)-getdate () 0and (cast (adddateasdatetime) -getdate ()-1; select * fromtable_namewhere (cast
// Obtain the current system time select getdate (); select cast (getdate () as datetime); // compare the current date select * from table_name where (cast (adddate as datetime) -getdate () 0 and (cast (adddate as datetime)-getdate ()-1; select * from table_na
Datediff Function
Returns the value of variant (long), indicating the number of time intervals between two specified days.
Syntax
Datediff (interval, date1, date2 [, firstdayofweek [, firstweekofyear])
The datediff function syntax contains the following naming parameters:
Partial descriptionInterval is required. String expression used to calculate the time diffe
SQL statement used to calculate the number of days in the month and quarter of a given date
SQL statement used to calculate the number of days in the month and quarter of a given date
SQL statement used to calculate the number of days in the month and quarter of a given date
Declare @ date datetime;
Set @ date = getdate ()
-- The number of days between the first day of the month and the next day of the month
Select datediff (day, dateadd
Tags: color io art ar cti Div line algorithm--本周第一天SELECTDATEADD(Day,1-(DATEPART(Weekday,getdate())[emailprotected]@DATEFIRST-1)%7,getdate())--orselectdateadd(wk,datediff(wk,0,getdate()),0)--本周第一天select dateadd(wk,datediff(wk,0,getdate()),6)--上月第一天SELECTCONVERT(CHAR(10),DATEADD(month,-1,DATEADD(dd,-DAY(GETDATE())+1,GETDATE())),111)--上月最后一天SELECTCONVERT(CHAR(10),DATEADD(ms,-3,DATEADD(mm,
The specific URL is not saved, but I this person is accustomed to good code to remember the way to save, now posted out, I hope the original author understanding.
Copy Code code as follows:
---The first day of the month
Select CONVERT (varchar (), DATEADD (Mm,datediff (Mm,0,dateadd (Mm,-1,getdate ()), 0), 112)
---The last day of the month
Select CONVERT (varchar (), DATEADD (Ms,-3,dateadd (Mm,dated
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-10-17 00:00:00.000
3. DateDiff returns the number of date and time boundaries across two specified dates.
Select DateDiff (Day, ' 2
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.