Usually used sql

 

To summarize the most usually used sql statement

 

1. Special Date

- This morning
the SELECT DATEADD (dd, DATEDIFF (dd, 0, GETDATE ()), 0)
- tomorrow morning
the SELECT DATEADD (dd, DATEDIFF (dd, 0, GETDATE ()) + 1,0)
- the week Monday (week beginning Sunday from)
the SELECT DATEADD (wk, DATEDIFF (wk, 0, GETDATE ()), 0)
- the first day of the month
SELECT DATEADD (mm, DATEDIFF (mm , 0, GETDATE ()), 0)
- the last day of the month
the SELECT the DATEADD (dd, -1, the DATEADD (mm, the DATEDIFF (mm, 0, GETDATE ()) + 1,0))
- on the first day of the year
SELECT DATEADD (yy, DATEDIFF ( YY, 0, GETDATE ()), 0)
- the last day of the year
SELECT DATEADD (dd, -1, DATEADD (yy, DATEDIFF (yy, 0, GETDATE ()) + 1,0))


2. String Processing

- remove spaces
SELECT LTRIM (col1), RTRIM ( col2), LTRIM (RTRIM (col3)) FROM tableName

The SELECT
the SUBSTRING (col1,3,2) - starts from the third character, taken two to obtain two characters taken
, STUFF (col2,3,2, '') - 2 start removing from the third character to give the remaining characters
, STUFF (col3,3,2, 'XXX' ) - from the first three characters, the two characters to replace 3,4 XXX
, the rEPLACE (COL4, 'Old', 'new new' ) - in the col4 replace all old new new
the FROM tableName


- a column col1, spliced together
SELECT STUFF ((SELECT ',' + col1 FROM tableName WHERE filter condition FOR XML PATH ( '')) , 1,1, '')


- serial number, for example, to add a space with 0 or A000001, A000002 ......, character prefix may be fixed or variable year, month, date, etc.

DECLARE @flowNo AS VARCHAR(10)
DECLARE @nextNo AS int
SELECT @flowNo = MAX(flowNo) FROM tableName WHERE 过滤条件
IF(@flowNo IS null)
SET @flowNo = 'A000001'
ELSE
BEGIN
SET @nextNo = RIGHT(@flowNo,6)+1
SET @flowNo = LEFT(@flowNo,1)+REPLICATE('0',6-LEN(@nextNo))+CONVERT(VARCHAR(6),@nextNo)
END
SELECT @flowNo;--要获取的流水号

 


- 2001 corresponding to 1,2009 9,2010 corresponding to the corresponding A, 2035 corresponding to Z, can be adjusted after 2035
the SELECT the SUBSTRING ( '123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', (YEAR (GETDATE ()) - 2000) 36,1%)

- October, November, December, respectively A, B, C
the SELECT the SUBSTRING ( '123456789ABC', MONTH (GETDATE ()),. 1)

- May English abbreviation
SELECT SUBSTRING ( 'JanFebMarAprMayJunJulAugSepOctNovDec', ( MONTH (GETDATE ()) - 1) * 3 + 1,3)

- 1 corresponding to the number of days corresponding to 9,10 corresponding to 1,9 A, so
SELECT SUBSTRING ( '123456789ABCDEFGHIJKLMNOPQRSTUV', DAY (GETDATE ()), 1)

 

3. Auxiliary

- removing duplicate data, the data table has many duplicate data, wherein if col1, col2, col3, col4, col5 each set of duplicate data can be shown
; the WITH CTE the AS
(the SELECT col1, col2, col3, col4, col5
, the ROW_NUMBER () the OVER (the PARTITION BY col1, col2, col3, COL4, the ORDER BY col1 COL5) the AS RN
the FROM tableName)
the DELETE the FROM CTE the WHERE RN>. 1


- quickly open stored procedures, views,
sp_helptext stored procedure names - names do not add dbo former owner.
Sp_helptext view name - do not put a name such as former owner dbo


- the use of certain strings views, stored procedures
the SELECT a.name, B [text].
The FROM A the sysobjects
the INNER the syscomments the JOIN B = the ON a.id b.id
the WHERE B [text] the LIKE '% you want to check. string% '
the AND a.xtype =' V '-' P '

Guess you like

Origin www.cnblogs.com/gyxianzg/p/12051830.html