Date Comparisions - Differing formats

Tools
N/A

Date Comparisions - Differing formats

My database uses the date format yyyy-mm-dd. I have a new data source that uses yyyymm for a key date. How can I compare the two? I need to essentially do this:
where
yyyymm between yyyy-mm-dd1 and yyyy-mm-dd2

5 REPLIES
N/A

Re: Date Comparisions - Differing formats

Just specify a matching format, if the day is missing it will be replaced by the 1st.

where '200605' (date, format 'yyyymm') between date1 and date2

Dieter

N/A

Re: Date Comparisions - Differing formats

thanks!
N/A

Re: Date Comparisions - Differing formats

I'm not getting results back from this.

SELECT SOURCE_MONTHYEAR
WHERE
SOURCE_MONTHYEAR (date, format 'yyyy/mm/dd') BETWEEN
VARIABLE1
AND VARIABLE2

VARIABLE1 and VARIABLE2 are date format YYYY/MM/DD
SOURCE_MONTHYEAR is date format YYYYMM

I did get results using substring and concatenation on the between variables, i.e.: SUBSTR(VARIABLE1,1,4) || SUBSTR(VARIABLE1,5,2)

I've tried using date, format yyyymm on the between variables as well with empty results.

What else can I try?

Thanks in advance
Teradata Employee

Re: Date Comparisions - Differing formats

I think you are confusing external and internal representation. Is SOURCE_MONTHYEAR actually defined as DATE or is it CHAR that you know is supposed to represent a YYYYMM format date? The FORMAT specification is used for the conversion between external (character string) and internal datatypes (such as DATE). If you aren't doing conversion to/from character, FORMAT isn't relevant.

If SOURCE_MONTHYEAR is a character string, try it the way Dieter suggested.

On the other hand, if VARIABLE1 and VARIABLE2 are actually character strings you'd want to CAST those.

In either case, the FORMAT describes the character string - not the comparison.
Teradata Employee

Re: Date Comparisions - Differing formats

I think you are confusing external and internal representation. Is SOURCE_MONTHYEAR actually defined as DATE or is it CHAR that you know is supposed to represent a YYYYMM format date? The FORMAT specification is used for the conversion between external (character string) and internal datatypes (such as DATE). If you aren't doing conversion to/from character, FORMAT isn't relevant.

If SOURCE_MONTHYEAR is a character string, try it the way Dieter suggested.

On the other hand, if VARIABLE1 and VARIABLE2 are actually character strings you'd want to CAST those.

In either case, the FORMAT describes the character string - not the comparison.