Compare date with current_date in Oracle - sql

I can not get what is wrong in this query ( ORACLE QUERY )
SELECT *
FROM HR.CUSTOMER C
dual
WHERE CREATED_AT = current_date
;
I am getting this error
ORA-00933: SQL command not properly ended

There is a dual too many in your query.
Moreover, in Oracle current_date is not the current date for the database, but the current datetime of your session. While your database server may be in a timezone where it is currently 11 p.m., it may be next day 3 a.m. already on your PC. Whenever you spot current_date in an Oracle query it is very likely wrong.
In Oracle use sysdate for now and trunc(sysdate) for today.
select *
from hr.customer
where created_at = trunc(sysdate);

Since you already know the table and wanted to get all the columns from it, you need not use dual
The DUAL table is a special one-row, one-column table present by
default in Oracle and other database installations. In Oracle, the
table has a single VARCHAR2(1) column called DUMMY that has a value of
'X'. It is suitable for use in selecting a pseudo column such as
SYSDATE or USER.
You may just use the following query instead
SELECT *
FROM HR.CUSTOMER
WHERE CREATED_AT = current_date;

Related

Select from nothing in Oracle without referencing the dual table?

selecting from nothing in Oracle SQL happens while referencing the dual table, like
SELECT sysdate FROM dual;
Now I'd like to have a query that also works for PostgreSQL, but selecting from dual there isn't possible. I know that I can drop the whole FROM part, but then it won't work in Oracle.
I've also tried things like SELECT CURRENT_TIMESTAMP FROM VALUES(1)) V(C);, but Oracle can't do this, either.
So is there a way to select from nowhere without using the dual table in Oracle SQL?
Alternatively, create a table named dual in Postgres, made of 1 row and 1 column
create table dual as (select 1);
and you can use it in Postgres as you would in Oracle
select 'whatever' from dual;
?column?
-----------
whatever
sysdate in Oracle is a built-in function, i.e. not a database table column. You can use any table as long as the query returns precisely one row, e.g.
select sysdate from EMP where rownum < 2
If you have a small table, TABLE_B, with at least one row, you could try selecting a single row from it.
select sysdate from TABLE_B where rownum < 2;
The contents of the table don't matter because you won't be selecting any of its columns.
The below code generates a fake row in both Oracle and Postgres (10+), using the XMLTABLE function. This code is pretty weird but it doesn't require any custom objects.
--Generate a fake row in either Oracle or Postgres.
select *
from xmltable
(
--The expression syntax is different for Oracle and Postgres.
--Oracle can use a literal, Postgres must reference the XML.
--The string '' is null in Oracle but not null in Postgres.
case when '' is null then '1' else '/a' end passing '<a>1</a>'
columns test int path '.'
);
test
----
1

ORACLE SQL create date from table colum parts

I'm trying to create a select statement that returns one row (date) combining 3 column entries
My idea that doesn't work:
SELECT
TO_DATE( d_date.day_varchar +
d_date.month_varchar +
d_date.year_varchar , 'DD/MM/YYYY')
FROM d_date
Is this possible in Oracle SQL and how can I get this result?
In SQL Server there is a DATEFROMPARTS() function but I don't know that well Oracle specifics ...
Your approach seems fine. You just need to update it for Oracle syntax:
select to_date(d_date.year_varchar || d_date.month_varchar || d_date.day_varchar, 'YYYYMMDD')
It is a curious that you would have a table with date parts, but not the actual dates.

Access data from database between two dates using sql

I want to access data from database between two dates, Date format is yyyy/mm/dd. I tried a lot of queries but not succeed.
Try following query to get data between the range:
SELECT *
FROM Table_Name
WHERE From_date >= '2016-08-01' AND
To_date <= '2016-08-30'
IF you are using just vanilla sql it would just be something alone the lines of
select x from table where datecolumn between 'xdate' and 'ydate'.
Do you have a date column in the table you are looking at correct?

How do I find records that are inserted in the last four hours? - (Oracle SQLPLUS)

This is regarding Oracle SQLPLUS query language.
SELECT * FROM mytable WHERE record_date > time_4_hours_ago;
I have tried several methods, described on net and all of them did not work for me.
Tied UNIX_SYSTEM_TIME as well.
Problem seems a pretty common one, but I am struggling to get a solution that works with "Oracle" SQLPLUS.
Assuming record_date is a DATE field:
SELECT * FROM mytable WHERE record_date > sysdate - (4/24)
Try this:
select *
from mytable
where record_date > sysdate - interval '240' minute
To avoid invalid datetime values due to time zone differences you should override (record_date) value at the server side "using a database trigger for example" so as to insert the correct datetime value.

how to find fifference between 2 dates in DB2

In my database i have a TIMESTAMP field.Now I want to find the difference between the value stored in this field and the current TIMESTAMP .I want the result in days.
How can i get it?
Note:I am using DB2 database
days(TIMESTAMP)_-days(your_col)
You don't say what version of DB2 you're using.
In older versions, you have to SELECT against a table, even though you're not retrieving any columns.
SELECT days(TIMESTAMP)-days(your_col)
FROM SYSIBM.SYSTABLES
Substitute a creator and table that you have SELECT authority for.
I shall reiterate what Gilbert answered. He was correct, but I don't think the original author understood the idea. As an example:
create table test ( mycolumn timestamp )
insert into test values (current timestamp)
select * from test
select days(current timestamp)-days(mycolumn) from test
The current timestamp is a special register that DB2 has available. Days is a default function that will return the number of days component of the given timestamp counting from January 1, year 1.
The CURRENT_DATE returns the current timestamp then this can be used
DAYS(CURRENT_DATE)-DAYS(TIMESTAMP)