SoFunction
Updated on 2025-05-21

SQL statement field interception method

SQL statement field intercept

1. In MySQL, field interception can be achieved using the SUBSTRING function. For example, to intercept the first 5 characters of a string field, you can use the following SQL statement:

SELECT SUBSTRING(column_name, 1, 5) FROM table_name;

This statement will return the first 5 characters of the column_name field in the table_name table.

2. Another implementation method is to use the LEFT function. The LEFT function can intercept a specified number of characters from the left side of a string field. Here is an example:

SELECT LEFT(column_name, 5) FROM table_name;

This statement will return the first 5 characters of the column_name field in the table_name table.

3. If you want to intercept the last 5 characters of a string field, you can use the RIGHT function. Here is an example:

SELECT RIGHT(column_name, 5) FROM table_name;

This statement will return the last 5 characters of the column_name field in the table_name table.

4. If you want to intercept the middle part of a string field, you can use the SUBSTRING function to combine the string length and position to achieve it. Here is an example, which intercepts 5 characters starting from the third character:

SELECT SUBSTRING(column_name, 3, 5) FROM table_name;

This statement will return 5 characters in the column_name field in the table_name table starting from the third character.

These are some common implementation methods, and choose the appropriate method to implement field interception according to the situation.

Supplement: SQL intercepts the specified fields in the table

sql intercepts the specified fields in the table

Function introduction:
Substring() function is used to intercept strings. It can start from a certain position of the string, intercept several characters to the right, and return a string of a specific length.

Function: Return part of a character, binary, text or image expression

Syntax: SUBSTRING ( expression, start, length )

The substring function in SQL is used to capture part of a column data. The name of this function is not exactly the same in different databases:

MySQL: SUBSTR(), SUBSTRING()
Oracle: SUBSTR()
SQL Server: SUBSTRING()

parameter:

expression string, binary string, text, image, column, or expression containing columns. Do not use expressions containing aggregate functions.

start integer or expression that can be implicitly converted to int, specifying the start position of the substring.

length integer or expression that can be implicitly converted to int, specifying the length of the substring.

UPDATE base_house set area_type= SUBSTRING(area_type,1,(LENGTH(area_type)-3)) WHERE area_type LIKE "%㎡㎡%"

This is the end of this article about the method of intercepting SQL statements. For more related contents of intercepting SQL fields, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!