[英]SQL Server - Error converting data type nvarchar to bigint
When I run following query with SELECT *
I get error saying : 当我使用SELECT *
运行以下查询时,出现错误提示:
[S0005][8114] Error converting data type nvarchar to bigint. [S0005] [8114]将数据类型nvarchar转换为bigint时出错。
SELECT * FROM (
SELECT * , ROW_NUMBER() OVER (ORDER BY CAST(id as BIGINT)) AS RowNum
FROM users
) AS users
WHERE users.RowNum BETWEEN 0 AND 5 ;
When I run this query only with SELECT id , ROW_NUMBER() ...
everything works. 当我仅使用SELECT id , ROW_NUMBER() ...
运行此查询时SELECT id , ROW_NUMBER() ...
一切正常。
My DB looks like this: 我的数据库如下所示:
This query run well with other table where id
column is NVARCHAR
此查询与id
NVARCHAR
其他表一起运行良好
ID column is number only and if i cast it as : CAST(id as NVARCHAR) i get same error. ID列仅是数字,如果我将其强制转换为:CAST(id为NVARCHAR),则会遇到相同的错误。
EDIT: 编辑:
I Found problem with column ID values 我发现列ID值有问题
ID 46903836 ID 9100000004 ID 46903836 ID 9100000004
Small ids dont have leading zeros 小ID没有前导零
Usually when I get this error it is because there is whitespace on the front or end of the column. 通常,当我收到此错误时,是因为列的开头或结尾有空白。 Here is how I fix it. 这是我的解决方法。
SELECT * FROM (
SELECT * , ROW_NUMBER() OVER (ORDER BY CAST(LTRIM(RTRIM(id)) as BIGINT)) AS RowNum
FROM users
) AS users
WHERE users.RowNum BETWEEN 0 AND 5 ;
This will ensure ID is just the number only I am also assuming that there aren't any alpha characters with the ID. 这将确保ID仅是数字,我还假设ID中没有任何字母字符。
Your ID
field is BIGINT
(you have posted your table structure), this don't cause the error in your question. 您的ID
字段是BIGINT
(您已经发布了表结构),这不会引起问题。
But, because is unuseful the CAST
you can rewrite your query as follow: 但是,由于CAST
没用,因此您可以按以下方式重写查询:
SELECT * FROM (
SELECT * , ROW_NUMBER() OVER (ORDER BY id) AS RowNum
FROM users
) AS users
WHERE users.RowNum BETWEEN 0 AND 5 ;
You Don't need to cast your id column as it is already in bigint datatype 您不需要强制转换id列,因为它已经在bigint数据类型中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.