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

时间:2014-07-25 16:18:06

标签: c# sql .net database sql-server-2012

我知道数据库大师在这里应该是一个轻而易举的事。我的数据库中有一个字段,格式为' 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 "";

4 个答案:

答案 0 :(得分:4)

这是一个答案,它以相反的顺序搜索第二个正斜杠的字符串,并返回带有正斜杠的子字符串:

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

答案 1 :(得分:3)

您需要反转字符串并找到/字符的第二次出现。一旦你有了它是非常直接的,只需要很多函数调用来获得所需的格式

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

答案 2 :(得分:1)

我知道您希望在SQL中执行此操作。但您是否考虑过使用SQL CLR用户定义函数?它的执行速度比SQL快。你总是要在C#中实现逻辑,它绝对比SQL中的逻辑简单。

答案 3 :(得分:0)

晚会,但这是我的尝试:

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), '/', '')