Teradata DDL with Date Formatting - date-format

I wanna create a table in teradata with YYYY-MM format and give default value as '0001-01'. Is it possible to do both?..However i am able to do this..
create table test(t1 date format 'yyyy-mm' not null);

Both are working perfectly.
See below for syntax.
BTEQ -- Enter your SQL request or BTEQ command:
ct t1(x1 int,y1 date format 'yyyy-mm' not null default date '0001-01-01');
ct t1(x1 int,y1 date format 'yyyy-mm' not null default date '0001-01-01');
*** Table has been created.
*** Total elapsed time was 1 second.
BTEQ -- Enter your SQL request or BTEQ command:
ins t1(1,'2011-11');
ins t1(1,'2011-11');
*** Insert completed. One row added.
*** Total elapsed time was 1 second.
BTEQ -- Enter your SQL request or BTEQ command:
ins t1(2,);
ins t1(2,);
*** Insert completed. One row added.
*** Total elapsed time was 1 second.
BTEQ -- Enter your SQL request or BTEQ command:
sel * from t1;
sel * from t1;
*** Query completed. 2 rows found. 2 columns returned.
*** Total elapsed time was 1 second.
x1 y1
----------- -------
1 2011-11
2 0001-01

Dates are stored internally as INTEGER values in Teradata. You will have to specify your DEFAULT as DATE '0001-01-01' and allow the FORMAT clause to return the formated data to the client. This also means you will have to load your data as valid dates and not 'yyyy-mm' values.
EDIT:
/* Test results in Teradata Studio 14.2 via JDBC connection to Teradata 13.10 */
create table MyDB.MyTable
(x1 date format 'yyyy-mm' not null default date '0001-01-01');
insert into MyDB.MyTable values (date '2013-01-01'); -- Succeeds
insert into MyDB.MyTable values ('2013-02'); -- Fails
insert into MyDB.MyTable values (date '2013-02'); -- Fails
select * from cms_work_comm_prd.t5; -- Returns to Teradata Studio (JDBC): 2013-01-01

Related

Influx DB: query return wrong time values (everything is +1)

I am struggling here on some quite simple queries on my influx DB.
every minute, a measurement from a sensor is written into the DB.
I i try to query a certain day, e.g from 00:00:00 to 23:59:59 the starting time of the query results is not 00:01:00 as I would expect, unfortunately it is 01:01:00
The epoch time value is e.g.
1578182460000000000
If i convert that value into human readable format I get (epochconverter.com):
1578182460000000000
Supports Unix timestamps in seconds, milliseconds, microseconds and nanoseconds.
Assuming that this timestamp is in nanoseconds (1 billionth of a second):
GMT: Sunday, 5. January 2020 00:01:00
Your time zone: Sonntag, 5. Jänner 2020 01:01:00 GMT+01:00
What is wrong?
The program which is writing values into the db and the system which is reading the values out of the database are both in the same timezone. (Europe/Vienna GMT+1)
the query is:
**> SELECT * FROM generalhistory WHERE time > '2020-01-05T00:00:00Z' and time < '2020-01-06' and DPName = 'Aussenbereich.Sensor.Hum' order by time asc limit 1;**
name: generalhistory
time DPName ID Manager Timestamp Value_Numeric Value_String
---- ------ -- ------- --------- ------------- ------------
1578182460000000000 Aussenbereich.Sensor.Hum 30104823 IPDriver_4 2020-01-05\ 01:01:00 99.9
Looking forward
BR
Dieter
Its quite right, the problem just when using local time (GMT+1) as filter arguments.
Therefore to get the right UTC time (GMT+0) that equal to local time (GMT+1), than the time filter must be deducted with one hour,
And the query would be following:
SELECT * FROM generalhistory
WHERE time >= '2020-01-05T00:00:00Z' -1h
and time < '2020-01-06T00:00:00Z' -1h
and DPName = 'Aussenbereich.Sensor.Hum'
ORDER BY time ASC
LIMIT 1;
Hope my answer could help.
Time syntax

Calculate week end date

I am working on HANA and I am not able to figure it out how to get the expected output.
Let me throw some light on getting the output. I am working on a procedure and my job is to get the data inserted in that particular table which I am successfully inserting, But the data is not in a correct manner.
I have 3 columns in my output named: report_date, report_week, week_end.
If suppose I start my data from 2010-01-01 the week end should start from 2010-01-06. And In this way I want my data from 2010 till 2030. It should show every week start in report_date and every week end start in week_end. The role of report_week is to show which week is going on currently.
My error output:
I have my procedure which I am posting below:
create procedure bhavya.zz_get_series()
LANGUAGE SQLSCRIPT
SQL SECURITY INVOKER
--DEFAULT SCHEMA <default_schema_name>
AS
LV_START_DATE date := '2010-01-01' ;
LV_END_DATE date := current_date ;
LV_WEEK_END NVARCHAR(10) := 'FRIDAY';
lv_report_date Date ;
Begin
TT_SERIES = (SELECT GENERATED_PERIOD_START AS REPORT_DATE,
week(add_days(generated_period_start , 2)) as report_week,
current_date as week_end --added ABHOOT
FROM SERIES_GENERATE_DATE ( 'INTERVAL 1 DAY', :LV_START_DATE,
ADD_DAYS(coalesce(current_date,:LV_END_DATE), 1)));
TT_WEEK_END = select report_week, max(report_date) as week_end
from :TT_SERIES
group by report_week ;
insert into "BHAVYA"."AFS_BASE.KPI.TABLES::DB_WEEK_SERIES"
(REPORT_DATE,REPORT_WEEK,WEEK_END)
select S.report_date, w.report_week, w.week_end
from :TT_SERIES S
left join :TT_WEEK_END W
on w.report_week = s.report_week;
end;
call bhavya.zz_get_series
Any help is really appreciated.
I think the problem is here:
week(add_days(generated_period_start , 2)) will return the week number within a year, so it will always be between 1 and 52. As week N will occur in 2010, 2011,...,2017 the max(report_date) will always be in the last year it occurs (so either 2016 or 2017).
TT_WEEK_END = select report_week, max(report_date) as week_end
from :TT_SERIES
group by report_week ;
So you have to extract the year as well from the report date and include it in the group by of the TT_WEEK_END select as well as in the join clause used for the INSERT statement.

Psql wrong date from timestamp

cloud somebody explain to me why casting timestamp to date in psql gives me wrong value? I have in my db stored time stamp value 2016-12-04 00:05:09.748000 and my machine time is in UTC, and datatype in psql is TIMESTAMP WITH TIME ZONE. If I did this,
orders.completed::date
it gives me 2016-12-03. Problem is that if i have some orders around midnight aggregate functions gives me wrong values. Is there some way how to solve it? I would appreciate any help!
Try this :
(orders.completed::timestamp at time zone 'UTC' at time zone 'America/Los_Angeles')::date;
You need to insert your timezone in query
Hm, I can't reproduce it:
b=# set timezone to "GMT+10";
SET
b=# create table edge (t timestamptz);
CREATE TABLE
b=# insert into edge select now();
INSERT 0 1
b=# select t::date, t from edge;
t | t
------------+-------------------------------
2016-12-08 | 2016-12-08 00:18:30.740132-10
(1 row)

How to insert data from text file where date column has date as "00-00-0000"

I have a text file and it contains one column as "NPA_DATE", in which few rows contain date as "00-00-000".
I am unable to load data where date is in "00-00-0000" format. I am getting the error as "Record 1: Rejected - Error on table MIS_PNPA, column NPA_DATE. ORA-01847: day of month must be between 1 and last day of month". However I want to load the data with NPA_DATE as "00-00-0000" along with other data. All other rows are getting uploaded in database. I use toad for oracle 9.0.1.8
You should add the SQLPlus tag to your original post as this is really a SQLPlus question.
Anyway alter your control file to this to set the date to NULL if it comes in as '00-00-0000' in the data file:
...
npa_date date "DD-MM-YYYY" NULLIF (npa_date="00-00-0000")
...

Query influxdb for a date

I have a table in influxdb that has a column called 'expirydate'. In the column I have afew dates e.g. "2016-07-14" or "2016-08-20". I want to select only the 2016-07-14 date, but I am unsure how?
My query is currently:
SELECT * FROM tablee where expirydate = '2016-07-14' limit 1000
But this does not work. Can someone please help me?
Assuming the value table**e** is a valid measurement...
If you are looking at selecting all of the points for the day '2016-07-14', then your query should look something like.
Query:
SELECT * FROM tablee where time >= '2016-07-14 00:00:00' and time < '2016-07-15 00:00:00'
You might also be interested in the influx's date time string in query.
See:
https://docs.influxdata.com/influxdb/v0.9/query_language/data_exploration/#relative-time
Date time strings Specify time with date time strings. Date time
strings can take two formats: YYYY-MM-DD HH:MM:SS.nnnnnnnnn and
YYYY-MM-DDTHH:MM:SS.nnnnnnnnnZ, where the second specification is
RFC3339. Nanoseconds (nnnnnnnnn) are optional in both formats.
Note:
The limit api could be redundant in your original query as it is there to impose restriction to the query from returning more than 1,000 point data.
I had to force influx to treat my 'string date' as a string. This works:
SELECT * FROM tablee where expirydate=~ /2016-07-14/ limit 1000;

Resources