When inserting timestamp to a table it inserts as date - Redshift - sql

I have table1 with data on it and in every row there is "period_start" and "period_end", both columns are dates. Ex: '2021-06-30'.
I have a table2 I created with 2 columns of the same name, this time they are saved as timestamp (client wants as timestamp).
I ran a query that inserts the results of table1 to table2 but converts the date columns to timestamp. For some reason I couldn't understand, the results in table2 are all dates and not as timestamp.
Creating table2 (removed other columns):
CREATE TABLE IF NOT EXISTS table2
(
,period_start timestamp ENCODE zstd
,period_end timestamp ENCODE zstd
)
Inserting from table1:
insert into table2 (period_start ,period_end)
select
cast(to_date(period_start,'YYYY-MM-DD HH24:MI:SS') as timestamp),
cast(DATEADD(millisecond, -1,last_day(period_start + 75) + 1) as timestamp)
from table1;
When running the same select I see the results as timestamp perfectly, when querying from table2 the results are saved as date.

Turns out the queries are indeed fine (I'm glad). It was a problem with DBeaver that for some reason didn't show them as timestamp. I had to exit and enter the program and then it worked.

Related

SQL - Oracle compare data in two columns of one table

Can we compare the columns of same table and get a result by selecting all the rows where both columns data does not match. Compare CREATE_DATE and UPDATE_DATE
Table with data
Expected output:
SELECT *
FROM Table
WHERE TIME_CREATED <> UPDATE_DATE
There should be no issue comparing date to timestamp, please update your OP if you have tried this already
If there is an issue, read this post and try something like this:
Oracle comparing timestamp with date (also shows use of truncate to disregard time of day)
SELECT *
FROM Table
WHERE to_timestamp(TIME_CREATED,'D/MM/YYYY HH:MM:SS') <> UPDATE_DATE
OR
SELECT *
FROM Table
WHERE TIME_CREATED <> TO_DATE(UPDATE_DATE,'DD.MM.YYYY:HH24:MI:SS')
You might have to play with the timestamp format a bit, see https://www.akadia.com/services/ora_date_time.html

Timestamp decrease the hour in insert overwrite

I have been work with Sqoop, hive and Impala.
My Sqoop Job get a field from SQL Server with the format datetime to write in a TABLE1 stored as textfile. The field in TABLE1 have the timestamp format.
After this, I created a HQL script using INSERT OVERWRITE TABLE2 ... SELECT TABLE1.
The field in TABLE2 have the Timestamp format too, but the time increased in 1 hour, I don't know why.
All the tables was created previously. How can I fix this?
The difference might come from different time zones (local time, server time, utc, ...)
However, you can fix the wrong values in the database with
DATEADD(interval, number, date);
Also see: https://www.w3schools.com/sql/func_sqlserver_dateadd.asp for more

Oracle incremental query in a table with no ID or timestamp

I need to regularly extract data from an Oracle 11 table using sqlplus. For example, I need every day to extract the new rows inserted into that table.
On a table with a primary key such as RECORD_ID (assuming it is inserted incrementally), that query would be:
SELECT * from TABLE WHERE RECORD_ID > &LAST_RECORD_ID_FROM_PREVIOUS_QUERY
On a table with a RECORD_DATE timestamp, this could similarly done like:
SELECT * from TABLE WHERE RECORD_DATE > &LAST_RECORD_DATE_FROM_PREVIOUS_QUERY
My question is: how do you do this when you have no timestamps and no incremental column you could use? Can this be achieved with ROWID?
One way would be to enable flashback and then you could do:
SELECT * FROM table_name
MINUS
SELECT * FROM table_name AS OF TIMESTAMP SYSTIMESTAMP - INTERVAL '1' DAY;
As I suspected there isn't any easy solution. It has to be one of:
Adding an identity or timestamp column
Do a diff using flashback
Add a trigger on insert on the table
Unfortunately none of which is practical in my environment. Case closed!

Display Now date and Time in SQl table column

I want to be able to have todays date and time now in a table column
If my table is say Table1, basically it should display the time and date when
SELECT * FROM Table1 is run.
I've tried the following but they just show the time from the moment in time I assign the value to column
ALTER TABLE Table1
ADD TodaysDate DateTime NOT NULL DEFAULT GETDATE()
and
ALTER TABLE Table1
ADD TodaysDate DateTime
UPDATE Table1
SET TodaysDate = GETDATE()
Hope this is clear. any help is appreciated.
Thanks
In SQL Server you can use a computed column:
alter table table1 add TodaysDate as (cast(getdate() as date));
(use just getdate() for the date and time)
This adds a "virtual" column that gets calculated every time it is referenced. The use of such a thing is unclear. Well, I could imagine that if you are exporting the data to a file or another application, then it could have some use for this to be built-in.
I hope this clarifies your requirement.
The SQL Server columns with default values stores the values inside the table. When you select the values from table, the stored date time will be displayed.
There are 2 options I see without adding the column to the table itself.
You can use SELECT *, GETDATE() as TodaysDate FROM Table1
You can create a view on top of Table 1 with additional column like
CREATE VIEW vw_Table1
AS
SELECT *, GETDATE() as TodaysDate FROM dbo.Table1
then you can query the view like you mentioned (without column list)
SELECT * FROM vw_Table1
This will give you the date and time from the moment of the execution of the query.

Hadoop - Formatting dates when creating tables

How to format dates during the process of creating Hive tables?
I've currently been dumping some data into a discovery environment at work and storing dates as string, because if I format them as a DATE or TIMESTAMP the values are null.
Here's what the raw data looks like:
12/07/2016 05:07:28 PM
My understanding is that Hive accepts dates in this format
yyyy-mm-dd hh:mm:ss
I can format these using a select statement:
select id, receipt_dt, from_unixtime(unix_timestamp(receipt_dt ,'MM/dd/yyyy'), 'yyyy-MM-dd') as app_dt from MySchema.MyTable where app_num='123456'
How can I add in the statement
from_unixtime(unix_timestamp(receipt_dt ,'MM/dd/yyyy'), 'yyyy-MM-dd')
How can I add this in to the generic CREATE EXTERNAL STATEMENT below so that I no longer have to store dates as a string, or use an ALTER TABLE statement to change the formatting?
CREATE EXTERNAL TABLE IF NOT EXISTS MySchema.My_New_Table
( Field1 Format,
Field2 Format,
Field 3 Format,
)
.......
Use MyTable as staging table with raw data and create final/target table my_new_table with transformations i.e, date format...it will be EDW kind of process...
example:
CREATE EXTERNAL TABLE IF NOT EXISTS MySchema.My_New_Table
( Field1 int,
Field2 string,
Field3 date
)
... more definitions....
AS
select id, receipt_dt,
cast(from_unixtime(unix_timestamp(receipt_dt ,'MM/dd/yyyy'), 'yyyy-MM-dd') as date) as app_dt
from MySchema.MyTable ;
NOTE: This is not tested statement. You may need to try and edit and try...but you got the idea...
Then inserting delta should be similar process...
INSERT INTO TABLE MySchema.My_New_Table
AS
select id, receipt_dt,
cast(from_unixtime(unix_timestamp(receipt_dt ,'MM/dd/yyyy'), 'yyyy-MM-dd') as date) as app_dt
from MySchema.MyTable where <<conditions>>;