SQL:如何在查询中使用用例和转换?
我想将 VARCHAR 转换为 INT,但在我的表中我有一些值,例如“???”然后 SQL Server 启动这个expcetion:
Conversion failed when converting the varchar value '????' to data type int.
Severity 16
我可以转换这个'???'为NULL,这没问题,但是怎么做呢?
我正在尝试做这样的事情:
INSERT INTO labbd11..movie(title, year)
SELECT movies.title,
CASE movies.mvyear IS '????' THEN NULL ELSE CAST (movies.mvyear AS INT)
FROM disciplinabd..movies
但是没有任何效果..
有什么想法吗?
I want to cast VARCHAR to INT, but in my table i have some value like '???' then SQL Server launch this expcetion :
Conversion failed when converting the varchar value '????' to data type int.
Severity 16
I could convert this '???' to NULL, that's no problem, but how do that ?
I'm trying to do something like this:
INSERT INTO labbd11..movie(title, year)
SELECT movies.title,
CASE movies.mvyear IS '????' THEN NULL ELSE CAST (movies.mvyear AS INT)
FROM disciplinabd..movies
But nothing works ..
Any ideas guys ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可能只是想一般性地解决这个问题并以相同的方式处理任何非 int 值
请参阅此问题
You might just want to solve this in general and deal with any non-int value the same way
See this question
我相信您会想要类似的内容
您希望您的 CASE 语句返回 VARCHAR(
MVYEAR
或 NULL),然后您希望 CAST 对 CASE 的结果进行操作。I believe you would want something like
You want your CASE statement to return a VARCHAR (either the
MVYEAR
or NULL) and then you want the CAST to operate on the result of the CASE.您还可以使用:
You can also use: