【问题标题】:Need to convert SQL column from varchar(255) to bigint(20)需要将 SQL 列从 varchar(255) 转换为 bigint(20)
【发布时间】:2012-11-27 23:35:22
【问题描述】:

旧的 Drupal 4.7 评论表中的“线程”列是 varchar(255),我需要将其转换为 bigint(20) 以使其适用于 Wordpress wp_cmets 表中的“comment_parent”列。

我尝试过各种我见过的 Cast 和 Convert 命令,但总是遇到语法错误。

【问题讨论】:

  • SQL 只是 结构化查询语言 - 许多数据库系统使用的语言,但不是数据库产品...很多事情都是特定于供应商的 - 所以我们真的需要知道您使用的是什么数据库系统(以及哪个版本)......
  • Chris 正在运行 MySQL 5.0.91 - 请参阅下面 Dan 的回答中的 cmets。

标签: mysql sql casting


【解决方案1】:

这适用于 SQL Server

create table Comments 
(
    [thread] nvarchar(255)
) 

insert comments 
select '1' 
union select '2' 
union select '3' 
union select '4' 
union select '5' 
union select 'x' 

select 
    case 
        when ISNUMERIC([thread]) > 0 
            then CAST([thread] as bigint) 
        else 
            null 
    end colAsBigInt 
    , [thread] colAsNvarChar 
from comments

http://sqlfiddle.com/#!6/337eb/1

对于 MySQL 试试:

create table if not exists Comments 
(
    thread varchar(255) character set UTF8 not null
);

insert comments(thread) values ('1');
insert comments(thread) values ('2');
insert comments(thread) values ('3');
insert comments(thread) values ('4');
insert comments(thread) values ('5');
insert comments(thread) values ('6.1');
insert comments(thread) values ('x');

select 
    case 
        when thread  REGEXP ('^(-|\\+)?([0-9]+\\.[0-9]*|[0-9]*\\.[0-9]+|[0-9]+)$') 
            then cast(thread as signed)
        else 
            null 
    end colAsBigInt 
    , thread colAsVarChar 
from comments

--regex trick from here: http://forums.mysql.com/read.php?60,1907,241284#msg-241284
--without the regex you'll get 0s instead of nulls for invalid values
--MySQL's cast only works on certain data types, given here http://www.roseindia.net/sql/mysql-example/mysql-cast.shtml

此处可运行 MySQL 示例:http://sqlfiddle.com/#!2/6d848/9

【讨论】:

  • 谢谢 - 但我不能按原样运行,对吧?我在哪里(以及如何)插入表名(cmets)和列名(线程)? declare comments table ([thread] nvarchar(255)) insert comments select '1' union select '2' union select '3' union select '4' union select '5' union select 'x' select case when ISNUMERIC([thread]) > 0 then CAST([thread] as bigint) else null end colAsBigInt , [thread] colAsNvarChar from comments
  • 您转换后的代码完美运行 - 我已经更新了上面的示例代码以匹配您的代码。只有最后的选择语句才是真正有趣的——创建和插入表只是为了设置数据(我在原始示例中使用了表变量,因为这比编写示例代码时创建和删除表更容易- 但在你的情况下,你会有一张实际的桌子)。上面的关键点是case语句和在强制转换之前使用IsNumeric,以确保在强制转换之前可以强制转换任何内容;从而避免错误。
  • 我刚刚在另一个评论线程中发现您正在使用 MySQL - 请参阅第二个示例。
  • @FahimParkar:刚刚仔细查看了您的链接并发现了舍入问题——这在 SQL 中不会发生,所以我怀疑这是一个 SQL Fiddle 错误。您可能需要向 JakeFeasel 报告。
【解决方案2】:

当我运行您为 MySQL 提供的第二组代码时,它没有将该列转换为 BigInt。它确实提供了 colasBigInt 和 colasVarChar 的并排比较。无一例外,对于数千行,无论 colasVarChar 值如何,所有 colasBigInt 都读取为 Null。

【讨论】:

    【解决方案3】:

    您遇到什么语法错误?你用的是什么数据库?

    您可以提供的信息越多,就越有可能有人可以帮助您。

    【讨论】:

    • 错误类似于:#1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以了解在附近使用的正确语法...数据库是 MySQL - 5.0.91-log 我正在尝试在 phpMyAdmin 中运行这些命令
    猜你喜欢
    • 2013-08-29
    • 1970-01-01
    • 2021-05-22
    • 2018-02-10
    • 1970-01-01
    • 2012-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多