taT4Sql | Converting Timestamp to Decimal using Teradata SQL

If you have ever worked in Oracle databases, you might have across DUAL table. It’s considered to be system table (I guess) where you can do some operations, get some values like current date, time, etc. I don’t really remember other uses.

Anyways, moving on to Teradata, I thought of doing the same, i.e. pull current timestamp from system table. However, I never came across any DUAL table till now, instead I came to know that just doing the following will do the needful —

SELECT CURRENT_TIMESTAMP;

Now, here comes the hack for the day, my colleague asked me, whether its possible to convert timestamp into decimal format.

Trick 2 Transform..

I remembered doing some casting few months back. If you look at my previous tricks on Teradata SQL, you might remember too. So, this is how I do it —

SELECT CAST( SUBSTR( CAST(CURRENT_TIMESTAMP AS CHAR(19)),1,10) || SUBSTR( CAST(CURRENT_TIMESTAMP AS CHAR(19)),12,8) AS DECIMAL(18));

See ya later..

Advertisements

taT4Sql | Comparing different Date formats using Teradata SQL

In a recent post, I shared code on how to change timestamp format from one style to another. Today, we do similar tweaks for date formats, as I had to do comparison between dates having different formats, on either side of operand in WHERE clause.

Trick 2 Compare Diff Formats..

Suppose, your table has a column having format ‘YYYY-MM-DD’ and you have to do comparison with a column having this format ‘YYYYMMDD’.

SEL *
FROM SOME_TBL
WHERE
CAST( CAST( YYYY_MM_DD_CLMN AS CHAR(10)) AS INTEGER) = YYYYMMDD_CLMN

Hope this helps..

taT4Sql | Changing Timestamp Formats using Teradata SQL

While working on some project at work, I was making use of Teradata SQL for querying databases and loading some table by creating various volatile tables and doing numerous joins in-between them.

Few days back, I came to know  from the client side that, I have to pull some data from a table’s attribute VC having datatype , VARCHAR(N) and insert into another table’s attribute TS, whose datatype would be TIMESTAMP(6). That’s an issue, if data is not in right format and hence, the client stated the following..

So, data present in VC having format ‘YYYYMMDDHHMISS’ and I have to change it to format ‘YYYY-MM-DD HH:MI:SS’, so as to get the data in right format for TS.

Trick 2 Change Format..

INSERT INTO
     SOME_TS_TBL(TS)
SELECT
     SUBSTR(VC,1,4) || '-' || SUBSTR(VC,5,2) || '-' || SUBSTR(VC,7,2) || '  ' ||
     SUBSTR(VC,9,2) || ':' || SUBSTR(VC,11,2) || ':' || SUBSTR(VC,13,2)
FROM
     SOME_VC_TBL

Assuming, SOME_TS_TBL has only one attribute (TS) and SOME_VC_TBL has VC. This computation will do the trick and transformation.

HTH..