Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Volkenand.

Asked: February 07, 2018 - 3:16 pm UTC

Last updated: February 08, 2018 - 2:37 am UTC

Version: 11.2.0.3

Viewed 1000+ times

You Asked

I have problem to determine the quarter dynamically for the spool path
in the form W:\SG-323\TeamZentraleKatalogredaktion\Regelupdate\Q4_2017\Sonderlocken\dst_polizei-GDP.csv

SQL Developer Execution:
@test
set echo on
column filename new_val filename

--print Quarternr. with fixed date, but I need it based on sysdate

--SELECT TO_CHAR(TO_DATE('12/26/2012', 'MM/DD/YYYY'), 'Q') AS MY_QTR FROM DUAL;

With the command below I get 3 instead 1, How can I get the Quarternr after the Q letter ?

I get error 01830. 00000 - "date format picture ends before converting entire input string"
--SELECT TO_CHAR(TO_DATE(sysdate, 'MM/DD/YYYY'), 'Q') AS MY_QTR FROM DUAL;

--the year I get with
select 'W:\SG-323\TeamZentraleKatalogredaktion\Regelupdate\Q_' || to_char(sysdate, 'yyyy')|| '\Sonderlocken\dst_polizei-GDP.csv' filename from dual;

--Output without Quarternummer:
--FILENAME
--------------------------------------------------------------------------------------------
--W:\SG-323\TeamZentraleKatalogredaktion\Regelupdate\Q_2018\Sonderlocken\dst_polizei-GDP.csv


spool &filename

and Connor said...

SYSDATE is *already* a date, so you do not need to put TO_DATE around it

SQL> SELECT TO_CHAR(TO_DATE(sysdate, 'MM/DD/YYYY'), 'Q') AS MY_QTR FROM DUAL;
SELECT TO_CHAR(TO_DATE(sysdate, 'MM/DD/YYYY'), 'Q') AS MY_QTR FROM DUAL
                       *
ERROR at line 1:
ORA-01858: a non-numeric character was found where a numeric was expected


SQL> select to_char(sysdate,'Q') AS MY_QTR FROM DUAL;

M
-
1

1 row selected.


You get the error because we (silently) go:

"Oh, you want to do TO_DATE on something....That something MUST be a string, so I'll convert SYSDATE to a STRING using the default format mask, and THEN I'll try convert it to a DATE , and THEN convert it using TO_CHAR with DD/MM/YYYY"


Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

uarter dynamically for the spool path

A reader, February 08, 2018 - 8:57 am UTC

Thanks for the help

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library