我正在寻找一种获取下表的方法:
CREATE TABLE [dbo].[#temp]
(
[ID_TASK] [NVARCHAR](300) NULL,
[CHNAME_NAME] [NVARCHAR](300) NULL,
[CHNAME_PHONE] [NVARCHAR](300) NULL
) ON [PRIMARY]
INSERT INTO [dbo].[#temp]
VALUES ('ID005', 'Anderson Abreu Oliveira', '68157120'),
('ID006', 'Gonzalez-IV', '64106929'),
('ID009', 'Parker W.H.', '60994308')
我正在使用此SQL查询:
SELECT
ID_TASK, CHNAME_NAME,
RTRIM(LTRIM(SUBSTRING(CHNAME_NAME, 1, CHARINDEX(' ', CHNAME_NAME)))) AS SURNAME,
RTRIM(LTRIM(SUBSTRING(CHNAME_NAME, CHARINDEX(' ', CHNAME_NAME) + 1, LEN(CHNAME_NAME) - (CHARINDEX(' ', CHNAME_NAME) - 1)))) AS FIRSTNAME,
REPLACE((RTRIM(LTRIM(CHNAME_PHONE))), '8-', '') AS CHNAME_PHONE
FROM
[dbo].[#temp]
但是我得到这个结果:
如何解决此SQL查询以获得所需的结果?谢谢
答案 0 :(得分:1)
您可能会尝试使用case..when
结构
带有CHARINDEX('-', CHNAME_NAME)
和CHARINDEX(' ', CHNAME_NAME)
与following statement
中一样:
SELECT
ID_TASK, CHNAME_NAME,
RTRIM(LTRIM(SUBSTRING(CHNAME_NAME, 1,
(Case CHARINDEX(' ', CHNAME_NAME) When 0 Then CHARINDEX('-', CHNAME_NAME)-1
Else CHARINDEX(' ', CHNAME_NAME) End)))) AS SURNAME,
RTRIM(LTRIM(SUBSTRING(CHNAME_NAME,
(Case CHARINDEX(' ', CHNAME_NAME) When 0 Then CHARINDEX('-', CHNAME_NAME)
Else CHARINDEX(' ', CHNAME_NAME) End) + 1,
LEN(CHNAME_NAME) - (CHARINDEX(' ', CHNAME_NAME) - 1)))) AS FIRSTNAME,
REPLACE((RTRIM(LTRIM(CHNAME_PHONE))), '8-', '') AS CHNAME_PHONE
FROM
[dbo].[#temp]
答案 1 :(得分:1)
这是一件很高级的事情,我已经做了一些SQL来带您到达那儿,但还没有整理一下...
DECLARE @temp TABLE (
[ID_TASK] [nvarchar](300) NULL,
[CHNAME_NAME] [nvarchar](300) NULL,
[CHNAME_PHONE] [nvarchar](300) NULL
)
INSERT INTO @temp
SELECT 'ID005','Anderson Abreu Oliveira','68157120' UNION ALL
SELECT 'ID006','Gonzalez-IV','64106929' UNION ALL
SELECT 'ID009','Parker W.H.','60994308'
select ID_TASK, col1, col2, col3
from
(
select a.ID_TASK, Item , 'col' + CONVERT(NVARCHAR(10), ItemNumber) ItemNumber
from @temp a
CROSS APPLY [dbo].[DelimitedSplit] ( REPLACE(a.CHNAME_NAME, '-', ' '), ' ' ) b
) d
pivot
(
MAX(item)
for itemNumber in (col1, col2, col3)
) piv
GROUP BY ID_TASK, col1, col2, col3
辅助功能:
CREATE FUNCTION [dbo].[DelimitedSplit] (
@pString VARCHAR(MAX),
@pDelimiter CHAR(1)
)
RETURNS TABLE WITH SCHEMABINDING
AS
RETURN
WITH E1(N) AS ( -- 10
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
),
E2(N) AS (SELECT 1 FROM E1 a, E1 b),
E3(N) AS (SELECT 1 FROM E2 a, E2 b, E2 c),
E4(N) AS (SELECT 1 FROM E3 a, E3 b, E3 c, E3 d),
cteTally(N) AS (
SELECT 0 UNION ALL
SELECT TOP ( DATALENGTH( ISNULL( @pString, 1 ) ) ) ROW_NUMBER() OVER ( ORDER BY ( SELECT NULL ) ) FROM E4
),
cteStart(N1) AS (
SELECT t.N + 1
FROM cteTally t
WHERE ( SUBSTRING( @pString, t.N, 1 ) = @pDelimiter OR t.N = 0 )
)
-- ------
SELECT
ItemNumber = ROW_NUMBER() OVER( ORDER BY s.N1 ),
Item = SUBSTRING( @pString, s.N1, ISNULL( NULLIF( CHARINDEX( @pDelimiter, @pString, s.N1 ), 0) - s.N1, 1000000 ) )
FROM cteStart s
GO
答案 2 :(得分:0)
您在这里:
CREATE TABLE [dbo].[#temp]
(
[ID_TASK] [NVARCHAR](300) NULL,
[CHNAME_NAME] [NVARCHAR](300) NULL,
[CHNAME_PHONE] [NVARCHAR](300) NULL
) ON [PRIMARY]
INSERT INTO [dbo].[#temp]
VALUES ('ID005', 'Anderson Abreu Oliveira', '68157120'),
('ID006', 'Gonzalez-IV', '64106929'),
('ID009', 'Parker W.H.', '60994308');
WITH CTE AS
(
SELECT [ID_TASK],
LEFT([CHNAME_NAME], IIF( CHARINDEX(' ', [CHNAME_NAME]) = 0 , CHARINDEX('-', [CHNAME_NAME])-1, CHARINDEX(' ', [CHNAME_NAME]))) AS T,
[CHNAME_NAME],
[CHNAME_PHONE]
FROM #Temp
)
SELECT [ID_TASK],
[CHNAME_NAME],
T AS SURNAME,
LTRIM(REPLACE(CASE WHEN (T IS NULL) OR (T = '') THEN [CHNAME_NAME] ELSE SUBSTRING([CHNAME_NAME], LEN(T)+1, LEN([CHNAME_NAME])) END, '-', '')) FirstName,
[CHNAME_PHONE]
FROM CTE;
结果:
+---+---------+-------------------------+-----------+-----------------+--------------+
| | ID_TASK | CHNAME_NAME | SURNAME | FirstName | CHNAME_PHONE |
+---+---------+-------------------------+-----------+-----------------+--------------+
| 1 | ID005 | Anderson Abreu Oliveira | Anderson | Abreu Oliveira | 68157120 |
| 2 | ID006 | Gonzalez-IV | Gonzalez | IV | 64106929 |
| 3 | ID009 | Parker W.H. | Parker | W.H. | 60994308 |
+---+---------+-------------------------+-----------+-----------------+--------------+