SQL Server,其中字段是int? [英] SQL Server, where field is int?

查看:42
本文介绍了SQL Server,其中字段是int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何完成:

select * from table where column_value is int

我知道我可能可以内部连接到系统表和类型表,但我想知道是否有更优雅的方法.

I know I can probably inner join to the system tables and type tables but I'm wondering if there's a more elegant way.

请注意,column_value 是一个可以"具有 int 值的 varchar,但不一定.

Note that column_value is a varchar that "could" have an int, but not necessarily.

也许我可以投射它并捕获错误?但同样,这似乎是一个黑客.

Maybe I can just cast it and trap the error? But again, that seems like a hack.

推荐答案

select * from table
where column_value not like '[^0-9]'

如果允许负整数,你需要像

If negative ints are allowed, you need something like

where column_value like '[+-]%' 
and substring(column_value,patindex('[+-]',substring(column_value,1))+1,len(column_value))
not like '[^0-9]'

如果 column_value 可以是超出int"类型限制的整数,则需要更多代码,并且您希望排除此类情况.

You need more code if column_value can be an integer that exceeds the limits of the "int" type, and you want to exclude such cases.

这篇关于SQL Server,其中字段是int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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