How to extract data before a specific character in SQL?

In SQL, you can use the SUBSTRING and LOCATE functions to extract data before a specific character.

  1. Utilize the SUBSTRING function.
SELECT SUBSTRING(column_name, 1, LOCATE('特定字符', column_name) - 1) AS result
FROM table_name;

In the statement above, ‘column_name’ refers to the column name to be extracted, ‘table_name’ refers to the table name to be queried, and ‘specific character’ represents the specific character to be extracted.

  1. Utilize the LEFT function:
SELECT LEFT(column_name, LOCATE('特定字符', column_name) - 1) AS result
FROM table_name;

In the statement above, column_name is the name of the column to be extracted, table_name is the name of the table to be queried, and ‘specific characters’ are the characters to be extracted.

Both methods will extract data before specific characters and return the results as a “result” column. Please choose the method that suits your situation.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds