How to use 'Between' operator for DATEs in Teradata? - teradata

I want to restrict the rows I retrieve by using 'between' for two dates.
The 'saledate' column I used has the following infomation
SALEDATE DATE FORMAT 'YYYY-MM-DD' NOT NULL
The code I used:
SELECT *
FROM trnsact
WHERE saledate BETWEEN '2005-01-01' AND '2005-06-30';
And then I got an error 'Error Code - 3535
Error Message - [Teradata Database] [TeraJDBC 15.10.00.09] [Error 3535][SQLState 22003]
A character string failed conversion to a numeric value.'
I also tried with DATE:
SELECT *
FROM trnsact
WHERE saledate BETWEEN DATE '2005-01-01' AND DATE '2005-06-30';
But end up with another error
Error Message - [Teradata Database] [TeraJDBC 15.10.00.09] [Error 3706] [SQLState 42000] Syntax error: Invalid DATE Literal.
Thanks for your help

You need to use DATE literals:
SELECT *
FROM trnsact
WHERE saledate BETWEEN DATE '2005-01-01' AND DATE '2005-06-31';

try below query for teradata. It's too late though.
SELECT *
FROM trnsact
WHERE saledate BETWEEN
to_date('2005-01-01','YYYY-MM-DD') AND
to_date('2005-06-30','YYYY-MM-DD') ;

Related

How to format an activity output as YYYY-MM-DD hh:mm:ss in Azure data factory

In my ADF pipeline I am trying to convert an output from my lookup activity to be in YYYY-MM-DD hh:mm:ss date format within the source query of a copy activity. The current output from my lookup activity is in YYYY-MM-DDThh:mm:ss format and I need to remove the 'T'.
I have tried using the dynamic content and formatDateTime functions but am having problems with the syntax. I am also using an SQL query to retrieve only the relevant data. The below is what I am
using as an input in the dynamic content query. I am able to get this to work, but I need to change '03/15/2018 12:00:00' to refer to the output of my lookup activity named LookupNewWaterMarkActivity.
SELECT *
FROM tableName
WHERE updatedDate >
'#{formatDateTime('03/15/2018 12:00:00', 'yyyy-MM-dd HH:mm:ss')}'
I have tried the below, but get the following error message:
'cannot fit package::output:any & { count, value } into the function parameter string. (6)'
SELECT *
FROM tableName
WHERE updatedDate >
'#{formatDateTime(activity('LookupNewWaterMarkActivity').output, 'yyyy-MM-dd HH:mm:ss')}'
Does anyone know how I can format the output of my activity within an SQL query any other way?
I am getting the below error when running the below code.
#concat('SELECT * FROM tableName WHERE sys_updated_on_value > ''',formatDateTime(activity('LookupNewWaterMarkActivity').output.value[0].sys_updated_on_value, 'yyyy-MM-dd HH:mm:ss'),'''')
Error code: FailToResolveParametersInExploratoryController
Details
The parameters and expression cannot be resolved for schema operations.
Error Message: { "message": "ErrorCode=InvalidTemplate,
ErrorMessage=The expression 'concat('SELECT * FROM tableName WHERE
sys_updated_on_value >
''',formatDateTime(activity('LookupNewWaterMarkActivity').output.value[0].sys_updated_on_value,
'yyyy-MM-dd HH:mm:ss'),'''')\n\n' cannot be evaluated because property
'value' doesn't exist, available properties are 'value[0]'.." }
Use the lookup activity output (activity('Lookup1').output.value[0].columnName) value in your expression with column name as shown below to refer the lookup activity output in later activities.
Lookup activity output:
Copy activity:
Expression:
#concat('SELECT * FROM tb2 WHERE date1 > ''',formatDateTime(activity('Lookup1').output.value[0].date1, 'yyyy-MM-dd HH:mm:ss'),'''')
Update:
If you have enables firstRow property in your lookup, use the below expression:
#concat('SELECT * FROM tb2 WHERE date1 > ''',formatDateTime(activity('Lookup1').output.firstRow.date1, 'yyyy-MM-dd HH:mm:ss'),'''')

how convert datetime to timestamp on cqlsh

I'm new in cassandra and specially to the cql syntax. But I have a created a column that is timestamp which gets me datetime as this '2018-05-18 03:08:58.246000+0000' but where I filter by created I get this error
InvalidRequest: Error froim server: code=2200 [Invalid query] message="Unable to coerce '2018-05-18 03:08:58.246000+0000' to formatted date (long)"
Which lead me to think that I either need to convert the datetime into a tick or do some sort of casting.
how convert datetime to timestamp on cqlsh in a where clause?
Although Cassandra stores timestamp fractions using the .ffffff format defined by the ISO 8601 standard as you mentioned.
cqlsh:test_keyspace> select * from timestamp_table ;
timestamp | other_field
---------------------------------+---------------
2018-05-18 03:08:58.246000+0000 | Other content
2018-05-18 03:08:58.000000+0000 | Other content
When interacting with the database (ie. INSERT, SELECT, ...) you need to use the .fff format like so:
cqlsh:test_keyspace> select * from timestamp_table WHERE timestamp='2018-05-18 03:08:58.123+0000';
timestamp | other_field
---------------------------------+---------------
2018-05-18 03:08:58.123000+0000 | Other content
Otherwise you will get the error you mentioned.
Error when Reading
cqlsh:test_keyspace> select * from timestamp_table WHERE timestamp='2018-05-18 03:08:58.123000+0000';
InvalidRequest: Error from server: code=2200 [Invalid query] message="Unable to coerce '2018-05-18 03:08:58.123000+0000' to a formatted date (long)"
Error when writing
cqlsh:test_keyspace> INSERT INTO timestamp_table (timestamp , other_field ) VALUES ( '2018-05-18 03:08:58.123456+0000', 'Other content');
InvalidRequest: Error from server: code=2200 [Invalid query] message="Unable to coerce '2018-05-18 03:08:58.123456+0000' to a formatted date (long)"

TPT load data type error

I'm trying to run a TPT load, but I am getting an error with my date datatype. Can someone help me with the right syntax?
DEFINE JOB MOVE_DATA
DESCRIPTION 'MOVE DATA'
(DEFINE SCHEMA SAMPLE_SCHEMA DESCRIPTION 'SAMPLE SCHEMA'
(
CUST DECIMAL(15,0) ,
START_DATE DATE FORMAT 'YYYY-MM-DD'
);
Error: TPT_INFRA: At "DATE" missing { PERIOD_ ANSIDATE_ BIGINT_ BLOB_
BYTE_ BYTEINT_ CHARACTER_ CHAR_ CHARACTERS_ CHARS_ CLOB_ DEC_ DECIMAL_
FLOAT_ GRAPHIC_ INT_ INTDATE_ INTEGER_ INTERVAL_ LONG_ NUMBER_
NUMERIC_ SMALLINT_ TIME_ TIMESTAMP_ VARBYTE_ VARCHAR_ VARDATE_
VARGRAPHIC_ XML_ } in Rule: Column Data Type
DATE is not a supported field type for your SCHEMA in a TPT. You can use VARCHAR(10). For Example:
START_DATE VARCHAR(10)
Or if you need to do a change of format in the TPT ETL, you can use VARDATE(). For example:
START_DATE VARDATE(10) FORMATIN ('YYYY-MM-DD') FORMATOUT ('MM/DD/YYYY')

Teradata BTEQ - Invalid timestamp issue

I am trying to execute an sql through TERADATA BTEQ and getting the below error.
But the same sql is running good when getting executed in Teradata SQL assistant.
WHERE S_ORDER_ITEM.LAST_UPD BETWEEN CAST( (('20050614' (DATE, FORMAT 'YYYY-MM-DD')) -1 (CHAR(10)) )|| ' ' || '22:00:01' AS TIMESTAMP(0)) AND CAST( (('20050614' (DATE, FORMAT 'YYYY-MM-DD')) (CHAR(10)) )|| ' ' || '22:00:00' AS TIMESTAMP(0) )
*** Failure 2666 Invalid date supplied for S_ORDER_ITEM.LAST_UPD.
The error message indicates that S_ORDER_ITEM.LAST_UPD is not a date/timestamp (char?) and the automatic typecast to a timestamp fails due to bad data.
This part '20050614' (DATE, FORMAT 'YYYY-MM-DD') should never work, because the string doesn't match the Format.
There's no need for casting a string to a date, because the recommended way to write a date literal is simpler and shorter, using Standard SQL DATE '2005-06-14'.
If the date is actually known you better write
BETWEEN TIMESTAMP '2005-06-13 22:00:01'
AND TIMESTAMP '2005-06-14 22:00:00'
Otherwise don't cast to/from string, use datetime calculations instead:
BETWEEN Cast(DATE '2005-06-14' AS TIMESTAMP(0)) - INTERVAL '01:59:59' HOUR TO SECOND
AND Cast(DATE '2005-06-14' AS TIMESTAMP(0)) + INTERVAL '22:00:00' HOUR TO SECOND
Edit:
If you can't change the input format you need to apply another FORMAT:
BETWEEN Cast((('20050614' (DATE, Format 'YYYYMMDD')) -1 (Format 'yyyy-mm-dd')) || ' ' || '22:00:01' AS TIMESTAMP(0))
AND Cast((('20050614' (DATE, Format 'YYYYMMDD')) (Format 'yyyy-mm-dd')) || ' ' || '22:00:00' AS TIMESTAMP(0))
timestemp error occurs when your phone time is wrong
The solution is to correct your device time

Teradata SQL Assistant Date Inserts

Context is Teradata SQL Assistant
Successfully created the following table:
CREATE VOLATILE TABLE RSN_WEEKLY_TMP, NO LOG
(
EXPLICIT_DATE DATE FORMAT 'MM/DD/YYYY'
)
PRIMARY INDEX (EXPLICIT_DATE)
ON COMMIT PRESERVE ROWS;
1) The following INSERT works successfully:
INSERT INTO JOCOOPER.RSN_WEEKLY_TMP (EXPLICIT_DATE) VALUES (CURRENT_DATE);
2) The following INSERT does not work and returns with Error:INSERT Failed [26665] Invalid date.
INSERT INTO JOCOOPER.RSN_WEEKLY_TMP (EXPLICIT_DATE) VALUES (02/02/2016);
3) However, if I use a string 'date value' and CAST it as a Date it works.
INSERT INTO JOCOOPER.RSN_WEEKLY_TMP (EXPLICIT_DATE) VALUES (CAST('02/03/2016' AS DATE FORMAT 'MM/DD/YYYY') );
I need to know how to make example #2 work? Please Advise?
02/02/2016 is an INTEGER calculation, dividing 2 by 2 by 2016, results in zero, of course this is not a valid date.
CAST('02/03/2016' AS DATE FORMAT 'MM/DD/YYYY') works because it tells the parser how to convert the string to a DATE.
The only recommended (and the shortest) way is a Standard SQL DATE literal:
DATE '2016-02-03'
You never need to think about formats because there's only one: YYYY-MM-DD
Actually, this format works too
insert into table_name (datecol) select '2015/12/31';
In your example:
CREATE VOLATILE TABLE RSN_WEEKLY_TMP, NO LOG
(
--EXPLICIT_DATE DATE FORMAT 'MM/DD/YYYY'
EXPLICIT_DATE DATE
)
PRIMARY INDEX (EXPLICIT_DATE)
ON COMMIT PRESERVE ROWS;
INSERT INTO JOCOOPER.RSN_WEEKLY_TMP (EXPLICIT_DATE) select '2016/02/02';

Resources