I have a SAS datetime stored in a macro variable and try to write that into a timestamp column in teradata using proc sql. E.g. I can write that into a string column with %BQUOTE('¯ovariable') and it returns 14APR2020:06:47:20. But I am failing to convert that into a timestamp and write into timestamp field in teradata. Cast as timestamp did also not work :( It returns ERROR: Teradata execute: Invalid timestamp.
My Code:
INSERT INTO MYTABLEWITHDATETIMEFIELD
VALUES (
%BQUOTE('¯ovariable')
)
Could you help me out? THANKS!
Best regards
Flo
Related
I'm new to TIBCO BW6. This is my scenario. I have a .csv file, and one of my columns is a string in this format: '31/08/2021 15:18:00'
I created a process that reads my file and inserts a new row into my database. I have a problem with the date.
In my palette JDBC Update, the date is a timestamp. When I match the input, the right pattern for matching my string in datetime is pattern yyyy-mm-dd hh:mm:ss.
To do this kind of thing the best solution is generally to use the SQL database formatting functions in your SQL query.
Something like this (for ORACLE) :
insert into table_name
(my_date_field)
values (TO_DATE('2022/01/23 11:25:44', 'yyyy-mm-dd hh24:mi:ss'));
In BusinessWorks the query would look like this and you would have to map the value of the field corresponding to the '?' with your actual timestamp :
insert into table_name
(my_date_field)
values (TO_DATE(?, 'yyyy/mm/dd hh24:mi:ss'));
In Sqlite I want to extract the date and time portions of a DateTime field separately in a view and return them also as a datetime, not strings. I've tried Cast, Date(), datetime(), but they all return strings.
I've read the SQLite documentation and understand how there is not an actual Date data type. Yet a Table field defined as DateTime is able to be parsed as a Date by an Excel query, but calculations on that field are not. I'm trying to do all data prep in the database view.
My data has the following field taken directly from the table definition:
LastModifiedDate datetime
I want the date (without time) to have the same DateTime data type as LastModifiedDate, not Text, because I use this view in many spreadsheets. I can apply Excel Date functions and formatting to LastModifiedDate field directly as returned from the ODBC query to Excel, and want to do the same to the Date-only part. I don't want to have to put a string-to-date conversion in every spreadsheet when I know it can get the date natively from Sqlite in LastModifiedDate.
SELECT LastModifiedDate,
date(LastModifiedDate) as Datepart,
cast(LastModifiedDate as numeric) as Date2
FROM Transactions
LastModifiedDate Datepart Date2
2019-07-28 18:22:38.9165394 2019-07-28 2019
LastModifiedDate in the above query is interpreted in Excel as a date to which date formats and date functions can be applied with no further processing required. Datepart above is returned as Text to Excel, and I can't apply date functions and formats without further pre-processing in Excel. I would like Datepart to be interpreted a date in Excel just as LastModifiedDate is.
I'm looking at the ch-werner.de sqliteodbc-0.9998. It will return an ODBC TIMESTAMP type only if the column decltype starts with timestamp or datetime. It returns ODBC TIME only for decltypes starting with time and ODBC DATE only for decltypes starting with date.
sqlite3 provides this decltype only for result table columns that are direct database column references. So if your SELECT statement has some expression that is more than a plain column reference, the decltype is lost. sqlite3 works like this at least up to version 3.39.0. It is documented.
The CAST expression converts the value of given expression to a storage classes by the determined affinity of the given declared type, but does not assign decltype to the result.
If you want to see the decltypes for query columns, you can use the sqlite3 cli and give it command .stats 2. Then it'll output the column declared types for each statement it executes.
If the decltype is found, the sqliteodbc-0.9998 will always parse string values into ODBC types. If DSN Option JDConv is enabled, it'll also parse floating point julianday values (whether provided as float or a string of a float) into ODBC types and when writing it'll write floating point into database.
If you can afford to change the schema, you can add a generated virtual column. This is cheap in storage, because data is not affected, but it costs when you query the column. This column can calculate other column into the values and decltypes you need for ODBC.
ALTER TABLE data ADD COLUMN
Datepart date AS (date(LastModifiedDate))
Then to get the Datepart, you simply query the column.
SELECT Datepart FROM data
I am running the following proc sql to pull out the max date.
Proc sql;
Connect to TERADATA (login details);
Create table dates as
Select * from connection to TERADATA
( select max (date1,'2011-12-31') from table1
);
Quit;
Error:
Syntax error: expected something between the word 'date1' and ','
Can someone help me where I am doing wrong?
In most flavors of SQL, the max function is an aggregation function, which only takes one argument and then takes the column (or whatever is passed to it) and chooses the maximum value from that column.
SAS is different in that it overloads max to also work as a row-level function.
To do this you could do:
Proc sql;
Connect to TERADATA (login details);
Create table dates as
Select max(date1,'2011-12-31') from connection to TERADATA
( select date1 from table1
);
Quit;
Which pulls it out of the teradata and into SAS where it's legal to do that.
You can do this in-database (push down optimization) with Teradata if you use the GREATEST function and cast the dates to INTEGER:
Proc sql;
Connect to TERADATA (login details);
Create table dates as
Select * from connection to TERADATA
( select GREATEST (CAST(date1 AS INTEGER), CAST(CAST('2011-12-31' AS DATE) AS INTEGER)) from table1
);
Quit;
Note: I double casted the second parameter to be on the safe side, even though it is being passed to Teradata in an implicit ANSI date format. If your date is nullable in the table (date1), there may be some obstacles with COALESCE.
I have a vendor oracle DB storing values to a column creation_timestamp as timestamp(6) when i create an extract of that table and load it to HDFS and create hive schema with that column defined as timestamp it does not populate the field. However if I use string it is all there. I have other MSSQL datetime data that successfully stored as timestamp in hive so I am just a little unsure why the oracle timestamp data is not populating.
The oracle timestmap looks like this: 07-JAN-15 05.55.20.732754000 PM
I have a feeling hive does not like the month as letters as the documentation states:Strings: JDBC compliant java.sql.Timestamp format "YYYY-MM-DD HH:MM:SS.fffffffff" (9 decimal place precision)
How can I convert the oracle timestamp(6):07-JAN-15 05.55.20.732754000 PM to the java.sql.Timestamp format shown above?
I was able to alter my session and then export the table, hive has now accepted it as a timestamp
ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF';
07-JAN-15 05.55.20.732754000 PM is now: 2015-01-07 17:55:20.732754000
Use to_date in hive.
Example :
select TO_DATE(from_unixtime(UNIX_TIMESTAMP('7-MAR-13', 'd-MMMM-yy'))) from table_name
Refer : date functions
create table abc_test(
id number,
rv timestamp
);
insert into abc_test(id,rv) values (1, CURRENT_TIMESTAMP);
Is there any better way to do it?
Can the timestamp be stored in the database in some binary format like "01x0000000000046565".
In Microsoft sql, this can easily be done as below :
create table xyz_test(id1 timestamp, name varchar(50))
insert into xyz_test (Name) values('person1')
select * from xyz_test
The above query will show some binary form for the timestamp column and "person1" in the name column
How to get the same result in the oracle?
It is indeed stored in a binary format -- the tool that you are using translates it when it is retrieved.
Display of dates and timestamps is dependent on tool configuration, and on session settings etc.. You can read the timestamp in whatever format you like -- consult the documentation for To_Char(datetime).