Short test statements in PL-SQL? - oracle

In SQL server I can print out the value of something with a select statement.
SELECT 'xyz'
SELECT GetDate()
Can I do something similar in Oracle without adding FROM <tablename>?

This is the purpose of the dual table. Oracle supplies the Dual in every database and it's accessible, by default, to everyone that connects. It's a single-row, single column table that is useful for testing expressions and pseuducolumns against. Example
SELECT 'xyz' from dual;
SQL> select user,sysdate,lower(user) loweruser, 10*1023 from dual;
USER SYSDATE LOWERUSER 10*1023
---------- ---------- ---------- ----------
NKODNER 22-NOV-11 nkodner 10230

There is a dual dummy table in Oracle, so try:
SELECT GetDate() FROM dual

You can't. You must use DUAL fictious table
To get current system date, you would type
SELECT SYSDATE FROM DUAL

Related

string to timestamp conversion plsql

I've inherited some data from an external source which is a timestamp. This was put into warehouse by someone as a varchar2. I need to convert this to a legitimate timestamp but am unsure how. This is how the string looks. "2021-04-23T11:02:17.00Z".
Would appreciate some help.
PS Ideally, I'd also like to know how to trunc this to a more traditional date format of DD-MMM-YYYY e.g. 21-Jan-2021 or even DD-MM-YYYY is fine.
Use to_timestamp_tz() to get the corresponding timstamp with time zone, convert it to the timezone you want it in (for example sessiontimezone) with AT TIME ZONE and cast() that to a timestamp.
SELECT cast(to_timestamp_tz('2021-04-23T11:02:17.00Z', 'YYYY-MM-DD"T"HH24:MI:SS.FF2:TZR') AT TIME ZONE sessiontimezone AS timestamp)
FROM dual;
db<>fiddle
Shouldn't be too difficult. Extract the "date" part, apply TO_DATE function to it (with appropriate format mask) and - that's all. It means that you should "stop" at the date_value in the following query. The last, final_result is a string again, just formatted as you wanted.
SQL> with test (col) as
2 (select '2021-04-23T11:02:17.00Z' from dual)
3 select substr(col, 1, 10) string,
4 --
5 to_date(substr(col, 1, 10), 'yyyy-mm-dd') date_value,
6 --
7 to_char(to_date(substr(col, 1, 10), 'yyyy-mm-dd'), 'dd-mm-yyyy') final_result
8 from test;
STRING DATE_VALUE FINAL_RESU
---------- ---------- ----------
2021-04-23 2021-04-23 23-04-2021
SQL>
In order to avoid that "operation", you might even create a view. For example:
This is a table you currently have:
SQL> create table test as
2 (select 1 id, 'Littlefoot' name, '2021-04-23T11:02:17.00Z' col from dual);
Table created.
Create a view, re-using code I posted above:
SQL> create or replace view v_test as
2 select id, name,
3 to_date(substr(col, 1, 10), 'yyyy-mm-dd') col
4 from test;
View created.
Select from it:
SQL> select * from v_test;
ID NAME COL
---------- ---------- ----------
1 Littlefoot 2021-04-23
Want another format? No problem:
SQL> alter session set nls_date_format = 'dd-mon-yyyy';
Session altered.
SQL> select * from v_test;
ID NAME COL
---------- ---------- -----------
1 Littlefoot 23-apr-2021
SQL>
Or, apply TO_CHAR to view's col column (also demonstrated in code I posted first; see the final_result).
You can use the TO_TIMESTAMP function to do this.
Try:
TO_TIMESTAMP('2021-04-23T11:02:17.00Z', 'YYYY-MM-DDTHH:MI:SS')
You can read more about this function in their Documentation

why do sqlplus variables act funny with a period?

why do sqplus variables act funny when attempting to use a period after them?
SQL> set define on
SQL> accept goo char prompt 'goo: ';
goo: mygoo
SQL> select '&goo' from dual;
old 1: select '&goo' from dual
new 1: select 'mygoo' from dual
MYGO
-----
mygoo
SQL> select '&goo something' from dual;
old 1: select '&goo something' from dual
new 1: select 'mygoo something' from dual
MYGOOSOMETHING
---------------
mygoo something
SQL> select '&goo.something' from dual;
old 1: select '&goo.something' from dual
new 1: select 'mygoosomething' from dual
MYGOOSOMETHIN
--------------
mygoosomething
SQL> select '&goo..something' from dual;
old 1: select '&goo..something' from dual
new 1: select 'mygoo.something' from dual
MYGOO.SOMETHIN
---------------
mygoo.something
why do I have to double up on periods? is there some better way?
That's not funny at all, but expected default behavior.
SET CONCAT character is a period (unless you set it to some other character). If you want to add a period right after the substitution variable, you have to use two consecutive periods.
If you set concat character to e.g. #, then period acts normally:
SQL> set concat '#'
SQL> select '&goo.' from dual;
old 1: select '&goo.' from dual
new 1: select 'mygoo.' from dual
'MYGOO
------
mygoo.
If you set it back to a period, it acts funny again:
SQL> set concat '.'
SQL> select '&goo.' from dual;
old 1: select '&goo.' from dual
new 1: select 'mygoo' from dual
'MYGO
-----
mygoo
SQL> select '&goo..' from dual;
old 1: select '&goo..' from dual
new 1: select 'mygoo.' from dual
'MYGOO
------
mygoo.
Finally, back to #, which also acts funny and you have to use two of them:
SQL> set concat '#'
SQL> select '&goo#' from dual;
old 1: select '&goo#' from dual
new 1: select 'mygoo' from dual
'MYGO
-----
mygoo
SQL> select '&goo##' from dual;
old 1: select '&goo##' from dual
new 1: select 'mygoo#' from dual
'MYGOO
------
mygoo#
SQL>

Retrieve Selected Column from the oracle query

I would require your help to retrieve the selected columns from the oracle query
like
SELECT col1,col2,col3+col4 as col3_4_sum,col5*col6 as col5_6_mul from tab1;
I want to retrieve the below output as
Details
==========
Col1
Col2
Col3+Col4
Col5*Col6
Not the alias ,because this can be easily retrieved from dbms_sql.describe_columns oracle utility ,Can someone suggest some data dictionaries to retrieve this
EDIT :Different forms,but i need a final output column only
Query 2:SELECT col1,col2,col3+col4 as col3_4_sum,col5*col6 as col5_6_mul
FROM (SELECT col1,col2,col3,col4,col5,col6 FROM tab1)
Query3:WITH tab as (SELECT col1,col2,col3,col4,col5,col6 FROM tab1)
SELECT col1,col2,col3+col4 as col3_4_sum,col5*col6 as col5_6_mul
FROM tab;
I hope this is what you looking for.
SELECT trim(regexp_substr('col1,col2,col3+col4,col5*col6', '[^,]+', 1, LEVEL)) str_2_tab
FROM dual
CONNECT BY LEVEL <= regexp_count('col1,col2,col3+col4,col5*col6', ',')+1
/
But the simplest way is using chr(10) to replace the commas:
SELECT REPLACE('col1,col2,col3+col4,col5*col6', ',', chr(10)) str_2_tab FROM dual
Output is the same:
STRING_2_TAB
------------
col1
col2
col3+col4
col5*col6
Good resources to check for Parameters and Arguments:
https://www.techonthenet.com/oracle/functions/regexp_substr.php
https://oracle-base.com/articles/misc/regular-expressions-support-in-oracle#example1

Store time while inserting data into oracle table

In one of my column, I want to store time also in below condition. Here is the query below.
APPROVED_DATE = CASE WHEN PAPPROVED_BY IS NULL THEN NULL ELSE SYSDATE END,
how to add time part in SYSDATE here
It's already there; if you don't see it, it is because your NLS settings. Here's an example of what you might do: ALTER SESSION or use TO_CHAR:
SQL> create table test (approved_Date date);
Table created.
SQL> insert into test values (sysdate);
1 row created.
SQL> select * from test;
APPROVED
--------
05.01.18
SQL> select to_char(approved_Date, 'dd.mm.yyyy hh24:mi:ss') from test;
TO_CHAR(APPROVED_DA
-------------------
05.01.2018 10:37:38
SQL> alter session set nls_date_format = 'dd-mm-yyyy hh:mi:ss am';
Session altered.
SQL> select * from test;
APPROVED_DATE
----------------------
05-01-2018 10:37:38 AM
SQL>
you CASE code is running perfectly fine. you just need to change your display setting as sysdate already have time element in it.
if you are using PL/SQL go to
tools > preference > Date/Time
and change to format as per your requirement.
and while accessing this field use to_char(sysdate , 'DD/MM/YYYY hh24:mi:ss')

Is Numeric in Oracle

I'm working in oracle 11g. I've a table with Number as the datatype. For development purpose we have created a staging table with varchar type. Initially data would be loaded in the staging table. We need find out the records that has only number in that column, since the data might contain the noises. Is there any way to find it.
You can select your data with a regexp_like :
SELECT *
FROM your_table t
WHERE REGEXP_LIKE (t.your_colonne, '^[0-9]+$');
The regexp_like function can be used to determine if a value consists of only digits. Here is an example:
with Your_table(your_column) as(
select '123456' from dual union all
select 'a123452' from dual union all
select '01456' from dual union all
select '1j6-d' from dual
)
select your_column
from your_table
where regexp_like(your_column, '^[[:digit:]]+$')
Result:
YOUR_COLUMN
--------------
123456
01456
SQL Fiddle Demo

Resources