SELECT SUBSTRING(column_name, 1, CHARINDEX('特定字符', column_name) - 1) AS new_column_nameFROM table_name
时间: 2023-05-26 13:04:31 浏览: 109
SELECT column_name(s)
As an AI language model, I cannot access your database and execute SQL queries. However, I can explain to you what the above SQL query means and give you an example.
Explanation:
- SELECT: used to specify the columns that you want to retrieve data from.
- SUBSTRING: used to extract a substring (a portion of a string) from a string.
- CHARINDEX: used to find the position of a specified character or string within a given string.
- column_name: the name of the column from which you want to extract a substring.
- table_name: the name of the table that contains the column.
- '特定字符': the specific character or string that you want to find within the column.
- new_column_name: the name that you want to give to the newly created column that contains the extracted substring.
Example:
Suppose you have a table called "Employees" with two columns: "Name" and "Email". You want to extract the first name from the "Name" column and create a new column called "First Name". The "Name" column contains full names in the format of "Last Name, First Name". Here's the SQL query that you can use:
SELECT SUBSTRING(Name, CHARINDEX(',', Name) + 2, LEN(Name)) AS [First Name]
FROM Employees
This query will extract the substring after the comma and space (which represents the first name) from the "Name" column and create a new column called "First Name". The result will look like this:
First Name
-----------
John
Mary
Peter
...
阅读全文