I want to make Oracle return dates using .NET’s Universal Sortable Time format so that I can consistently use this rather nice date/time format and so I can easily parse the result to a DateTime simply using “u” as the format:
// "u" is less unwieldy than "yyyy'-'MM'-'dd HH':'mm':'ss'Z'"
myDT = DateTime.ParseExact(oraString, "u", CultureInfo.InvariantCulture);
I can easily make Oracle return this format:
SELECT to_char(sysdate, 'YYYY-MM-DD HH:MM:SS') || 'Z' udt FROM dual
However, the ‘Z’ added with concatenation would make the query unable to practically use a bind variable for the format:
someBindMethod("uformat", "YYYY-MM-DD HH:MM:SS"); // Hypothetical .NET/Oracle lib
-- No matter the format string, date/time will always end with 'Z'.
SELECT to_char(sysdate, :uformat) || 'Z' udt FROM dual
(Oracle complains if the ‘Z’ ends the format string)
How do I pass a bind string such that I can return the universal sortable time format, but can still use any other date format without altering the query?
(Q&A format self-answer): You can add arbitrary strings using double-quotes within the bind variable or within the single-quoted Oracle SQL text, e.g.:
I wasn’t aware of this feature, and it was a little tricky to find at least in the context of my searches.
Now you can bind any format but still get the ‘Z’ ending on the universal format: