简体   繁体   English

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

[英]What is the best way to convert this to SQL

I know for the Database Guru's here this should be a doddle. 我知道数据库大师在这里应该是一个轻而易举的事。 I have a Field in my database in the format of ' A/B/C/D/E/F ' 我的数据库中有一个字段,格式为' A/B/C/D/E/F '

The format is irrelevant I generally need the last two parts so for the above it would be 格式是无关紧要的我通常需要最后两个部分,所以对于上面它将是

'EF'

But if I had another string 但如果我有另一个字符串

AB/CD/EF/GH == EFGH

And I am looking to getting the last two parts to return like this 'EFGH' 而且我希望让最后两部分像'EFGH'一样返回

Does anyone know an SQL Function I can do that will split this 有谁知道我可以做的SQL函数会拆分它

I am using Microsoft SQL Server 2012 - I Hope this helps, 我正在使用Microsoft SQL Server 2012 - 我希望这有帮助,

Here is C# Code. 这是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 "";

Here is one answer that searches a string in reverse order for the second forward slash and returns that substring with forward slashes removed: 下面是一个答案,它以相反的顺序搜索第二个正斜杠的字符串,并返回带有正斜杠的子字符串:

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

Testing this with a couple of other inputs: 使用其他几个输入进行测试:

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 ''

Here is a ridiculously complicated way to do the same thing with a series of common table expressions (CTEs). 使用一系列公用表表达式(CTE)执行相同操作是一种非常复杂的方法。 Credit goes to Itzik Ben-Gan for the CTE technique to generate a tally table using cross-joins: 感谢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

You need to reverse the string and find the 2nd occurrence of the / character. 您需要反转字符串并找到第二次出现的/字符。 Once you have that it is pretty straight forward, just a lot of function calls to get the desired format 一旦你有了它,它是非常直接的,只需要很多函数调用来获得所需的格式

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

I understand that you want to do this in SQL. 我知道你想在SQL中这样做。 But did you think about using SQL CLR User Defined Functions? 但您是否考虑过使用SQL CLR用户定义函数? It will execute faster than SQL. 它的执行速度比SQL快。 you anyways have the logic implemented in C# which definitely simpler than the logic in SQL. 你总是在C#中实现逻辑,它绝对比SQL中的逻辑简单。

Late to the party, but here is my attempt: 迟到了,但这是我的尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将此SQL查询转换为Linq的最佳方法是什么? - What is the best way to convert this SQL query to Linq? 将一本字典转换为另一本的最佳方法是什么 - what is the best way to convert one dictionary to another 将IEnumerator转换为通用IEnumerator的最佳方法是什么? - What is the best way to convert an IEnumerator to a generic IEnumerator? 在EmguCV中将视频转换为List的最佳方法是什么? - What is the best way to convert a video to a List in EmguCV 将Address对象转换为String的最佳方法是什么? - What is the best way to convert an Address object to a String? 将此字符串转换为分钟数的最佳方法是什么? - What is the best way to convert this string into number of minutes? 将FlowDocument转换为PDF的最佳方法是什么? - What's the best way to convert a FlowDocument into PDF 什么是Sql Raw Queries或Linq的最佳方法 - What is Best way Sql Raw Queries or Linq 在OpenRasta处理程序中处理异常时,转换为响应的最佳方法是什么? - When handling exceptions in OpenRasta handlers, what is the best way to convert to a response? 转换VB if语句将字符串返回到C#的最佳方法是什么 - What is the best way to convert VB if statement that return a string to C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM