1

I have this table:

Original_Date  Date_        Date_1      YearMonth  YearMonth_1
03-07-2020     2020-07-03   2020-06-03  03-0       2020
13-03-2020     2020-03-13   2020-02-13  13-0       2020
08-01-2020     2020-01-08   2019-12-08  08-0       2019
13-11-2020     2020-11-13   2020-10-13  13-1       2020
23-03-2020     2020-03-23   2020-02-23  23-0       2020
30-07-2020     2020-07-30   2020-06-30  30-0       2020
13-07-2020     2020-07-13   2020-06-13  13-0       2020
24-01-2020     2020-01-24   2019-12-24  24-0       2019
05-10-2020     2020-10-05   2020-09-05  05-1       2020
11-07-2020     2020-07-11   2020-06-11  11-0       2020 

Where if I use TYPE() function on them they return:

Original Date: VARCHAR
Date_: DATE
Date_1: DATE
YearMonth_1: VARCHAR
YearMonth: VARCHAR

These columns are built based on Original_Date column, where:

SELECT
Original_Date
CAST(Original_Date AS DATE FORMAT 'DD-MM-YYYY') AS Date_, -- This is in a subquery, put for simplicity I put it here
ADD_MONTHS(CAST(Original_Date AS DATE FORMAT 'DD-MM-YYYY'),-1) AS Date_1, -- This is in a subquery, put for simplicity I put it here
LEFT(CAST(Date_ AS varchar(10)),4) AS YearMonth,
LEFT(CAST(Date_1 AS varchar(10)),4) AS YearMonth_1,
FROM TABLE

The problem is:

  • I can't understand why YearMonth takes the last (right) four characters instead of the first (left) four characters.
  • I was looking for a YearMonth with type DATE and format like YYYY-MM. But as I didn't find any solution with Teradata (if you can provide one, it will be better than the workaround I'm doing now), I'm going for YYYYMM as integer, but as you can see when I'm applying LEFT() function it seem to work like a RIGHT() function, even when both (Date_ and Date_1) have the same data types. why? How can I solve this?

EDIT

SELECT     
LEFT(CAST(Date_ AS varchar(10)),4) AS YearMonth_v2,
LEFT(CAST(Date_1 AS varchar(10)),4) AS YearMonth_1_v2,
FROM TABLE

Results in:

YearMonth_v2  YearMonth_1_v2
03-07-2020    2020-06-03
13-03-2020    2020-02-13
08-01-2020    2019-12-08
13-11-2020    2020-10-13
23-03-2020    2020-02-23
30-07-2020    2020-06-30
13-07-2020    2020-06-13
24-01-2020    2019-12-24
05-10-2020    2020-09-05
11-07-2020    2020-06-11

Why transforming the data type of these columns behave differently even when both of them (Date and Date_1) have the same data type and date format?

7
  • As for your first point, SELECT CAST(Date_ AS varchar(10)) will probably help you to visualize whats happening. Jan 26, 2021 at 14:47
  • It is obviously because you didn't specify the date format. Since you see the date as dd-MM-yyyy it worth to assume that cast returns the date in the same format and you substring from this string. How the DBMS can know you need a year with substring after you turn it into VARCHAR?
    – astentx
    Jan 26, 2021 at 14:54
  • @HoneyBadger I put the example recently.
    – Chris
    Jan 26, 2021 at 14:54
  • @Chris Does it really return 10 characters from LEFT(..., 4)?
    – astentx
    Jan 26, 2021 at 14:55
  • @astentx it's not. As you can see, it's returning only four characters. I transformed to varchar(10) before as I don't know how Teradata will cut the date specifically when you specify a varchar with a length less than the length of the date characters
    – Chris
    Jan 26, 2021 at 14:58

3 Answers 3

3

If you want to format a date/timestamp, the easiest way is TO_CHAR plus a format, e.g.

TO_CHAR(date_col, 'YYYY-MM') AS YearMonth

To get a YYYYMM integer:

  Extract(YEAR From date_col) * 100 
+ Extract(MONTH From date_col)
1
  • Adding that TO_CHAR() to my mental library. That is so much nicer than CAST(CAST(CAST)))
    – JNevill
    Jan 26, 2021 at 22:02
1

The big miss here is that you are fighting nature, or rather then nature of date storage and formatting in Teradata. If you run, as an example, the following:

SELECT
  '03-07-2020' AS Original_Date,
  CAST(Original_Date AS DATE FORMAT 'DD-MM-YYYY') AS Date_, -- This is in a subquery, put for simplicity I put it here
  ADD_MONTHS(CAST(Original_Date AS DATE FORMAT 'DD-MM-YYYY'),-1) Date_1, -- This is in a subquery, put for simplicity I put it here
  CAST(Date_ AS varchar(10)),
  CAST(Date_1 AS varchar(10)),
  LEFT(CAST(Date_ AS varchar(10)),4) AS YearMonth,
  LEFT(CAST(Date_1 AS varchar(10)),4) AS YearMonth_1


+-----------------+------------+------------+--------------+--------------+-------------+---------------+
| "Original_Date" |  "Date_"   |  "Date_1"  |   "Date_"    |   "Date_1"   | "YearMonth" | "YearMonth_1" |
+-----------------+------------+------------+--------------+--------------+-------------+---------------+
| "03-07-2020"    | 2020-07-03 | 2020-06-03 | "03-07-2020" | "2020-06-03" | "03-0"      | "2020"        |
+-----------------+------------+------------+--------------+--------------+-------------+---------------+

You'll notice that the return of the ADD_MONTHS() function is a date formatted in the Teradata default of YYYY-MM-DD. This is why your attempt to use the LEFT() string function against both of these dates is yielding strange results.

To correct you can just flip that first YearMonth to use the RIGHT() function instead of LEFT or you can re-cast the date format of your DATE_ like:

SELECT
  '03-07-2020' AS Original_Date,
  CAST(CAST(Original_Date AS DATE FORMAT 'DD-MM_YYYY') AS DATE FORMAT 'YYYY-MM-DD') AS Date_, -- This is in a subquery, put for simplicity I put it here
  ADD_MONTHS(CAST(Original_Date AS DATE FORMAT 'DD-MM-YYYY'),-1) Date_1, -- This is in a subquery, put for simplicity I put it here
  CAST(Date_ AS varchar(10)),
  CAST(Date_1 AS varchar(10)),
  LEFT(CAST(Date_ AS varchar(10)),4) AS YearMonth,
  LEFT(CAST(Date_1 AS varchar(10)),4) AS YearMonth_1


+-----------------+------------+------------+--------------+--------------+-------------+---------------+
| "Original_Date" |  "Date_"   |  "Date_1"  |   "Date_"    |   "Date_1"   | "YearMonth" | "YearMonth_1" |
+-----------------+------------+------------+--------------+--------------+-------------+---------------+
| "03-07-2020"    | 2020-07-03 | 2020-06-03 | "2020-07-03" | "2020-06-03" | "2020"      | "2020"        |
+-----------------+------------+------------+--------------+--------------+-------------+---------------+

The take-away here is that in Teradata if you want to change the format of the date, you MUST use a cast statement so sometimes you'll find yourself casting multiple times, plus Teradata has the tendency to gravitate to the default 'YYYY-MM-DD'. I've found it best to just go with the default so there are no surprises.

2
  • Excellent response. My TLDR - always specify a format when casting to and from dates.
    – Andrew
    Jan 26, 2021 at 16:15
  • Totally on the TLDR. You'll see often times when you want to output in your result set a date in a particular format you get into multiple casts to pull it off (since a cast is needed to change formats): CAST(CAST(myActualDate as DATE FORMAT 'MM/DD/YYYY') AS VARCHAR(10)) as myStringDate Which feels strange since it was already a date to begin with. It's also worth noting that instead of your string functions. Something like CAST(CAST(DATE_ AS DATE FORMAT 'MMYYYY') AS CHAR(6)) AS YearMonth will work well too.
    – JNevill
    Jan 26, 2021 at 16:40
0

Simple rules:

  • FORMAT clauses are only applied when converting some other data type to character string or vice versa. (This could be an explicit CAST, or an implicit conversion triggered by applying a function that only operates on strings to a different data type.)
  • Applying a FORMAT to something does not change how the value is stored.
  • When retrieving a value with a data type other than character string, the client is responsible for any formatting performed; the database FORMAT clause will be ignored. (In other words, if you want the database to format the data, you need something in your SQL that forces conversion to character string.)
2
  • Aside: By default BTEQ uses the native CLIv2 "field mode" which in effect asks the database to convert all values to character (thereby applying the FORMAT). Other utilities and standard drivers (ODBC, JDBC, Python, etc.) do not.
    – Fred
    Jan 26, 2021 at 17:49
  • you need something in your SQL that forces conversion to character string: TO_CHAR rulez :-)
    – dnoeth
    Jan 26, 2021 at 17:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.