Summary of how to use common functions in SQL Server
This article mainly introduces a summary of the common functions of SQL Server. Friends who need it can refer to it.
I wanted to record some common functions of SQL before, but it has never been implemented. . . hey-hey. . .
Until today, I have used the substring() function. The starting value of this method in C# is 0, while the starting value in SQL is 1. stupidly can not tell. . .
This blog is used to record the use of SQL functions. I will write wherever I think of using it. . .
SubString(): Method used to intercept the specified string. This method has three parameters:
Parameter 1: used to specify the string to be operated on.
Parameter 2: used to specify the starting position of the string to be intercepted, the starting value is 1.
Parameter 3: used to specify the length to be intercepted.
select substring('abcdef',1,3) -- 返回 abc select substring('123456321',0,2) -- 返回 1,即第一位,最好不要这样做
Left(): Used to return the left part of the specified length in the specified string. This method has two parameters:
Parameter 1: used to specify the string to be operated on.
Parameter 2: used to specify the length of the substring to be returned.
select LEFT('abc123',3) -- 返回 abc select LEFT('左侧部分右侧部分',4) -- 返回 左侧部分
Right(): Used to return the right part of the specified length in the specified string. This method has two parameters:
Parameter 1: used to specify the string to be operated on.
Parameter 2: used to specify the length of the substring to be returned.
select RIGHT('abc123',3) -- 返回 123 select RIGHT('左侧部分右侧部分',4) -- 返回 右侧部分
CharIndex(): Used to return the starting position of the specified substring in the specified string. Returns 0 if not found. This method has two parameters:
Parameter 1: used to specify the string to be searched.
Parameter 2: used to specify the string used for retrieval.
select charindex('a','123a123') -- 返回 4 select charindex('abc','123a123') -- 返回 0 select charindex('abc','123abc123') -- 返回 4
Stuff(): Used to delete characters of a specified length and insert new characters/values at the deleted positions. This method has four parameters:
Parameter 1: used to specify the string to be operated on.
Parameter 2: used to specify the starting position of the characters to be deleted.
Parameter 3: used to specify the length of characters to be deleted.
Parameter 4: Used to specify the new string/value inserted at the deleted position.
select stuff('123abc456',4,3,'ABC') -- 返回 123ABC456 select stuff('123abc456',1,3,'') -- 返回 abc456,用空字符串替代
Len(): Used to return the length of the value of the specified text. Leading spaces are counted, trailing spaces are not. This method has one parameter:
Parameter 1: used to specify the text or string to be operated on.
select len('123') -- 返回 3 select len('字符串') -- 返回 3
Difference(): Used to return an integer value indicating the difference between the SOUNDEX values of two character expressions. (i.e. the similarity of two strings) So what is a SOUNDEX value? Remember it first, it's its turn next.
The returned value ranges from 0 to 4: 0 means almost different or completely different, 4 means almost the same or exactly the same. This method has two parameters:
Parameter 1: used to specify the first string SOUNDEX value to be compared.
Parameter 2: used to specify the second string SOUNDEX value to be compared.
select difference('action','demo') -- 返回 2 select difference('123456','整数') -- 返回 4
Soundex(): used to return the SOUNDEX value of the specified string. SOUNDEX is a phonetic algorithm that uses the pronunciation of English words to calculate approximate values. The value consists of four characters, the first character is an English letter, and the last three are numbers. In Pinyin text, sometimes you can pronounce but cannot spell the correct word. You can use Soundex to achieve a similar fuzzy matching effect. The fuzzy matching here is different from LIKE.
Brief description of the algorithm:
-- 将英文字按以下规则替换(不使用第一个字符进行匹配,并且不使用对应值为 0 的英文字符的值) a e h i o u w y -> 0 b f p v -> 1 c g j k q s x z -> 2 d t -> 3 l -> 4 m n -> 5 r -> 6
If there are 2 or more letters with the same corresponding numbers in the string together (such as j and k) , delete the others and keep only 1. Remove the characters whose corresponding value is 0, and only return the first 4 bytes, which are not enough to fill with 0.
select soundex('string') -- 返回 S215 select soundex('str') -- 返回 S210 select soundex('123') -- 返回 0000 select soundex('字符串') -- 返回 0000
PS: Characters other than English characters will return 0000, so the second example of the method Difference() above will return 4 (meaning exactly the same).
Lower(): used to return the lowercase string of the specified English string. If it is not an English string, the original value is returned. This method has one parameter:
Parameter 1: used to specify the string to be converted to lowercase.
select lower('ABC') -- 返回 abc select lower('123') -- 返回 123
Upper(): Used to return the uppercase string of the specified English string. If it is not an English string, the original value is returned. This method has one parameter:
Parameter 1: used to specify the string to be converted to uppercase.
select upper('abc') -- 返回 ABC select upper('123') -- 返回 123
Ltrim(): Used to return the string after removing leading spaces. This method has one parameter:
Parameter 1: used to specify the string to be removed from leading spaces.
select ltrim(' 123') -- 返回 123 select ltrim(' 好多空格') -- 返回 好多空格 select len(' 123') -- 返回 11 select len(ltrim(' 123')) -- 返回 3
Rtrim(): used to return the string after truncating trailing spaces. This method has one parameter:
Parameter 1: used to specify the string to be truncated with trailing spaces.
select rtrim('123 ') -- 返回 123 select len(rtrim('123 ')) -- 返回 3
Replace(): Replaces all occurrences of the second given string expression in the first string expression with the third expression. This method has three parameters:
Parameter 1: used to specify the string to be operated on, that is, the string to be matched.
Parameter 2: used to specify the string to be matched.
Parameter 3: Used to specify the string used to replace existing matches.
--把 abc 替换为 xxx select replace('123abc456','abc','xxx') -- 返回 123xxx456 -- 用空字符串替换匹配项 select replace('123abc456','abc','') -- 返回 123456
The above is the detailed content of Summary of how to use common functions in SQL Server. For more information, please follow other related articles on the PHP Chinese website!

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

The steps to build a MySQL database include: 1. Create a database and table, 2. Insert data, and 3. Conduct queries. First, use the CREATEDATABASE and CREATETABLE statements to create the database and table, then use the INSERTINTO statement to insert the data, and finally use the SELECT statement to query the data.

MySQL is suitable for beginners because it is easy to use and powerful. 1.MySQL is a relational database, and uses SQL for CRUD operations. 2. It is simple to install and requires the root user password to be configured. 3. Use INSERT, UPDATE, DELETE, and SELECT to perform data operations. 4. ORDERBY, WHERE and JOIN can be used for complex queries. 5. Debugging requires checking the syntax and use EXPLAIN to analyze the query. 6. Optimization suggestions include using indexes, choosing the right data type and good programming habits.

MySQL is suitable for beginners because: 1) easy to install and configure, 2) rich learning resources, 3) intuitive SQL syntax, 4) powerful tool support. Nevertheless, beginners need to overcome challenges such as database design, query optimization, security management, and data backup.

Yes,SQLisaprogramminglanguagespecializedfordatamanagement.1)It'sdeclarative,focusingonwhattoachieveratherthanhow.2)SQLisessentialforquerying,inserting,updating,anddeletingdatainrelationaldatabases.3)Whileuser-friendly,itrequiresoptimizationtoavoidper

ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

Dreamweaver CS6
Visual web development tools