2012-10-10 56 views
0

我需要的是一個數據項表達式,它輸出去年當前季度的開始日期。找到當年很容易,因爲從那一天起減去1年。但除此之外,我卡住了。Cognos 8中的日期算術

我現在有一個像醜if表達每個季度:

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

但無論我做什麼,我不能concatonate年份和日期轉換爲字符串,我可以轉換成日期對象。上面的代碼提供了錯誤:

The operation "add" is invalid for the following combination of data types: "integer" and "character"

嘗試投放的整數使用cast()我得到這個錯誤的字符。我也得到試圖把字符數組轉換成日期時,此錯誤:

The operation "condexp" is invalid for the following combination of data types: "character" and "integer"

嘗試使用SQL Server特定功能(它是一個SQL Server數據庫)只是給了我一個錯誤,這些功能是當地不可用處理,所以我似乎無法使用SS日期算術,並且我找不到特別適用於Cognos內置日期函數的任何內容。

如何操作日期以將一年添加到已知的日/月組合並將其用作日期對象?

回答

0

我將使用Cognos中存在的SQLSERVER內置函數。
下面是對我的作品的表達:

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

的FullDateAlternateKey場是我的日期字段(從ADVERTUREWORKS DW DB)。
如果仍有問題,請嘗試通過在新的簡單列表報告中嘗試使用此表達式來查明問題。

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))