"hello-world" "This is a test-6ren">
gpt4 book ai didi

tsql - 用于生成 slug 的 T-SQL 函数?

转载 作者:行者123 更新时间:2023-12-04 09:00:44 24 4
gpt4 key购买 nike

快速检查是否有人拥有或知道能够从给定的 nvarchar 输入生成 slug 的 T-SQL 函数。 IE;

"Hello World" > "hello-world"
"This is a test" > "this-is-a-test"



我有一个通常用于这些目的的 C# 函数,但在这种情况下,我有大量数据要解析并转换为 slug,因此在 SQL Server 上执行此操作更有意义,而不是必须将数据传输过来电线。

顺便说一句,我没有对盒子的远程桌面访问权限,所以我无法针对它运行代码(.net、Powershell 等)

提前致谢。

编辑:
根据要求,这是我通常用来生成 slug 的函数:
public static string GenerateSlug(string n, int maxLength)
{
string s = n.ToLower();
s = Regex.Replace(s, @"[^a-z0-9s-]", "");
s = Regex.Replace(s, @"[s-]+", " ").Trim();
s = s.Substring(0, s.Length <= maxLength ? s.Length : maxLength).Trim();
s = Regex.Replace(s, @"s", "-");
return s;
}

最佳答案

您可以使用 LOWER REPLACE 去做这个:

SELECT REPLACE(LOWER(origString), ' ', '-')
FROM myTable

对于列的批量更新(代码根据 slug 列的值设置 origString 列:
UPDATE myTable
SET slug = REPLACE(LOWER(origString), ' ', '-')

关于tsql - 用于生成 slug 的 T-SQL 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3082588/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com