SQL Server 中的 varchar 比较

发布于 2024-09-02 17:50:50 字数 748 浏览 2 评论 0原文

我正在寻找一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

错爱 2024-09-09 17:50:50

我们现在可以忽略大小写,当字符表达式相同时应返回零,当字符表达式不同时应返回非零表达式

如果这就是你想要的,你可以只使用这个:

CASE WHEN 'a' = 'b' THEN 0 ELSE 1 END

请注意,当任一参数为 NULL 时,此表达式返回 1 。这可能不是您想要的。如果任一参数为 NULL,您可以使用以下命令返回 NULL:

CASE WHEN 'a' = 'b' THEN 0
     WHEN 'a' <> 'b' THEN 1
END

如果您想要负值和正值取决于哪个比较“更大”,则可以使用以下代码:

CASE WHEN 'a' < 'b' THEN -1 
     WHEN 'a' = 'b' THEN 0
     WHEN 'a' > 'b' THEN 1
END

we can ignore case for now, should return zero when the character expression are same and a non zero expression when they are different

If that's all you want, you can just use this:

CASE WHEN 'a' = 'b' THEN 0 ELSE 1 END

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:

CASE WHEN 'a' = 'b' THEN 0
     WHEN 'a' <> 'b' THEN 1
END

If you want negative and positive values depending on which compares 'larger', you could use this instead:

CASE WHEN 'a' < 'b' THEN -1 
     WHEN 'a' = 'b' THEN 0
     WHEN 'a' > 'b' THEN 1
END
却一份温柔 2024-09-09 17:50:50
select case when a.col1 = b.col1 then 0 else 1 end from a join b on a.id = b.id

或者

select case when lower(a.col1) = lower(b.col1) then 0 else 1 end from a join b on a.id = b.id
select case when a.col1 = b.col1 then 0 else 1 end from a join b on a.id = b.id

or

select case when lower(a.col1) = lower(b.col1) then 0 else 1 end from a join b on a.id = b.id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文