Cognos Analytics provides the CAST function to convert data types.
When you intend to CAST from a string type to a temporal type, such as DATE or TIMESTAMP, the character value must follow the SQL standard.
YYYY-MM-DD
YYYY-MM-DD HH:MI:SS.fff (up to 9)
Given your initial description of combining a DATE and TIME value
CAST( CAST ( DATECOL, CHAR(11)) || CAST ( TIMECOL, VARCHAR(8)) , TIMESTAMP ) -> converts your character string to a TIMESTAMP type
- CAST ( DATECOL, CHAR(11)) -> 'YYYY-MM-DD '
- CAST ( TIMECOL, VARCHAR(8)) -> 'HH:MI:SS'
- CAST ( DATECOL, CHAR(11)) || CAST ( TIMECOL, VARCHAR(8)) -> 'YYYY-MM-DD HH:MI:SS'
Vendors, such as SQL Server offer "similar" functions whereby they allow you to define various string formats, such as CONVERT provides.
In some cases, can be more straightforward to use those versus nesting substrings, concatenation etc.
https://docs.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-ver15
Similarly, they may also provide a means to define what value should be used if the value cannot be converted.
cast ( 'HELLO', DATE ) is an error, not a default value of null etc.
https://docs.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-ver15
------------------------------
NIGEL CAMPBELL
------------------------------
Original Message:
Sent: Fri October 29, 2021 01:40 PM
From: Towandra Grant
Subject: Framework Manager - Convert Varchar to Datetime (SQL SERVER)
Hello all,
Cognos version 11.1.6
Database: SQLSERVER 2016
What is the correct function in Cognos to use to convert a varchar to a datetime (I need the date and the time)?
This is what I am doing:
Taking a date field and a time field, concatenating it together to create a date/24hr time query item (I created one for Start time and one for Stop time)
CAST(ACTIVITY_DATE AS VARCHAR(18)) || ' ' || SUBSTRING(CAST(replicate('0', 4 - len(START_TIME)) AS VARCHAR(2)) || CAST(START_TIME AS VARCHAR(4)) || ':' || SUBSTRING(CAST(replicate('0', 4 - len(START_TIME)) AS VARCHAR(2)) || CAST(START_TIME AS VARCHAR(4)) || ':00' AS StartTime_DateTime |
The result is like 2021-10-29 13:50:00
Now I need to take the new query item and convert it to a date and time so that I can perform a calculation (StopTime-StartTime)
Just for clarity:
The original SQL that runs in SSMS is
CONVERT(DATETIME,
CONCAT(CONVERT(VARCHAR,ACTIVITY_DATE,23),' ',
SUBSTRING(START_TIME,1,2),':',SUBSTRING(START_TIME,3,2),':00'),120)
Suggestions?
------------------------------
Towandra Grant
------------------------------
#CognosAnalyticswithWatson