简体   繁体   English

SQL Server-将数据类型nvarchar转换为bigint时出错

[英]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数据类型中

SQL Server数据库]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在SQL Server中将数据类型nvarchar转换为bigint时出错 - Error converting data type nvarchar to bigint in SQL Server 即时通讯在SQL Server 2008中收到此错误:将数据类型nvarchar转换为bigint时出错 - im getting this error in sql server 2008 : Error converting data type nvarchar to bigint 将存储过程从 SQL Server 引入 Power BI:将数据类型 nvarchar 转换为 bigint 时出错 - Bringing stored procedure into Power BI from SQL Server: Error converting data type nvarchar to bigint 在子查询中将数据类型nvarchar转换为bigint时出错 - Error converting data type nvarchar to bigint in subquery 在视图中将数据类型nvarchar转换为bigint时出错 - Error Converting data type nvarchar to bigint in view 在SQL Server中将nvarchar转换为bigint - Converting nvarchar to bigint in sql server 触发器执行期间出错:将数据类型nvarchar转换为bigint时出错 - Error during trigger execution: Error converting data type nvarchar to bigint SQL异常:从存储过程返回值时,“将数据类型bigint转换为nvarchar时出错。” - Sql Exception : “Error converting data type bigint to nvarchar.” , when returning a value from stored procedure 将数据类型nvarchar转换为bigint时出错-由联接引起 - Error converting data type nvarchar to bigint - caused by join 将数据类型nvarchar转换为bigint时出错-值转换 - Error converting data type nvarchar to bigint - value conversion
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM