ORACLE SQL create date from table colum parts - sql

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.

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

Compare date with current_date in Oracle

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;

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?

Return an oracle column as timestamp

I'm in coldfusion working with data from an sql table, and using a query of queries to join the sql data to some data from an oracle database. Unfortunately, I need to order them by date, and the oracle table has two columns - DRWR_DATE which is of type DATE and TIME which is of type VARCHAR2. The two columns put into a string read 17-JUN-03 16:35:18 or something similar. I need to return these two columns as a TIMESTAMP so I can use query of queries to sort them.
Also, I think I read that a date column holds the time in Oracle anyway? I don't have much experience with Oracle so I am unsure how best to do this.
Try this:
SELECT to_timestamp(
to_char( drwr_date,'dd-mon-yy') ||' '|| time
, 'dd-mon-yy hh24:mi:ss'
)
FROM your_table
Try using TO_TIMESTAMP function:
SELECT TO_TIMESTAMP('17-JUN-03 16:35:18', 'DD-MON-RR HH24:MI:SS')
FROM DUAL;
you can try converting the column to date as the following:
TO_DATE(column,'DD-MON-YY HH24:MI:SS')
The first parameter takes your column and the second parameter specifies the date format that is used
If all you have to do is order by date, all you need is an order by clause.
order by drwr_date, time
You don't have to cast these to anything, unless you need to do so for another reason. Remember that a date datatype is essentially a floating point number. This, "17-JUN-03" is simply how your client is displaying it.

SQL to return fixed data in both SQL Server and Oracle

I need a common select statement that returns a fixed value / row without the need of tables, which has to work with both Oracle & Sql Server.
eg for Oracle I know I can use:
select 'O' AS INDICATOR from DUAL;
But this won't work on Sql Server.
Can this be done with the same SQL on both Oracle & SQL Server?
AFAIK, you'll need different queries, unless you can find a table that exists both on the SQL Server and on the Oracle Server.
Oracle uses the DUAL table for dummy queries, while the syntax to just select a constant on SQL server is a bit simpler:
select 'O' as Indicator
will return a one-row recordset.
P.S. If you intend to write just standard SQL and have it work on both SQL Server and Oracle, note that there are lots and lots of differences, even if you do not use database-side code (stored procedures and functions).
Off the top of my head, some things that are different:
Case statement syntax
NVL vs IsNull
Null sorting behaviour
Data conversion functions
String manipulation functions
etc, etc.
You can't select data in Oracle without from statement. So you need to have a table in Oracle (common practice is to use standard table - Dual). The best solution if you really need to run same query on both database servers is to create Dual table with only one row in MS SQL. But really it's better to use different queries for different servers (maybe via some abstraction layer).
Use a common table expression (CTE) e.g.
WITH D (INDICATOR)
AS
(
SELECT *
FROM (
VALUES ('O')
) T (c1)
)
SELECT INDICATOR
FROM D;
Or more simply in line:
SELECT *
FROM (
VALUES ('O')
) D (INDICATOR)
You can create the DUAL table in SQL Server:
CREATE TABLE DUAL (DUMMY NVARCHAR(1) NOT NULL);
INSERT INTO DUAL VALUES ('X');
and then use the same query as in Oracle:
select 'O' AS INDICATOR from DUAL;