在 SQL Server 中修剪前导零的更好技术? [英] Better techniques for trimming leading zeros in SQL Server?

查看:24
本文介绍了在 SQL Server 中修剪前导零的更好技术?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用这个 一段时间:

I've been using this for some time:

SUBSTRING(str_col, PATINDEX('%[^0]%', str_col), LEN(str_col))

但是最近,我发现所有0"字符(例如00000000")的列存在问题,因为它从未找到要匹配的非0"字符.

However recently, I've found a problem with columns with all "0" characters like '00000000' because it never finds a non-"0" character to match.

我见过的另一种技术是使用 TRIM:

An alternative technique I've seen is to use TRIM:

REPLACE(LTRIM(REPLACE(str_col, '0', ' ')), ' ', '0')

如果有嵌入的空格,这有问题,因为当空格变回0"时,它们会变成0".

This has a problem if there are embedded spaces, because they will be turned into "0"s when the spaces are turned back into "0"s.

我试图避免使用标量 UDF.我发现 SQL Server 2005 中的 UDF 存在很多性能问题.

I'm trying to avoid a scalar UDF. I've found a lot of performance problems with UDFs in SQL Server 2005.

推荐答案

SUBSTRING(str_col, PATINDEX('%[^0]%', str_col+'.'), LEN(str_col))

这篇关于在 SQL Server 中修剪前导零的更好技术?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆