ABAP透明表里的时间戳,数据类型为dec:
有个需求:计算这两个时间戳之间的天数间隔,丢弃时间戳年-月-日8位后面的小时:分钟:秒。
举个例子:如果时间戳是20180918173132,丢弃173132,只保留20180918, 然后再计算天数间隔。
直接用CDS view的字符串操作函数substring是不行的,因为时间戳类型dec和substring期待的字符串类型不匹配。
解决方案:
先将时间戳字段类型从dec强制转换成abap.dats:
@AbapCatalog.sqlViewName: 'zproday'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Day between'
define view zdate_day_between as select from comm_product {
key comm_product.product_id as prod_id,
comm_product.product_guid as prod_guid,
comm_product.valid_from as valid_from,
comm_product.valid_to as valid_to,
cast(substring(cast(valid_from as abap.char(32)),1,8) as abap.dats) as from_date,
cast(substring(cast(valid_to as abap.char(32)),1,8) as abap.dats) as to_date
}
然后再用CDS view标准的时间处理函数DATS_DAYS_BETWEEN:
@AbapCatalog.sqlViewName: 'zdbetw'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Day between'
define view zc_date_day_between as select from zdate_day_between as host{
key host.prod_guid,
host.prod_id,
host.from_date,
host.to_date,
DATS_DAYS_BETWEEN(host.from_date, host.to_date) as no_of_days
}
测试结果:
要获取更多Jerry的原创文章,请关注公众号"汪子熙":
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。