SQL - Oracle compare data in two columns of one table - sql

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

Related

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

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.

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!

SQL Pivot table - filter timestamp

I have a logger table with timestamp,tagname and tagvalue fields.
Every time tag value changes, the control system writes record to the table with those 3 parameters.
Timestamp for records is not synchornized.
I want to run a pivot table query to get all data for 3 different tags to show the values of those 3 tags only.
When I run the query below, I get in return a dataset with all timestamp records in the table and lots of null values in the value fields(the SQL returns me all timestamp values).
I use the query:
SELECT *
FROM (
SELECT [timestamp],
[_VAL] AS '_VAL',
[point_id]
FROM DATA_LOG) p
PIVOT(SUM([_VAL]) FOR point_id in ([GG02.PV_CURNT],
[GG02.PV_JACKT],
[GG02.PV_SPEED],
[GG02.PV_TEMP])
) as tagvalue
ORDER BY timestamp ASC
Here's an example to the values I get in return from the SQL Server:
Results example:
Please anybody can help me how to limit the timestamp that SQL returns me only for timestamp relevant to those 3 tags and not all timestamp values in the table? (the return values list will include a record when at least one of the tags values will not be null)
If anybody have other ideas and not using PIVOT query to get the data in the format shown above - any idea will be welcome.
I think you simply want:
WHERE [GG02.PV_CURNT] IS NOT NULL OR
[GG02.PV_JACKT] IS NOT NULL OR
[GG02.PV_SPEED] IS NOT NULL OR
[GG02.PV_TEMP] IS NOT NULL
in the subquery.

Compare columns with time datatype in SQL

I have table with column with time Datatype.I want to compare the time in SQL.The main problem is when i compare any time with '00:00:00'/'00:10:00'.
For e.g. select timings from train where trn_time > '19:00:00'.
then in output i want '00:00:00' also wic I am not getting.
And I dont want to use 'Datetime' data type.
Please help.
i created a table in mysql with name 'table11'
CREATE TABLE `table11` (
`id` INT(10) NULL DEFAULT NULL,
`time_col` TIME NULL DEFAULT NULL
)
and inserted data as follows
and when i used following query
select * from table11 tbl where tbl.time_col< '19:00:00'
i got result
if you use tbl.time_col< '19:00:00' then only you will get '00:00:00' in the output.
i think in your case its better to use 'Datetime' datatype

INSERT INTO SELECT FROM ACCESS TO ORACLE

I have a table in Access named TEST_DATE1 with the column TEST_DATE that is a String datatype and the records look like 20080130. yyyymmdd
I have a table in Oracle named TEST with the column TEST_DATE that is a DATE datatype and I want the records to look like 2008/01/30 yyyy/mm/dd.
I have the two tables linked and when I usually update tables between Access and Oracle I usually do a
INSERT INTO TEST
SELECT *
FROM TEST_DATE1;
How would you convert the string to a DATE using the INSERT INTO SELECT
I have tried
INSERT INTO TEST
(SELECT TO_DATE(TEST_DATE, 'yyyy/mm/dd'))
FROM TEST_DATE1;
Thanks!
To move the records to Oracle, converting a string to a date:
INSERT INTO test (test_date)
SELECT TO_DATE(test_date, 'YYYYMMDD')
FROM test_date1
Once it's in Oracle stored as a date, you can retrieve it in any format you like:
SELECT TO_CHAR(test_date, 'YYYY/MM/DD') as test_date
FROM test;