用于生成 slugs 的 T-SQL 函数?
快速检查是否有人拥有或知道能够从给定 nvarchar 输入生成段的 T-SQL 函数。 IE;
“你好世界”> “你好世界”
“这是一个测试”> “这是一个测试”
我有一个 C# 函数,通常用于这些目的,但在这种情况下,我有大量数据需要解析并转换为 slugs,因此在SQL Server 而不必通过线路传输数据。
顺便说一句,我没有远程桌面访问该盒子的权限,因此我无法对其运行代码(.net、Powershell 等),
提前致谢。
编辑: 根据请求,这是我通常用来生成 slugs 的函数:
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;
}
Quick check to see if anyone has or knows of a T-SQL function capable of generating slugs from a given nvarchar input. i.e;
"Hello World" > "hello-world"
"This is a test" > "this-is-a-test"
I have a C# function that I normally use for these purposes, but in this case I have a large amount of data to parse and turn into slugs, so it makes more sense to do it on the SQL Server rather than have to transfer data over the wire.
As an aside, I don't have Remote Desktop access to the box so I can't run code (.net, Powershell etc) against it
Thanks in advance.
EDIT:
As per request, here's the function I generally use to generate slugs:
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以使用
LOWER
和REPLACE
执行此操作:用于列的批量更新(代码根据
origString
列的值设置slug
列:You can use
LOWER
andREPLACE
to do this:For wholesale update of the column (the code sets the
slug
column according to the value of theorigString
column:这就是我想出的解决方案。请随意在需要的地方修复/修改。
我应该提到的是,我当前正在开发的数据库不区分大小写,因此 LOWER(@str).
提及: http://blog.sqlauthority.com/2007/05/13/sql-server-udf-function-to-parse-alphanumeric-characters-from-string/ 查看原始代码。
This is what I've come up with as a solution. Feel free to fix / modify where needed.
I should mention that the database I'm currently developing against is case insensitive hence the LOWER(@str).
Mention to: http://blog.sqlauthority.com/2007/05/13/sql-server-udf-function-to-parse-alphanumeric-characters-from-string/ for the original code.
我知道这是一个旧线程,但对于下一代,我找到了一个甚至可以处理重音的函数 这里:
I know this is an old thread, but for future generation, I found one function that deals even with accents here:
以下是杰里米回应的变体。从技术上讲,这可能不会变得迟缓,因为我正在做一些自定义的事情,比如替换“。”带有“-dot-”,并去掉撇号。主要改进是它还删除了所有连续的空格,并且不删除预先存在的破折号。
Here's a variation of Jeremy's response. This might not technically be slugifying since I'm doing a couple of custom things like replacing "." with "-dot-", and stripping out apostrophes. Main improvement is this one also strips out all consecutive spaces, and doesn't strip out preexisting dashes.
我将杰里米的回应更进一步,即使在替换空格后也删除了所有连续的破折号,并删除了前导和尾随的破折号。
I took Jeremy's response a couple steps further by removing all consecutive dashes even after spaces are replaced, and removed leading and trailing dashes.