Cast Date Column in the Redshift COPY Command - amazon-web-services

My data has the date in the "02JAN2020" format and I want to load the data using the COPY Command
copy test.Demographics from 's3://xyz-us-east-1/Blu/'
access_key_id ,’Access_Key_ID>’
secret_access_key ’<Secret_Access_Key>’
delimiter ',' dateformat 'auto'
GZIP;
The column data type is a date but it's still failing. I checked the stl error logs and it's the date formate issue.
I want the value in the column as 2020-01-02 and not 02Jan2020

Specify the date format with
DATEFORMAT 'DDMONYYYY'

Related

Convert text column to timestamp in Amazon redshift

I have a text field "completed_on" with text values "Thu Jan 27 2022 11:55:12 GMT+0530 (India Standard Time)".
I need to convert this into timestamp.
I tried , cast(completed_on as timestamp) which should give me the timestamp but I am getting the following error in REDSHIFT
ERROR: Char/varchar value length exceeds limit for date/timestamp conversions
Since timestamps can be in many different formats, you need to tell Amazon Redshift how to interpret the string.
From TO_TIMESTAMP function - Amazon Redshift:
TO_TIMESTAMP converts a TIMESTAMP string to TIMESTAMPTZ.
select sysdate, to_timestamp(sysdate, 'YYYY-MM-DD HH24:MI:SS') as seconds;
timestamp | seconds
-------------------------- | ----------------------
2021-04-05 19:27:53.281812 | 2021-04-05 19:27:53+00
For formatting, see: Datetime format strings - Amazon Redshift.

aws athena query result in json format

I create aws athena table that contain some rows
example of data:
first_name | age
=================
a 20
b 30
c 35
When I query the data I the result are saved in CSV format in S3.
SELECT * FROM table1
I would query the data and get the result in JSON format.
The reason is that I should transfer that JSON data to another application for another process.
Is there a way to get query result in JSON format?

How to copy specific columns from a csv into redshift table using lambda

I am trying to load a csv file ins s3 into redshift using aws copy command in lambda. The problem is i have more columns in csv than in redshift table.
so whenever i trigger lambda fnction i get the error "Extra columns found"
how to load specific columns from csv
my csv files is of form
year, month, description, category,SKU, sales(month)
and my redshift table is of form
year month description category SKU
-----------------------------------
my copy command is as follows
COPY public.sales
FROM 's3://mybucket/sales.csv'
iam_role 'arn:aws:iam::99999999999:role/RedShiftRole'
delimiter ','
ignoreheader 1
acceptinvchars
You can specify the list of columns to import into your table - see COPY command documentation for more details.
COPY public.sales (year, month, description, category, SKU)
FROM 's3://mybucket/sales.csv'
iam_role 'arn:aws:iam::99999999999:role/RedShiftRole'
delimiter ','
ignoreheader 1
acceptinvchars

Amazon Athena Convert String to Date

I am looking to convert the following string: mmm-dd-yyyy to a date: yyyy-mm-dd
e.g
Nov-06-2015 to 2015-11-06
within Amazon Athena
I would do date_parse. Adjust your regex accordingly.
select date_parse('Nov-06-2015','%b-%d-%Y')
2015-11-06 00:00:00.000
refd:https://prestodb.io/docs/current/functions/datetime.html
You can also use cast function to get desire output as date type.
select cast(date_parse('Nov-06-2015','%M-%d-%Y') as date);
output--2015-11-06
in amazon athena
https://prestodb.io/docs/current/functions/datetime.html
used date parse to parse string and cast to convert 2015-11-06 00:00:00.000
into 2015-11-06

copy timestamp from AWS iot rule to Amazon redshift table column

My current iot design is iot > rule > kinesis firehose > redshift
I have iot rule as
SELECT *, timestamp() AS timestamp FROM 'topic/#
I get json message something like below
{
"deviceID": "device6",
"timestamp": 1480926222159
}
In my redshift table I have a column eventtime as Timestamp
Now i want to store the json timestamp value to eventtime column, but it gives me error as it needs
TIMEFORMAT AS 'MM.DD.YYYY HH:MI:SS
for timestamp. So how to covert the iot rules timestamp to redshift timestamp?
There is no direct way to converting epoch date value while inserting it to Redshift table Timestamp datatype column.
I have created a column with Bigint datatype and inserting epoch value directly to this column.
After that I am using Quicksight for analytics so I can edit my dataset and create New calculated field for this column and use Qucksight function as below
epochDate(epoch_date)
which converts the epoch value to timestamp field.
One can use similar functions like
SELECT
(TIMESTAMP 'epoch' + myunixtimeclm * INTERVAL '1 Second ')
AS mytimestamp
FROM
example_table