5. Commonly used string processing functions
CHARINDEX(SUBSTR,STR)
Returns the first occurrence of the substring SUBSTR in the string STR, or returns 0 if the character SUBSTR does not exist in the string STR;
SELECT CHARINDEX('数据','SQL数据库开发')
--结果:4
LEFT(STR, LENGTH)
Intercept STR from the left, LENGTH is the length of interception;
SELECT LEFT('SQL数据库开发',6)
--结果:SQL数据库
RIGHT(STR, LENGTH)
Intercept STR from the right, LENGTH is the length of interception;
SELECT RIGHT('SQL数据库开发',6)
--结果:L数据库开发
SUBSTRING(STR,N ,M)
Returns the string STR starting from the Nth character and intercepting the M characters after it;
SELECT SUBSTRING('SQL数据库开发',4,3)
--结果:数据库
REPLACE(STR, STR1, STR2)
Replace the STR1 character in the string STR with the STR2 character;
SELECT REPLACE('SQL数据库开发', 'SQL', 'sql')
--结果:sql数据库开发
LEN(STR)
Calculate the length of the string STR;
SELECT LEN('SQL数据库开发')
--结果:8
REVERSE(STR)
reverse the string;
SELECT REVERSE('SQL数据库开发')
--结果:发开库据数LQS
6. Copy table data
INSERT INTO TableName1 (field1, field2, field3)
SELECT field4, field5, field6 FROM TableName2
Note: The column data types of the copied table and the copied table need to be consistent
7. Conversion of letter case
Change uppercase letters to lowercase letters
UPDATE TableName SET Field = LOWER (Field)
Convert lowercase letters to uppercase letters
UPDATE TableName SET Field = UPPER(Field)
8. Delete table/data
DELETE FROM TableName
-
Just delete some data in the table, the table structure is still there..
-
DELETE can have a WHERE clause to delete part of the data, for example DELETE FROM Student WHERE Age > 20
-
Auto numbering is not restored to the initial value.