原文:
https://sapyard.com/abap-for-sap-hana-part-xxv-usage-of-built-in-functions-in-cds-part-i/
https://sapyard.com/abap-for-sap-hana-part-xxvi-usage-of-built-in-functions-in-cds-part-ii/
https://sapyard.com/abap-for-sap-hana-part-xxvii-usage-of-built-in-functions-in-cds-part-iii/
https://sapyard.com/cds-part-16-usage-of-built-in-functions-in-cds-iv/
https://help.sap.com/doc/abapdocu_750_index_htm/7.50/en-US/abensql_functions_string.htm
https://sapyard.com/cds-part-19-finding-week-of-the-year-in-cds-views/

https://help.sap.com/doc/abapdocu_750_index_htm/7.50/en-US/abencds_f1_sql_functions_character.htm

尊重版权,仅供个人学习

CDS中的内置函数

有两类可用的Functions:

  1. SQL Functions
  2. Built In Functions

数学函数

FUNCTION DEFINITION OUTPUT
ABS(arg) In mathematics, the absolute value or modulus x
CEIL(arg) Hitting the Ceiling of the Floating Number. Smallest integer number not less than the value of arg
DIV(arg1, arg2) Conventional Division Quotient
DIVISION(arg1, arg2, dec) Conventional Division but with an additional feature of specificing deicmal places The result is rounded to dec decimal places.
MOD(arg1, arg2) Conventional Modulo Operation Remainder
FLOOR(arg) Largest integer number not greater than the value of arg. More like scientific numbers
ROUND(arg, pos) Rounded value of arg. Rounding the Designated decimal point value

实例例子更好说明用法:

@AbapCatalog.sqlViewName: 'ZFLIGHT_SCH_V'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Built In Functions'
define view ZFLG_FNC_DDL as select distinct from sflight as a
{abs(-2) as Abs_Op,/*Ceil and Floor*/ceil(25.3) as Ceil_Op,floor(25.3) as Floor_Op,/*Division*/div(5,3) as Div_Op,division(5,3,5) as Div_Op2,mod(5,3) as Mod_Op,a.price as Flg_Price, round( a.price,1) as Round_Op

参考结果:

字符串函数

Function Description
Length gives Length of String
Instr finds the position of respective string within corresponding the field of the View
Concatenate joining two strings
Concatenate with Space Third Parameter in this function represents the Number of space between two strings
Left gives us left most characters equal to argument passed
Lower converts all into lower case [ Rather Subscript ]
Lpad & Rpad first parameter is field, second is the OUTPUT Length after padding, string that is to be padded
Ltrim & Rtrim first parameter is field, second is string or character that is to be removed
Replace second parameter finds the string to be replaced by the third
Substring finds the string that you want – second parameter is starting position and third is how many characters
Upper converts all characters of string into Upper case

参考代码:

@AbapCatalog.sqlViewName: 'ZSTR_FN_V1'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Built In Functions'
define view ZFNC_DDL as select distinct from makt as a
{
key a.matnr as Mat_Num,
a.maktx as Mat_Desc,
length( a.maktx ) as Des_Len,
instr( a.maktx, 'est' ) as Des_Find,
concat( a.maktx, a.spras ) as Des_Con,
concat_with_space( a.maktx, a.spras, 2 ) as Des_Con_space,
left( a.maktx, 3 ) as Des_left,
lower( a.maktx ) as Des_lower
}

参考结果:

参考代码:

@AbapCatalog.sqlViewName: 'ZSTR_FN_V1'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Built In Functions'
define view ZFNC_DDL as select distinct from makt as a
{key a.matnr as Mat_Num,
a.maktx as Mat_Desc,
right( a.maktx, 5 ) as Des_right, /*For Strings */
lpad( a.maktx, 6, 'xx' ) as Des_lpad,
rpad( a.maktx, 6, 'y' ) as Des_rpad,
ltrim( a.maktx, 't' ) as Des_ltrim,
rtrim( a.maktx, 't' ) as Des_rtrim,
replace( a.maktx, 'est','ough' ) as Des_replace,
substring( a.maktx, 2, 1 ) as Des_substring,
upper( a.maktx ) as Des_upper
}

参考结果:

参考代码:

Example:
define view ZFLG_FNC_DDL as select distinct from sflight as a
{
key a.connid as Flg_Connid,/*For Numericals */
lpad( a.connid, 6, '22' ) as Flg_lpad,
rpad( a.connid, 6, '99' ) as Flg_rpad,
ltrim( a.connid, '0' ) as Flg_ltrim,
a.fldate as Flg_Date,
rtrim( a.fldate, '8' ) as Flg_rtrim
}

参考结果:

例子:将‘分钟’改为‘小时:分钟’
分钟:

@AbapCatalog.sqlViewName: 'ZFLIGHT_SCH_V'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Built In Functions'
define view ZFLG_FNC_DDL as select from spfli as a
left outer join sgeocity as bon a.cityfrom = b.city
{key b.city as Source,key b.city as Destination,key a.carrid as Flg_ID,key a.connid as Flg_Conn,a.fltime as Flg_Time
}

小时:分钟

@AbapCatalog.sqlViewName: 'ZFLIGHT_SCH_V'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Flight Schedule Details'
define view ZFLG_FNC_DDL as select from spfli as a
left outer join sgeocity as bon a.cityfrom = b.city
{key b.city as Source,key b.city as Destination,key a.carrid as Flg_ID,key a.connid as Flg_Conn,concat( concat(lpad ( ltrim ( cast( div(a.fltime, 60) as abap.char( 12 ) ), '0' ), 2, '0' ), ':' ) , lpad ( ltrim ( cast( mod(a.fltime, 60) as abap.char( 12 ) ), '0'), 2, '0' ) ) as Flg_Time
}

单位转换函数

  • 第一个参数是数值
  • 第二个和第三个是源单位和目标单位

参考代码:

<code>@AbapCatalog.sqlViewName: 'ZUNIT_FN_V'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Test Unit Funtions'define view ZUNIT_FN as select distinct from mara as a{key a.matnr as Material,a.brgew as MatQuan,a.meins as SrcUnit,a.gewei as TgtUnit, unit_conversion( quantity => brgew, source_unit => meins, target_unit => gewei ) as ConvF1} where a.matnr > 'CH-6000'and a.matnr &lt; 'CH-6600';</code>

参考结果:

货币转换函数

  • 第一个参数是金额
  • 第二个和第三个是源货币和目标货币
  • 第四个参数是汇率的日期

参考代码:

参考结果:

小数位转换函数

对于JPY, HUF, KRW, COP等特定货币来说,外部的显示和内部储存的形式是不所不同的。
在表TCURX中,JYP货币的小数位是0位,EUR没在这个表中,默认小数位是2位。

参考代码:

<code>@AbapCatalog.sqlViewName: 'ZDEC_SHFT_V'@AbapCatalog.compiler.compareFilter: true@AbapCatalog.preserveKey: false@AccessControl.authorizationCheck: #NOT_REQUIRED@EndUserText.label: 'Test Currency Conversion'define view ZDEC_SHFTwith parameters p_amt : abap.curr( 5, 2 )as select distinct from sflight as a{key a.carrid as FlgID,key a.connid as FlgConID,key a.fldate as FlgDat,a.currency,decimal_shift( amount => :p_amt, currency => a.currency ) as DEC_SHFT}</code>

参考结果:



从上面两幅图可以很明显的看出,输入21.34按照对应的货币有不同的解释。

日期和时间转换函数

日期

  1. ADD DAYS(dats_add_days)
    3个参数,第1 个是要计算的基础日期,第2个是要加减的数字,第3个是用来错误处理的。
  2. ADD MONTHS(dats_add_months)
    周上面dats_add_days相似的3个参数。
  3. DAYS BETWEEN TWO DATES(dats_days_between)
    计划两个日期相差的天数
  4. DATE VALIDATION(dats_is_valid)
    检验一个日期是否是有效日期,结果返回10
  5. DATS_TIMS_TO_TSTMP
    日期时间转为timestamp格式
  6. TIMEZONE
    时区

参考代码:

<code>@AbapCatalog.sqlViewName: 'ZDT_TIME_FN_V'@AbapCatalog.compiler.compareFilter: true@AbapCatalog.preserveKey: true@AccessControl.authorizationCheck: #NOT_REQUIRED@EndUserText.label: 'Test Date and Time'define view ZDT_TIME_FNwith parameters p_add_days  : abap.int4,  p_add_months: abap.int4,@Environment.systemField: #SYSTEM_DATEp_curr_date : abap.dats as select from sflight as a {key a.carrid as FlgId,key a.connid as FlgConnId,key a.fldate as FlgDate,dats_add_days     (a.fldate, :p_add_days  , 'INITIAL') as Added_DT,dats_add_months   (a.fldate, :p_add_months, 'NULL'   ) as Added_MT,dats_days_between (a.fldate, $parameters.p_curr_date ) as Days_BTW,dats_is_valid     (a.fldate)                           as Is_Valid                               } </code>

参考结果:

Case1


Case2:


Case3:

<code>@AbapCatalog.sqlViewName: 'ZDT_DT_TIME_FN_V'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Test Date and Time 2'define view ZDT_DT_TIME_FNwith parameters  @Environment.systemField: #SYSTEM_DATEp_curr_date : abap.dats as select from sflconn as a{key a.agencynum as FlgAgy,key a.flconn    as FlgCon,a.arrtime   as FlgArr,//Convert Arrival Timedats_tims_to_tstmp($parameters.p_curr_date, a.arrtime, abap_system_timezone( $session.client,'NULL' ), $session.client, 'NULL')  as Flg_Arr_Conv,              abap_system_timezone( $session.client,'NULL' )
as MyTimeZone }</code>


时间

  • TIME_IS_VALID,检查参数是否是一个有效的时间,一个参数,字段类型应该而且必须是时间戳- DEC -长度15。
  • UTC TIME,返回UTC时间,没有参数。
  • ADD SECONDS,和上面计算时间的相似,可以正负计算
  • SECONDS BETWEEN,同日期相似

参考代码:

@AbapCatalog.sqlViewName: 'ZTM_FNS_SQL_V'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Time Functions'
define view ZTM_FNS as select from zdt_concr_rept as a{key a.rpt_id as RPT_Comment,a.ztime as RPT_TMSP,tstmp_is_valid(a.ztime) as valid1,   tstmp_current_utctimestamp() as UTC_TM,tstmp_add_seconds(a.ztime, cast( 15 as abap.dec(15,0) ), 'INITIAL') as ADDED_TM,    //TESTING DIFFERENCE OF SECONDStstmp_seconds_between(tstmp_current_utctimestamp(), a.ztime , 'FAIL') as difference
}

参考结果:

weeks


CDS View-Part8Part9Part10Part16 内置函数相关推荐

  1. python常用的内置函数

    内置函数,就是Python提供的, 可以直接拿来直接用的函数. 一.数字相关 01 数据类型 bool() 描述:测试一个对象是True, 还是False.bool 是 int 的子类. 语法:cla ...

  2. 七 递归与二分法、匿名函数、内置函数

    一 递归与二分法 一.递归调用的定义 二.递归分为两个阶段:递推,回溯 三.python中的递归效率低且没有尾递归优化 四.可以修改递归最大深度 五. 二分法 二 匿名函数 一. 什么是匿名函数? 二 ...

  3. 深浅拷贝、函数、内置函数、文件处理、三元运算、递归

    深浅拷贝 import copy copy.copy() #浅拷贝 copy.deepcopy() #深拷贝 num = 110 copynum = num #赋值 一.数字和字符串 对于 数字 和 ...

  4. Python3中的68个内置函数总结

    一.内置函数 10大类 数学运算(7个) 类型转换(24个) 序列操作(8个) 对象操作(9个) 反射操作(8个) 变量操作(2个) 交互操作(2个) 文件操作(1个) 编译执行(4个) 装饰器(3个 ...

  5. python 三元表达式、列表推导式、生成器表达式、递归、匿名函数、内置函数

    一.三元表达式 语法:[成立1 if condition1 else成立2 if condition2 else ...if 成立N conditionN else 不成立] sex = 'man' ...

  6. day11 - 15(装饰器、生成器、迭代器、内置函数、推导式)

    day11:装饰器(装饰器形成.装饰器作用.@语法糖.原则.固定模式) 装饰器形成:最简单的.有返回值的.有一个参数的.万能参数 函数起的作用:装饰器用于在已经完成的函数前后增加功能 语法糖:使代码变 ...

  7. smarty模板引擎_6-Smarty的内置函数

    内置函数 [php] view plaincopy <h3>声明变量</h3> <{*assign 声明变量*}> <{assign var='usernam ...

  8. python之列表推导式 内置函数

    一.列表推导式,生成器表达式 [ 变量(加工后的数据) for 变量i in 可迭代的数据类型 ] 列表的推导式, 循环模式 [ 变量(加工后的数据) for 变量i in 可迭代的数据类型 if 条 ...

  9. python-全栈开发-前方高能-内置函数

    python_day_14 13. 前方高能-内置函数 ⼀. 本节主要内容: 1. 内置函数 什么是内置函数? 就是python给你提供的. 拿来直接⽤的函数, 比如print., input等等. ...

最新文章

  1. 更改UISwitch大小
  2. HashMap的负载因子为什么默认是0.75
  3. ES6新特征总结与介绍——声明与表达式
  4. php正则 与 js正则
  5. Undefined exploded archive location Tomcat之项目不能发布
  6. C语言试题三十一之判断字符串是否为回文?若是则函数返回1,主函数中输出yes,否则返回0,主函数中输出no。回文是指顺读和倒读都是一样的字符串。
  7. C++中 Map的了解与基本用法(代码演示+自我总结+map中一对多的用法)
  8. php里的抽象类和接口
  9. 设计模式三大类及六大设计原则
  10. 微信小程序图片选择,预览和删除
  11. java中类的接口是什么_Java中的接口知识汇总
  12. linux socket的select函数例子
  13. hdu5618 (三维偏序,cdq分治)
  14. Altium designer学习(二)pcb库不求人——立创商城导出封装库
  15. eclipse的plugins导入hadoop-eclipse-plugin-2.6.0.jar后Preference下没有hadoop Map/Reduce的解决方法
  16. python代码示例大全 下载-python基础代码大全
  17. vue中用echarts 绘制geo 中国地图
  18. 【zt】克服当众怕羞的心理1
  19. 启用计算机的无线同屏,完美:将计算机转换为无线显示器,Windows 10的此功能确实强大...
  20. ida-IDC脚本剖析

热门文章

  1. 测试必会之 Linux 三剑客之 grep
  2. Linux——定时运行作业
  3. react手机号码344格式分割
  4. 1993年入市一老股民愤然离场 今年已亏55万
  5. 《文本大数据情感分析》读书报告
  6. python数据分析 - 如何探索数据
  7. 搜索引擎:检索技巧(Google谷歌,百度)+ 常用的资源网站及技巧
  8. python打地鼠脚本_制作一个打地鼠的小游戏!100行Python代码轻松搞定
  9. android 密度像素,Android屏幕密度适配问题之px,dp,sp等详细介绍
  10. 东北大学2018辽宁级计算机专项分数线,2018东北大学各省录取分数线【最新】