我需要获得第1和第1个名字的名称。姓名各部分的最后一个字母(名字,中间名,姓氏)匹配。名称可以有5个部分。 如果名称是:
croc lil brob tart
cron lee bro tasr
应该结果:
croc lil brob tart
Declare @name as varchar(70) = 'cron lee bro tasr test'
Select @name, Substring(@name,1,1) As "First letter of First Name",
Substring(@name,charindex(' ',@name) +1 -2,1) As "Last letter of First Name",
Substring(@name,charindex(' ',@name) +1 ,1) As "First letter of Second Name",
Substring(@name,charindex(' ',@name, charindex(' ',@name) + 1) +1 -2 ,1) As "Last letter of Second Name",
Substring(@name,charindex(' ',@name, charindex(' ',@name) + 1) +1 ,1) As "First letter of Third Name",
Substring(@name,charindex(' ',@name, charindex(' ',@name,charindex(' ',@name) + 1) + 1 +1)+1 -2 ,1) AS "Last Letter of 3rd Name",
Substring(@name,charindex(' ',@name, charindex(' ',@name,charindex(' ',@name) + 1) + 1 +1)+1 ,1) AS "First Letter of 4th Name",
Substring(@name,charindex(' ',@name,charindex(' ',@name, charindex(' ',@name,charindex(' ',@name) + 1) + 1 +1)+1)+1 -2 ,1) AS "Last Letter of 4th Name",
Substring(@name,charindex(' ',@name,charindex(' ',@name, charindex(' ',@name,charindex(' ',@name) + 1) + 1 +1)+1)+1 ,1) AS "first Letter of 5th Name",
SUBSTRING(Reverse(@name),1,1) AS "Last letter of 5th Name" WHERE
Substring(RIGHT(@name, CHARINDEX('.', REVERSE('.' + @name)) - 1),1,1) = Substring(RIGHT(@name, CHARINDEX('.', REVERSE('.' + @name)) - 1),charindex(' ',RIGHT(@name, CHARINDEX('.', REVERSE('.' + @name)) - 1)) +1 -2,1) OR
Substring(RIGHT(@name, CHARINDEX('.', REVERSE('.' + @name)) - 1),charindex(' ',RIGHT(@name, CHARINDEX('.', REVERSE('.' + @name)) - 1)) +1 ,1) = Substring(RIGHT(@name, CHARINDEX('.', REVERSE('.' + @name)) - 1),charindex(' ',RIGHT(@name, CHARINDEX('.', REVERSE('.' + @name)) - 1), charindex(' ',RIGHT(@name, CHARINDEX('.', REVERSE('.' + @name)) - 1)) + 1) +1 -2 ,1) OR
Substring(RIGHT(@name, CHARINDEX('.', REVERSE('.' + @name)) - 1),charindex(' ',RIGHT(@name, CHARINDEX('.', REVERSE('.' + @name)) - 1), charindex(' ',RIGHT(@name, CHARINDEX('.', REVERSE('.' + @name)) - 1)) + 1) +1 ,1) = Substring(RIGHT(@name, CHARINDEX('.', REVERSE('.' + @name)) - 1),charindex(' ',RIGHT(@name, CHARINDEX('.', REVERSE('.' + @name)) - 1), charindex(' ',RIGHT(@name, CHARINDEX('.', REVERSE('.' + @name)) - 1),charindex(' ',RIGHT(@name, CHARINDEX('.', REVERSE('.' + @name)) - 1)) + 1) + 1 +1)+1 -2 ,1) OR
Substring(RIGHT(@name, CHARINDEX('.', REVERSE('.' + @name)) - 1),charindex(' ',RIGHT(@name, CHARINDEX('.', REVERSE('.' + @name)) - 1), charindex(' ',RIGHT(@name, CHARINDEX('.', REVERSE('.' + @name)) - 1),charindex(' ',RIGHT(@name, CHARINDEX('.', REVERSE('.' + @name)) - 1)) + 1) + 1 +1)+1 ,1) =
Substring(RIGHT(@name, CHARINDEX('.', REVERSE('.' + @name)) - 1),charindex(' ',RIGHT(@name, CHARINDEX('.', REVERSE('.' + @name)) - 1),charindex(' ',RIGHT(@name, CHARINDEX('.', REVERSE('.' + @name)) - 1), charindex(' ',RIGHT(@name, CHARINDEX('.', REVERSE('.' + @name)) - 1),charindex(' ',RIGHT(@name, CHARINDEX('.', REVERSE('.' + @name)) - 1)) + 1) + 1 +1)+1)+1 -2 ,1) OR
Substring(RIGHT(@name, CHARINDEX('.', REVERSE('.' + @name)) - 1),charindex(' ',RIGHT(@name, CHARINDEX('.', REVERSE('.' + @name)) - 1),charindex(' ',RIGHT(@name, CHARINDEX('.', REVERSE('.' + @name)) - 1), charindex(' ',RIGHT(@name, CHARINDEX('.', REVERSE('.' + @name)) - 1),charindex(' ',RIGHT(@name, CHARINDEX('.', REVERSE('.' + @name)) - 1)) + 1) + 1 +1)+1)+1 ,1) = SUBSTRING(Reverse(RIGHT(@name, CHARINDEX('.', REVERSE('.' + @name)) - 1)),1,1)
通过这个选择查询,我能够得到第一个&名称各部分的最后一个字符。但是,我对where子句感到困惑。
答案 0 :(得分:0)
试试这个:
DECLARE @t TABLE(id INT, t VARCHAR(100))
INSERT INTO @t VALUES
(1, 'croc lil brob tart'),
(2, 'cron lee bro tasr')
;WITH cte AS(
SELECT id,
Split.a.value('.', 'VARCHAR(100)') AS t
FROM
(
SELECT id, CAST ('<M>' + REPLACE(t, ' ', '</M><M>') + '</M>' AS XML ) AS t FROM @t
) AS A CROSS APPLY t.nodes ('/M') AS Split(a))
SELECT * FROM @t t
WHERE NOT EXISTS(SELECT * FROM cte WHERE id = t.id AND LEFT(t, 1) <> RIGHT(t, 1))
输出:
id t
1 croc lil brob tart
答案 1 :(得分:0)
你可以创建一个分裂函数,然后它相对容易:
SELECT * FROM dbo.TableName t
WHERE NOT EXISTS
(
SELECT 1 FROM dbo.Split(t.Col, ' ')x
WHERE LEFT(x.Item, 1) <> RIGHT(x.Item, 1)
)
这是我使用的一个:
CREATE FUNCTION [dbo].[Split]
(
@ItemList NVARCHAR(MAX),
@delimiter CHAR(1)
)
RETURNS @ItemTable TABLE (Item VARCHAR(250))
AS
BEGIN
DECLARE @tempItemList NVARCHAR(MAX)
SET @tempItemList = @ItemList
DECLARE @i INT
DECLARE @Item NVARCHAR(4000)
SET @i = CHARINDEX(@delimiter, @tempItemList)
WHILE (LEN(@tempItemList) > 0)
BEGIN
IF @i = 0
SET @Item = @tempItemList
ELSE
SET @Item = LEFT(@tempItemList, @i - 1)
INSERT INTO @ItemTable(Item) VALUES(@Item)
IF @i = 0
SET @tempItemList = ''
ELSE
SET @tempItemList = RIGHT(@tempItemList, LEN(@tempItemList) - @i)
SET @i = CHARINDEX(@delimiter, @tempItemList)
END
RETURN
END