c# - 将其转换为 SQL 的最佳方法是什么

标签 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 "";

最佳答案

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

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

关于c# - 将其转换为 SQL 的最佳方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24960165/

相关文章:

c# - Linq查询帮助

c# - 完整性检查 : Do these nested . 所有调用都等同于 LINQ 中相应的 .Where 和 .SelectMany 调用?

mysql - 合并这两个查询

c# - 有没有办法实时查看 sql server 从我的应用程序接收的查询语句?

c# - .NET Winforms c# 上传大文件

c# - 如何在 WPF 中沿路径绘制平行线

c# - 在 Canvas WPF c# 中拖动和选择并调整元素大小

MySQL 工作台 : 'Service Started then Stopped'

php - 计数并获取mysqli/php中的所有记录

.net - 如何避免 File.Exists/File.Delete 或 Directory.Exists/Directory.Delete 中的竞争