0

我需要的是一个数据项表达式,它输出去年当前季度的开始日期。查找当前年份很容易,从该日期减去 1 年也是如此。但除此之外,我被卡住了。

我目前if对每个季度都有一个丑陋的表达,例如:

if (extract(month,current_date) in (10,12,12)) then ((extract(year,_add_years (current_date,-1))||'-10-01'))

但无论我做什么,我都无法将年份和日期连接成字符串,我可以将其转换为日期对象。上面的代码给出了错误:

“加法”操作对以下数据类型组合无效:“整数”和“字符”

尝试使用cast()我将整数转换为字符时出现此错误。尝试将字符数组转换为日期时,我也会收到此错误:

“condexp”操作对以下数据类型组合无效:“字符”和“整数”

尝试使用 SQL Server 特定函数(它是一个 SQL Server 数据库)只会给我一个错误,即这些函数不可用于本地处理,所以我似乎无法使用 SS 日期算术,而且我找不到任何特别适用的东西在 Cognos 的内置日期函数中。

如何操作日期以将年份添加到已知的日/月组合并将其用作日期对象?

4

2 回答 2

3

我正在使用 cognos 10.1.1,但它会工作。

您可以使用以下代码获取该值:

_make_timestamp(
  extract(year, _add_years(current_date, -1)),
  (floor((extract(month,current_date)-1)/3)*3+1),
  01
)

以下是获取季度开始月份的简单方法。

(floor((extract(month,current_date)-1)/3)*3+1),

您可以将时间戳转换为日期或字符串。

cast(
  _make_timestamp(
  extract(year, _add_years(current_date, -1)),
  (floor((extract(month,current_date)-1)/3)*3+1),
  01
  ),
  date
)

,

cast(
  cast(
    _make_timestamp(
      extract(year, _add_years(current_date, -1)),
      (floor((extract(month,current_date)-1)/3)*3+1),
      01
    ),
    date),
    varchar(10)
)

另外,当您获得一天的价值时,您可以使用提取功能。

extract(day, _first_of_month (current_date))
于 2012-10-12T09:44:36.173 回答
0

I would go with SQLSERVER built-in functions tht exists in Cognos.
Here is an expression that works for me:

DATEADD({month},(3)*((DATEPART({quarter},[FullDateAlternateKey]))-1),
DATEADD({YEAR}, DATEDIFF({YEAR}, 0, dateadd({year},-1,[FullDateAlternateKey])), 0))

The FullDateAlternateKey field is my date field (from ADVERTUREWORKS DW DB).
If you still have problems, try to isolate the problem by trying this expression on new simple list report.

于 2012-10-12T09:18:43.697 回答