跨数据库管理系统检查字符串是否为数字的方法

2024-05-26

好的,我有这个字段:code varchar(255)。它包含我们导出例程中使用的一些值,例如

DB84
DB34
3567
3568

我需要仅选择自动生成的(全数字)字段

WHERE is_numeric(table.code)

is_numeric()检查是否code字段仅包含正数。

你能提出任何可以在 mysql 5.1 和 oracle 10g 下工作的东西吗?


下面是 SQL Server、MySQL 和 Oracle 的三个单独的实现。没有人使用(或可以)相同的方法,因此似乎没有跨 DBMS 的方法来做到这一点。 对于MySQL和Oracle,仅显示简单整数测试;对于 SQL Server,显示了完整的数值测试。

对于 SQL Server:请注意,isnumeric('.') 返回 1.. 但它实际上无法转换为浮点数。某些文本(例如“1e6”)无法直接转换为数字,但可以先传递浮点数,然后传递数字。

;with tmp(x) as (
    select 'db01' union all select '1' union all select '1e2' union all
    select '1234' union all select '' union all select null union all
    select '1.2e4' union all select '1.e10' union all select '0' union all
    select '1.2e+4' union all select '1.e-10' union all select '1e--5' union all
    select '.' union all select '.123' union all select '1.1.23' union all
    select '-.123' union all select '-1.123' union all select '--1' union all
    select '---1.1' union all select '+1.123' union all select '++3' union all
    select '-+1.123' union all select '1 1' union all select '1e1.3' union all
    select '1.234' union all select 'e4' union all select '+.123' union all
    select '1-' union all select '-3e-4' union all select '+3e-4'  union all
    select '+3e+4' union all select '-3.2e+4' union all select '1e1e1' union all
    select '-1e-1-1')

select x, isnumeric(x),
    case when x not like '%[^0-9]%' and x >'' then convert(int, x) end as SimpleInt,
    case
    when x is null or x = '' then null -- blanks
    when x like '%[^0-9e.+-]%' then null -- non valid char found
    when x like 'e%' or x like '%e%[e.]%' then null -- e cannot be first, and cannot be followed by e/.
    when x like '%e%_%[+-]%' then null -- nothing must come between e and +/-
    when x='.' or x like '%.%.%' then null -- no more than one decimal, and not the decimal alone
    when x like '%[^e][+-]%' then null -- no more than one of either +/-, and it must be at the start
    when x like '%[+-]%[+-]%' and not x like '%[+-]%e[+-]%' then null
    else convert(float,x)
    end
from tmp order by 2, 3

对于MySQL

create table tmp(x varchar(100));
insert into tmp
    select 'db01' union all select '1' union all select '1e2' union all
    select '1234' union all select '' union all select null union all
    select '1.2e4' union all select '1.e10' union all select '0' union all
    select '1.2e+4' union all select '1.e-10' union all select '1e--5' union all
    select '.' union all select '.123' union all select '1.1.23' union all
    select '-.123' union all select '-1.123' union all select '--1' union all
    select '---1.1' union all select '+1.123' union all select '++3' union all
    select '-+1.123' union all select '1 1' union all select '1e1.3' union all
    select '1.234' union all select 'e4' union all select '+.123' union all
    select '1-' union all select '-3e-4' union all select '+3e-4'  union all
    select '+3e+4' union all select '-3.2e+4' union all select '1e1e1' union all
    select '-1e-1-1';

select x,
    case when x not regexp('[^0-9]') then x*1 end as SimpleInt
from tmp order by 2

对于甲骨文

case when REGEXP_LIKE(col, '[^0-9]') then col*1 end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

跨数据库管理系统检查字符串是否为数字的方法 的相关文章

随机推荐

  • jQueryUI autoComplete 返回空列表

    在我的 asp net mvc3 应用程序中 我为搜索框添加了自动完成功能 当我测试它时 该操作返回了 3 个结果 您可以看到显示了一个列表 但是 这是一个空列表 您只看到 3 个 并且 li 标记之间没有任何内容 我很确定 操作没问题 因
  • Python OO程序结构规划

    我是 OOP 的初学者 我想创建一个包含三个类 A B 和 C 的程序 该类的每个实例都由一组特征 Achar1 Achar2 等定义 该程序应该创建uses由 A 元素 B 元素和 C 元素以及开始日期和结束日期组成 A 和 B 都有子类
  • 在 React 中将 Tailwind 类作为 prop 传递

    我正在创建一个可重用的组件按钮 我想将两个 Tailwind 类作为道具传递给该按钮并动态使用它们 这是我的组件 function Button primary secondry label onClick return
  • 从两个数组中查找公共文件

    我正在尝试从两个数组中查找通用名称文件 我已将两个不同文件夹的文件名保存在两个不同的数组中 现在我正在创建一个通用文件数组 其中包含具有通用名称的文件 filenames 1 包含文件夹 1 中文件名称的数组 filename2 包含文件夹
  • 在同一个容器但不同的耳朵中使用本地EJB

    我正在尝试在同一个 Glassfish 但不同的耳朵中使用本地 EJB 但是Glassfish找不到本地EJB或者无法消费 我读到了这个 根据 JavaEE 教程 Local bean 的客户端 必须在与其访问的企业 bean 相同的 JV
  • 如何从应用程序扩展启动父 iOS 应用程序

    有谁知道如何从应用程序扩展的视图控制器启动父应用程序 我只想从其应用程序扩展启动主应用程序 在 WWDC 会议上为 iOS 和 OS X 创建扩展 第 1 部分 https developer apple com videos wwdc 2
  • 生成脚本 - 仅数据 - 循环依赖项发现错误

    我正在尝试生成数据库中所有数据的脚本 以便我可以将数据移动到另一台服务器上的相同数据库 在 SQL Server 2012 中 我右键单击源数据库 任务 gt 生成脚本 gt 为整个数据库和所有数据库对象编写脚本 gt 高级 gt 要编写脚
  • Bot 在本地计算机上的 Bot Framework Emulator 中运行,但在部署到 Microsoft Azure 后无法运行 - HTTP 状态代码 NotFound

    现在 我正在测试启动机器人项目所需的步骤2019 年虚拟演播室社区 测试机器人机器人框架模拟器 V4 然后将该机器人部署到 Microsoft Azure 现在 我正在测试 Virtual Studio Community 2019 中提供
  • 实时/热重载不适用于 dockerized NestJS API

    我遇到了一个奇怪的问题 我目前正在使用 Windows 10 并通过 WSL2 运行 docker 我在 Windows 文件管理器中克隆了存储库 并且我尝试在 DEV 中运行它 也就是说 一旦我更新了在我的本地存储库中的文件中 此更改反映
  • 检查 Firebase 中是否存在用户名[重复]

    这个问题在这里已经有答案了 我有一个这样的数据库 users UID1 用户名 用户 1 UID2 Username of user 2 UID3 Username of user 3 UID4 Username of user 4 等等
  • 如何在 *ngFor 中停止属性绑定到 Angular 中的每个元素

    在我的 html 中 我想将属性绑定应用于每个元素 我有一个点击和悬停事件 每当用户 将鼠标悬停或单击单个元素 但现在悬停或 单击发生在其中的每个元素上 ngFor 我想要它只 发生在他们选择 悬停的元素上 我需要什么 改变 我看到了另一个
  • 使用 jQuery 从字符中获取文本

    我想在出现特定字符后从字符串中获取文本 比方说 文本文本文本 abc 我想得到 abc jquery 中这是如何完成的 这对某些人来说可能是微不足道的 但我对 jQuery 没有什么经验 你可以这样做 var text texttextte
  • 有没有用于iPhone开发的高级手势库?

    iPhone 平台有许多常见的手势习惯用法 例如 有轻击 捏合和滑动等操作 每种操作都需要不同数量的手指 但是 当您开发应用程序时 您需要根据有关触摸次数和位置的低级信息来实现这些内容 看起来这是图书馆的主要候选者 您将注册一个委托 设置一
  • Excel的解析路径

    其实我想问以下问题 对于位于 目录中定义的 PATH 怎么能 我找出这些目录中的哪个 找到了 因为我需要使用 Process Run 从 C 运行 Excel 并且只需指示 Excel 即可正常工作 Windows 似乎知道在哪里可以找到它
  • 如何获取sql server 2005中数据库连接的详细列表?

    如何获取sql server 2005中数据库连接的详细列表 使用系统存储过程sp who2
  • Vue SPA - 如何在浏览器中呈现时隐藏 .vue 文件

    我正在开发一个用 Vue js 开发的单页应用程序 托管在 Node js 服务器上 目前它仍在开发中 但最终将暴露给外部客户 并且由于我们将处理敏感数据 我们希望避免在用户检查元素时看到 vue 文件和相对文件树结构在开发工具中 See
  • Magento 路由器 URL - 需要连字符的路径名称

    假设我使用自定义控制器 其 url 路径 前端名称为 customcategory 好吧 显然如果我有一个名为 TestController php 和indexAction的控制器文件 url 路径将是 customcategory te
  • 在tinymce 4中裁剪后上传图像

    我正在开发tinymce 并且已经实现了imagetools 现在 当图像插入到文本编辑器中 然后我编辑 裁剪图像时 它将图像 src 更改为类似的内容blob www localhost asdf ghij 我想要的是裁剪后我可以将此 u
  • PHP crypt() Blowfish 函数不工作

    这是我第一次在 PHP 中使用 crypt 函数 我不明白为什么它不起作用 我的代码基于这篇文章 http www techrepublic com blog australia secure passwords with blowfish
  • 跨数据库管理系统检查字符串是否为数字的方法

    好的 我有这个字段 code varchar 255 它包含我们导出例程中使用的一些值 例如 DB84 DB34 3567 3568 我需要仅选择自动生成的 全数字 字段 WHERE is numeric table code is num