teradata CAST

1)、数值型截位或四舍五入
SEL CAST(50500.75 AS INTEGER) AS "Truncated”
,CAST(50500.75 AS DECIMAL(6,0)) AS "Rounded";

Truncated Rounded
----------- ----------
50500 50501.

[@more@]

SEL CAST(6.74 AS DECIMAL(2,1)) AS "Drop Precision”
,CAST(6.75 AS DECIMAL(2,1)) AS "Round Up”
,CAST(6.85 AS DECIMAL(2,1)) AS "Round Down to Even";

Drop Precision Round Up Round Down to Even
----------------- ------------ ------------------------
6.7 6.8 6.8

2)、字符型截位

SEL 'Teradata'(char(4)) as "Truncate with CAST”
,'Teradata'(char(4),UPPERCASE) as "Truncate and UPPERCASE";

Truncate with CAST Truncate and UPPERCASE
---------------------- ------------------------------
Tera TERA


3)、数值型进行格式变换
Numeric formatting symbols:
$ Fixed or floating dollar sign
9 Decimal digit (no zero suppress)
Z Zero-suppressed decimal digit
, Comma – inserted where specified
. Decimal point position
- Dash character – inserted where specified
/ Slash character – inserted where specified
% Percent character – inserted where specified

SELECT 123 (FORMAT '99999999'), 123 (FORMAT '9(8)')
,000005 (FORMAT 'Z(5)9'), 1234.56 (FORMAT '$$$,$$9.99')
,5 (FORMAT 'Z9%'), 2225551212 (FORMAT '999/999-9999');


123 123 5 1234.56 5 2225551212.
-------- -------- ------ ---------- --- ------------
00000123 00000123 5 $1,234.56 5% 222/555-1212

将DECIMAL型转成字符型:
不要用这种办法:
TRIM(TRAILING '.' FROM TRIM(CAST(P1.Acct_Item_Type_Id AS CHAR(20)))
要采取这种办法:
CAST(cast(P1.Acct_Item_Type_Id AS format ‘z(18)') as CHAR(20))

日期型进行格式变换
Date formatting symbols:
Y Year as YYYY/Y4 (2004) or YY (04)
M Month as MMMM/M4 (August), MMM/M3 (Aug) or MM (08)
D Day as DDD/D3 (day of the year) or DD (day of the month)
E Day of the week as EEEE/E4 (Monday) or EEE/E3 (Mon)
, Comma – inserted where specified
. Decimal point position
- Dash character – inserted where specified
/ Slash character – inserted where specified
B Blank position – inserted where specified

SELECT CAST(current_date AS FORMAT 'YYYYMMDD')
,CAST(current_date AS FORMAT 'MMMBDD,BYYYY')
,CAST(current_date AS FORMAT 'M4BDD,BY4')
,CAST(current_date AS FORMAT 'YYDDD');

Date Date Date Date
----------- ---------------- ------------------- -----
20040814 Aug 14, 2004 August 14, 2004 04227

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/16723161/viewspace-1018477/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/16723161/viewspace-1018477/

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值