I / O:
col_details
Sam100
Ram200
Sam1000
Ram50000
Aryan450
O / P:
col_name col_salary
Sam 1100
Ram 50200
Aryan 450
答案 0 :(得分:3)
PATINDEX应该找到给定表达式的第一个位置。在这种情况下,一个数字。因此,您根据找到该数字的位置进行拆分。对名称的SUBSTRING采用数字前的任何文本,名称后的SUBSTRING采用以数字开头的任何文本。
SELECT SUBSTRING(COL_DETAILS, 1, patindex('[0-9]',col_details)-1) col_name,
SUBSTRING(COL_DETAILS, patindex('[0-9]',col_details), len(COL_DETAILS)) col_salary
FROM TABLE
答案 1 :(得分:1)
请找到以下脚本
Declare @EmpData AS TABLE (col_details Varchar(100))
INSERT INTO @EmpData
SELECT 'Sam100' UNION ALL
SELECT 'Ram200' UNION ALL
SELECT 'Sam1000' UNION ALL
SELECT 'Ram50000' UNION ALL
SELECT 'Aryan450'
SELECT EmpNAme,SUM(Sal) AS col_salary
FROM
(
SELECT col_details,
SUBSTRING(col_details,0,PATINDEX('%[0-9]%',col_details))AS EmpNAme,
CAST(SUBSTRING(col_details,PATINDEX('%[0-9]%',col_details), LEN (col_details)) AS INT)AS Sal FROM @EmpData
)dt
GROUP BY EmpNAme
ORDER BY EmpNAme DESC
结果
EmpNAme col_salary
------------------
Sam 1100
Ram 50200
Aryan 450
查看演示Result