C# 将其转换为SQL的最佳方式是什么
我知道对于数据库专家来说,这应该是一个艰难的过程。我的数据库中有一个“a/B/C/D/E/F”格式的字段 格式是不相关的,我通常需要最后两部分,所以对于上面的内容,它将是C# 将其转换为SQL的最佳方式是什么,c#,sql,.net,database,sql-server-2012,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 ide
'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 "";
您需要反转字符串并找到第二个/字符。一旦你知道它是非常直接的,只需要大量的函数调用就可以得到想要的格式
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
下面是一个答案,它以相反的顺序搜索字符串中的第二个正斜杠,并返回删除了正斜杠的子字符串:
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来做同样的事情。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 @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), '/', '')
我知道您希望在SQL中执行此操作。但您是否考虑过使用SQLCLR用户定义函数?它将比SQL执行得更快。无论如何,您都可以使用C实现逻辑,这显然比SQL中的逻辑更简单。这可能非常特定于您使用的平台DBMS。你能用平台更新你的问题吗?在这种情况下,数据库、.net和优化标记似乎是多余的。您的格式是A/B/C/D/E/F还是AB/CD/EF/GH?你有一个双向的例子。加上1用于使用doddle这个词。这不是浪费,这是一个完全有效的问题。我不确定答案是什么,但我确实发现你可以参考。不要感到羞愧。这是提问的地方。看一看我提到的问题,当你能够解决问题时,请随意回答你自己的问题,并与其他人分享你的知识。总有一天,有人会遇到同样的问题,可能会把你的问题带到这里来寻求解决方案。很好,我喜欢你在没有案例的情况下设法做到这一点statement@Aducci非常感谢。我不确定所有的案子都符合OP的要求,但很接近。我们提出了非常相似的解决方案!