Teradata error - importing timestamp column with null values - teradata-sql-assistant

I have a timestamp column in a delimter file in the format '12/04/2018 15:38:42' and I am trying to import this file thro Teradata SQL assistant. I have defined the target table with this column as
SYSTEM_DT Timestamp(0) FORMAT 'MM/DD/YYYYBHH:MI:SS'
I am using the below statement to import, its loading fine until it detects a null value on the file for this column. I am getting 'Invalid timestamp' error.
INSERT INTO TARGET_TABLE
VALUES (CAST(? AS TIMESTAMP(0) FORMAT 'MM/DD/YYYYBHH:MI:SS'),?,?,?)
So I tried below, nothing worked -
1. CAST(COALESCE(? , '01/01/1900 00:00:00') AS TIMESTAMP(0) FORMAT 'MM/DD/YYYYBHH:MI:SS')
2. COALESCE(CAST(? AS TIMESTAMP(0) FORMAT 'MM/DD/YYYYBHH:MI:SS'), CAST('01/01/1900 00:00:00' AS TIMESTAMP(0) FORMAT 'MM/DD/YYYYBHH:MI:SS'))
Please help.

SQL Assistant expects NULL to be represented by whatever string value you have specified in Tools / Options / Data Format as "Display this string for Null data values".
For example, if NULL is indicated by an empty string in your input dataset, delete all the characters from that option setting to have SQL Assistant send NULL to the database.
Alternatively you could use a CASE or NULLIF statement inside the CAST to have the database convert an empty string to NULL.

Related

How to convert date from MM/dd/yyyy to yyyy-MM-dd in MySQL? Error: 1411. Incorrect datetime value: '' for function str_to_date

I just need help on how to convert date from MM/dd/yyyy to yyyy-MM-dd in MySQL?
I already asked this question a few months ago, and I was able to successfully convert it. Please see thread here.
However, I'm having difficulty with this new dataset. Previously, when I created thie previous post, ALL of my dataset were in the format MM/dd/yyyy, to which I successfully converted to yyyy-MM-dd using the following query:
Update human_resources.timekeeping Set Actual_Date = str_to_date(Actual_Date,'%m/%d/%Y');
However, for this new problem that I am encountering, I have a mix of data format in one table. Some time entries are in the format of MM/dd/yyyy while some are in the format yyyy-MM-dd. (Datatype is VARCHAR)
I need to change my old format MM/dd/yyyy to my new format yyyy-MM-dd.
I'm trying to use the same query:
Update human_resources.time_entry_request Set Actual_Date = str_to_date(Actual_Date,'%m/%d/%Y');
but I'm encountering the following error:
Error Code: 1411. Incorrect datetime value: '2021-05-08' for function
str_to_date
On the other hand, I tried to use this query: (Specified the actual date in the where clause)
Update human_resources.time_entry_request Set Actual_Date = str_to_date(Actual_Date,'%m/%d/%Y') where Actual_Date = "05/31/2021";
and it was successful. (Changing specific MM/dd/yyyy time entry to format yyyy-MM-dd).
I believe the error that is causing this problem is that the dataset already contains the new and accurate format which is the yyyy-MM-dd.
It also happens vice versa, tried to change the new format into the old one so I can change them to the new one altogether.
PS. The data type for Actual_Date is VARCHAR. I tried changing it to DATE() but am receiving this error:
Executing:
ALTER TABLE `human_resources`.`time_entry_request`
CHANGE COLUMN `Actual_Date` `Actual_Date` DATE NULL DEFAULT NULL ;
Operation failed: There was an error while applying the SQL script to
the database.
ERROR 1292: Incorrect date value: '06/04/2021' for
column 'Actual_Date' at row 17
SQL Statement:
ALTER TABLE `human_resources`.`time_entry_request`
CHANGE COLUMN `Actual_Date` `Actual_Date` DATE NULL DEFAULT NULL
Is there a way I can change the old format to the new one, please? Would really appreciate your help! The dataset contains a mix of MM/dd/yyyy while some are in the format yyyy-MM-dd
My previous post contains ALL MM/dd/yyyy so I was able to convert it successfully.

What type of column will support datetime with offset?

During liquibase script execution I'm filling the table with data. Data has one column with the next value: 2017-10-03T07:11:00.0000000Z which I'm struggling with to parse into the column:
liquibase.exception.MigrationFailedException: Migration failed for change set /changelog/00000000000001.xml::00000000000001::IAmUser:
Reason: java.lang.NumberFormatException: For input string: "0000000Z"
I've tried multiple column types but none of them worked: datetime, datetime2, timestamp (datetimeoffset doesn't work with MySQL).
How can I parse this date into SQL column?
Use datetime(6) / timestamp(6) where 6 is the precision for fractional seconds. Also, leave out the Z from the end of the date string.

Incorrect string value errors in AES_ENCRYPT/AES_DECRYPT

I'm introducing myself to the encryption functions in MYSQL.
Just wrote a simple SQL statement to add an encrypted entry into a field
INSERT INTO test_table (field1) VALUES(aes_encrypt('fieldentry','password'))
When I execute the SQL I get the following error
Error 1366: Incorrect string value: '\xC7\xE13\xC4\xF4!...' for column 'field1' at row 1 SQL Statement - CHANGE COLUMN field1 VARCHAR(255) NOT NULL COMMENT ''
Now I've read it may have something to do with CHARACTER SET, and tried changing it from utf8-default collation to utf8mb4 - default collation as recommended, but that didnt make any difference.
I've also tried changing the column type from VARCHAR to VARBINARY. The SQL statement then ran successfully, however, when I tried the following to retreive the data:
SELECT AES_DECRYPT(field1, '12345') AS endata FROM test_table
Do Until rst.EOF
Response.Write(rst("endata"))
rst.movenext
Loop
The loop runs but no values are returned (blank lines)
I'm just looking for a straightforward what to encrypt and then decrypt my data using a password in the function AES_ENCRYPT/AES_DECRYPT.
AES_ENCRYPT() encrypts a string and returns a binary string.
AES_DECRYPT() decrypts the encrypted string and returns the original
string.
So you should change the type of field1,firstname from VARCHAR(255)to VARBINARY(255). It will fix the issue..
EDIT : For type missmatch.. please try this..
SELECT *,
CAST(AES_DECRYPT(field1, 'mypassword') AS CHAR(50)) end_data
FROM user
Hope this helps..

CSV import via PHPMyAdmin, blank timestamp results in 0000-00-00 00:00:00

The destination table has a column named updated and is set as:
Name: updated
Type: timestamp
Attributes: on update CURRENT_TIMESTAMP
Null: No
Default: CURRENT_TIMESTAMP
Extra: ON UPDATE CURRENT_TIMESTAMP
The source data is in a CSV, where the updated field is blank for every row, e.g.
id,item_name,updated,quantity
1,car,,4
2,truck,,5
3,chair,,5
After importing using PHPMyAdmin, I expect to see the updated column filled with the current date/time when the import was executed, however, I get all 0000-00-00 00:00:00 instead.
MySQL version: 5.5.30. This result is the same with MyISAM and InnoDB.
That is because the field is supplied. It is supplied as empty string, which is something different than NULL or default.
Empty strings will be converted to 0 when cast to a numeric value. And timestamp 0 formatted as a date is 0000-00-00 00:00:00.
You can either run UPDATE table SET updated=NOW() after importing (if you always import a complete set) or remove the entire column from the CSV.
How do you do a SET updated in phpmyadmin?
With the column described above, assign each value of the update column in the CSV as "NULL". The date of csv import is then added to mySQL.
One strange thing I noticed was errors in defining in the column. Even with an empty table, it was telling me that the default value for TIMESTAMP could not be CURRENT_TIME. To fix this, I banged my head on the table three times, changed the type to datetime, saved, then re-edited, and was allowed to change the type to TIMESTAMP.
The below steps works for setting timestamps to NULL.
1. Make the default value of the column - updated as NULL (anyhow, you want the timestamp to be empty)
2. In csv file type NULL instead of keeping the field blank
This method can also be used for auto increments

MySQL - How to parse a string value to DATETIME format inside an INSERT statement?

I have a MySQL database, with a column that is date type DATETIME.
I am getting a string value for a date / time from an external application. That string value looks like this:
'5/15/2012 8:06:26 AM'
MySQL throws an error on the INSERT: "Error. Incorrect datetime value". My workaround was to change the column type to VARCHAR, which works, but I really need the data as a proper Date & Time for future use.
I researched accepted formatting for MySQL DATETIME values, and found that MySQL wants the DATETIME format as 'YYYY-MM-DD HH:MM:SS'.
I can't change the external application to reformat the date / time string in a format, so my only chance is to deal with it.
What I need to do, I think, is parse the existing string, using MySQL syntax, inside of my INSERT statement, but I'm not sure how to do that. I have some idea that I need to use a SELECT clause, perhaps STR_TO_DATE, somehow in combination with my INSERT statement.
Here is my current INSERT statement. I removed the other fields that are not causing a problem, just to make the example clean.
INSERT INTO tblInquiry (fldInquiryReceivedDateTime) VALUES ('5/15/2012 8:06:26')
Use MySQL's STR_TO_DATE() function to parse the string that you're attempting to insert:
INSERT INTO tblInquiry (fldInquiryReceivedDateTime) VALUES
(STR_TO_DATE('5/15/2012 8:06:26 AM', '%c/%e/%Y %r'))