How to determine the field value which can not convert to (decimal, float,int) in SQL Server(SQL Server中如何确定不能转换为(decimal,float,int)的字段值)
问题描述
我有一个 SQL Server 数据库.
I have a SQL Server database.
一个字段的值类似于
 ID      VALUE
  1      NEGATIF
  2      11.4
  3      0.2
  4      A RH(+)
  5      -----
  6      >>>>>
  7      5.6<
  8      -13.9
我想将 VALUE 字段转换为十进制,当然是可转换的字段.
I want to CONVERT VALUE field to decimal, of course convert-able fields.
什么样的 SQL 语句可以做到这一点?
What kind of SQL statement can do this?
我如何了解转换时哪个值引发错误?
How can I understand which value is raising error while converting?
PS:我认为这可以解决 WHERE VALUE LIKE '[a-z]' 但如何添加更多过滤器,如 [-+ ()] ?
PS: I think this can solve  WHERE VALUE LIKE '[a-z]' but how can I add more filter like [-+ ()] ?
推荐答案
Plain ISNUMERIC is rubbish
Plain ISNUMERIC is rubbish
- 空字符串、
+、-和.都是有效的 + 也是如此.等等1e-3对浮点数有效,但对十进制数无效(除非您先将浮点数转换为十进制数)
- Empty string, 
+,-and. are all valid - So is 
+.etc 1e-3is valid for float but not decimal (unless you CAST to float then to decimal)
对于特别神秘但故障安全的解决方案,附加 e0 或 .0e0 then 使用 ISNUMERIC
For a particularly cryptic but failsafe solution, append e0 or .0e0 then use ISNUMERIC
SELECT
   ISNUMERIC(MyCOl + 'e0')   --decimal check,
   ISNUMERIC(MyCOl + '.0e0')  --integer check
所以
SELECT
    ID, VALUE,
    CAST(
          CASE WHEN ISNUMERIC(VALUE + 'e0') = 1 THEN VALUE ELSE NULL END
          AS decimal(38, 10)
        ) AS ConvertedVALUE
FROM
    Mytable
                        这篇关于SQL Server中如何确定不能转换为(decimal,float,int)的字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:SQL Server中如何确定不能转换为(decimal,float,int)的字段值
				
        
 
            
        - SQL 临时表问题 2022-01-01
 - 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
 - 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
 - 更改自动增量起始编号? 2021-01-01
 - 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
 - 导入具有可变标题的 Excel 文件 2021-01-01
 - 在SQL中,如何为每个组选择前2行 2021-01-01
 - 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
 - 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
 - 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
 
						
						
						
						
						
				
				
				
				