zoukankan      html  css  js  c++  java
  • to_char() 数据格式化

    业务场景:

    设备运行状态类型0、1、2、3、4、5,当设备状态处于1、2、3、4、的时候就返回该状态下设备的值。

    其他状态给前端返回空,既然返回空,那就默认空串。

    select (case when (a.type=1 or a.type=2 or a.type=3 or a.type=4) then to_char(a.realValue ,'fm9900.0099')  else '') as realvalue end from a where 1=1;

    知识点一:

       to_char() 是oracle 的字符转换函数。to_char(value,'format') value是待转化的值,'format' 是转化后的 pattern。

      这里样式 fm9900.0099的包括如下含义:

    •  输出的字符串总共占了9位,小数点前4位,小数点后4位。
    • 9代表含义,如果这个位置是数字就显示数字,否则就用空格替代占位,总之要占住这个位置。
    • 0代表含义,如果这个位置是数字就显示数字,否则就用0替代占位,总之要占住这个位置。

    样例如下(注意两竖线之间是9个字符位置):

    select to_char(37 ,'fm9900.0099')  from dual。

    结果:------|    37.00    |-------

    select to_char(37 ,'fm0000.0099')  from dual。

    结果:------|0037.00  |-------

    select to_char(37 ,'fm9900.9999')  from dual。

    结果:------|    37.      |-------

    所以如果要让数字显示出来看着不错误应该用这种格式

    select to_char(37 ,'fm9990.00')  from dual。

    小数点前必须有一个0,小数点后保留几位小数就用几个0。

    结果:------|    37. 00   |-------

    所以,第一次我的写法其实是不恰当的,应该改为:

    select (case when (a.type=1 or a.type=2 or a.type=3 or a.type=4) then to_char(a.realValue ,'fm9990.0000')  else '') as realvalue end from a where 1=1;

    关注点:Number Format Elements

    A number format model is composed of one or more number format elements. The tables that follow list the elements of a number format model and provide some examples.

    Negative return values automatically contain a leading negative sign and positive values automatically contain a leading space unless the format model contains the MI, S, or PR format element.

    注意红色部分,意思是Number类型转换为字符时,负数会返回前面包含负号的字符串,正数则会返回前面包含空格的字符串,除非格式模式包含MI、S、或PR元素。查看TO_CHAR(4, '0000')返回的字符串长度,你会发现其长度为5.

    所以 ------|    37. 00   |------- 其实竖线之间输出的字符串长度是10,因为前面还有一个默认的正值(空格)的占位。

    知识点二:

    to_char() 是oracle 的字符转换函数。转换的数据类型当然不止一种。

     日期转换。

    select to_char(sysdate,'yyyy-MM-dd HH24:mi:ss') from dual;

  • 相关阅读:
    [k8s]kube-dns/dashboard排错历险记(含sa加载用法/集群搭建)
    禁用nginx的access日志
    redis info 各信息意义
    使用JProfiler进行内存分析
    lr11 BUG?Failed to send data by channels
    loadrunner 怎么能得到返回的http状态?
    SQL SERVER查看当前连接情况
    总结: Struts2 + Spring 的线程安全问题
    转发:maven打包时始终出现以下提示:-source 1.3 中不支持泛型(请使用 -source 5 或更高版本以启用泛型)
    SQL Server 索引结构及其使用(一)
  • 原文地址:https://www.cnblogs.com/huangsxj/p/7767500.html
Copyright © 2011-2022 走看看