1

The ORACLE SQL manual (Page 2-58 of the main 11.1 SQL Manual) states:

The total length of a datetime format model cannot exceed 22 characters.

However, I've been able to use a format string for TO_CHAR with many more characters, almost all of which are text "literals".

Is this safe (ORACLE 11)? (Perhaps the limit does not include non-date literals?)

For example:

SELECT TO_CHAR(sysdate,
       '"SOMESTRING_"YYYYMMDD"_SOME LOTS MORE STRINGS OF A FILENAME PREFIX"') 
FROM DUAL;

Result:

SOMESTRING_20200226_SOME LOTS MORE STRINGS OF A FILENAME PREFIX

It also works with dates from tables.

The reason I want to do this is to build a filename template in a database column; otherwise I will need to put the date format in a separate field.

P.S. Please tell me if this should be asked in a different community. I've never asked a question before, so I have no reputation to lose :-). Be nice.

6
  • Link to the online manual would be nice. Commented Feb 27, 2020 at 0:44
  • It also works with dates from tables. You put in gibberish and you get out gibberish (aka GIGO). As far as I'm concerned what you've done should throw an error - but hey, that's Oracle's problem! otherwise I will need to put the date format in a separate field. IF it's a date, then it belongs in a date column - otherwise you'll spend a lot of time parsing strings - which is NOT what SQL was designed for! p.s. welcome to the forum! :-)
    – Vérace
    Commented Feb 27, 2020 at 3:43
  • @/v%c3%a9race I'm not sure what you meant by gibberish. Yes, I realized the alternative is to use a format in a separate column and use replace; I was trying to keep the question short. In fact, the actual template is more like %PCNAMEYYMMDD_RESTOFNAME, with REPLACE used to repalce %P with another database field. Also, I didn't want to be too specific for proprietary reasons. And no, I'm not storing any new dates, just the template of the filename. (Thank you for the welcome!)
    – mzk1
    Commented Feb 27, 2020 at 14:43
  • So - the answer is, don't take a chance. Understood.
    – mzk1
    Commented Feb 27, 2020 at 14:43
  • @Verace Sorry, I was trying to get the accent in your name, and now it won't let me edit. Also, sorry everyone for all of the comments.
    – mzk1
    Commented Feb 27, 2020 at 14:54

2 Answers 2

2

Just because you can jump off a cliff, doesn't mean you should.

Use REPLACE() with a token:

select REPLACE('SOMESTRING_##DATE##_SOME LOTS MORE STRINGS OF A FILENAME PREFIX',
               '##DATE##',
               TO_CHAR(sysdate,'YYYYMMDD'))
from DUAL; 

You could take this a step further to allow different format strings:

with mytable as (
select 'SOMESTRING_##YYYYMMDD##_SOME LOTS MORE STRINGS OF A FILENAME PREFIX' as format_string
from DUAL
)
select REGEXP_REPLACE(format_string,'##(.*)##',TO_CHAR(sysdate,replace(
                    regexp_substr( format_string, '##(.*)##', 1, 1),'##','')))
  from mytable;

Note that there have been parsing bugs with long TO_CHAR identifiers that have caused ORA-600 errors & session crashes.

3
  • 1
    @Boneist read the question. "The reason I want to do this is to build a filename template in a database column; otherwise I will need to put the date format in a separate field." -- the OP wants to store a format string in a single column
    – Philᵀᴹ
    Commented Feb 27, 2020 at 8:56
  • Ah, I missed that bit! D'oh!
    – Boneist
    Commented Feb 27, 2020 at 9:04
  • @Phil Thank you. So, I'll use one column for the template and one for the date format, and use REPLACE. That was the alternative I intended.
    – mzk1
    Commented Feb 27, 2020 at 14:51
-1

I would use this one instead:

SELECT 'SOMESTRING_' || TO_CHAR(sysdate,'YYYYMMDD') || '_SOME LOTS MORE STRINGS OF A FILENAME PREFIX'
FROM DUAL;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.