将其转换为SQL的最佳方法是什么?

本文关键字:最佳 方法 是什么 SQL 转换 | 更新日期: 2023-09-27 18:15:07

我知道对于这里的数据库专家来说这应该是轻而易举的事情。我有一个字段在我的数据库在' A/B/C/D/E/F '

的格式

格式是无关的,我通常需要最后两部分,所以上面的格式是

'EF'
但是如果我有另一个字符串
AB/CD/EF/GH == EFGH

我希望最后两部分像这样返回'EFGH'

有谁知道我可以做一个SQL函数来分割这个

我正在使用Microsoft SQL Server 2012 -我希望这有帮助,

这是c#代码。

var myText = "A/B/C/D/E/F";
var identificationArray = myText.Split('/');
if(identificationArray.Length >= 2)
{
    var friendlyId = identificationArray[identificationArray.Length - 2] + identificationArray[identificationArray.Length - 1];
    return friendlyId;
}
return "";

将其转换为SQL的最佳方法是什么?

下面是一个答案,它以反向顺序搜索字符串中的第二个正斜杠,并返回删除了正斜杠的子字符串:

declare @s varchar(20)
set @s = 'A/B/C/D/E/F'
-- result: 'EF'
select reverse(replace(left(reverse(@s), charindex('/', reverse(@s), charindex('/', reverse(@s)) + 1)), '/', ''))
set @s = 'AB/CD/EF/GH'
-- result: 'EFGH'
select reverse(replace(left(reverse(@s), charindex('/', reverse(@s), charindex('/', reverse(@s)) + 1)), '/', ''))

用其他几个输入测试:

set @s = '/AB/CD' -- result: 'ABCD'
set @s = 'AB/CD'  -- result: an empty string '' -- you may not want this result
set @s = 'AB'     -- result: an empty string ''

下面是一种非常复杂的方法,可以使用一系列公共表表达式(cte)完成相同的事情。Itzik Ben-Gan使用CTE技术使用交叉连接生成一个理货表:

declare @s varchar(50)
set @s = 'A/B/C/D/E/F/G'
--set @s = 'AB/CD/EF/GH'
--set @s = 'AB/CD'
--set @s = 'ABCD/EFGH/IJKL'
--set @s = 'A/B'
-- set @s = 'A'
declare @result varchar(50)
set @result = ''
;with
-- cross-join a meaningless set of data together to create a lot of rows
Nbrs_2    (n) AS (SELECT 1 UNION SELECT 0 ),
Nbrs_4    (n) AS (SELECT 1 FROM Nbrs_2     n1 CROSS JOIN Nbrs_2     n2),
Nbrs_16   (n) AS (SELECT 1 FROM Nbrs_4     n1 CROSS JOIN Nbrs_4     n2),
Nbrs_256  (n) AS (SELECT 1 FROM Nbrs_16    n1 CROSS JOIN Nbrs_16    n2),
Nbrs_65536(n) AS (SELECT 1 FROM Nbrs_256   n1 CROSS JOIN Nbrs_256   n2),
Nbrs      (n) AS (SELECT 1 FROM Nbrs_65536 n1 CROSS JOIN Nbrs_65536 n2),
-- build a table of numbers from the data above; this is insanely fast
nums(n) as
(
   select row_number() over(order by n) from Nbrs
),
-- split the string into separate rows per letter
letters(n, c) as
(
    select n, substring(@s, n, 1)
    from nums
    where n < len(@s) + 1
),
-- count the slashes from the rows in descending order
-- the important slash is the second one from the end
slashes(n, num) as
(
    select n, ROW_NUMBER() over (order by n desc)
    from letters
    where c = '/'
)
select @result = @result + c
from letters
where n > (select n from slashes where num = 2) -- get everything after the second slash
and c <> '/' -- and drop out the other slash
select @result

您需要反转字符串并找到第二次出现的/字符。一旦有了这些,就很简单了,只需调用很多函数来获得所需的格式

declare @test varchar(max);
set @test = 'b/b/a/v/d';
select 
    case
        when charindex('/', reverse(@test), charindex('/', reverse(@test))+1) = 0 then ''
        else replace(reverse(substring(reverse(@test), 0, charindex('/', reverse(@test), charindex('/', reverse(@test))+1))), '/', '')
    end

我理解您想在SQL中这样做。但是你想过使用SQL CLR用户定义函数吗?它将比SQL执行得快。

来晚了,但这是我的尝试:

declare @text varchar(max), @reversedtext varchar(max)
select @text = 'AB/CD/EF/GH'
select @reversedtext = reverse(@text)
declare @pos1 int
declare @pos2 int
declare @pos3 int

select @pos1 = charindex('/', @reversedtext)
select @pos2 = charindex('/', replace(@reversedtext, left(@reversedtext, @pos1), ''))
select @pos3 = @pos1 + @pos2
select REPLACE(RIGHT(@text, @pos3), '/', '')