SQL Server 中的 varchar 比较
我正在寻找一些 SQL varchar 比较函数,例如 C# string.compare (我们现在可以忽略大小写,当字符表达式相同时应返回零,当字符表达式不同时应返回非零表达式)
基本上我在一个表中有一些字母数字列需要在另一个表中验证。
我无法从(查询)中选择 A.col1 - B.col1,因为“-”运算符无法应用于字符表达式
我无法将表达式转换为 int(然后进行差值/减法),因为它失败
select cast ('ASXT000R' as int)
Conversion failed when converting varchar 'ASXT000R' to int
Soundex< /code> 不会这样做,因为 soundex 对于 2 个相似的字符串是相同的
Difference
不会这样做,因为 select Difference('abc','ABC')
= 4 (< a href="http://msdn.microsoft.com/en-us/library/aa258844%28v=SQL.80%29.aspx" rel="nofollow noreferrer">根据 msdn,区别在于2 个字符表达式的 soundex 的差异和差异 =4 意味着差异最小)
我可以编写一个 curson 来对每行中的每个字母数字字符进行 ascii 比较,但我想知道是否还有其他方法可以做到这一点?
I am looking for some SQL varchar comparison function like C# string.compare (we can ignore case for now, should return zero when the character expression are same and a non zero expression when they are different)
Basically I have some alphanumeric column in one table which needs to be verified in another table.
I cannot do select A.col1 - B.col1 from (query) as "-" operator cannot be applied on character expressions
I cannot cast my expression as int (and then do a difference/subtraction) as it fails
select cast ('ASXT000R' as int)
Conversion failed when converting varchar 'ASXT000R' to int
Soundex
would not do it as soundex is same for 2 similar strings
Difference
would not do it as select difference('abc','ABC')
= 4 (as per msdn, difference is the difference in the soundex of 2 character expressions and difference =4 implies least different)
I can write a curson to do ascii comparison for each alphanumeric character in each row, but I was wondering if there is any other way of doing it ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果这就是你想要的,你可以只使用这个:
请注意,当任一参数为 NULL 时,此表达式返回 1 。这可能不是您想要的。如果任一参数为 NULL,您可以使用以下命令返回 NULL:
如果您想要负值和正值取决于哪个比较“更大”,则可以使用以下代码:
If that's all you want, you can just use this:
Note that this expression returns 1 when either of the arguments is NULL. This is probably not what you want. You can use the following to return NULL if either argument is NULL:
If you want negative and positive values depending on which compares 'larger', you could use this instead:
或者
or