I'm trying to write an SQL statement with the output as:
Day XXX of 2010
I can get the day number and the year with the following:
SELECT TO_CHAR(SYSDATE, 'DDD YYYY') as CurrDate
2 FROM DUAL;
But, how do I add the text, 'Day' and 'of'' to the output?|||You should be able to splice the pieces together with string concatenation. Assuming this is Oracle, you use the really odd choice of || as the string concat operator.
Since you want text between the day and year part, you'll need to break up the call to sysdate.
This should be close (don't have Oracle handy to test it):
SELECT 'DAY' || TO_CHAR(SYSDATE, 'DDD') || ' of ' || TO_CHAR(SYSDATE, 'YYYY')
as CurrDate
FROM DUAL;|||Just use the concatenation operator to combine the output from the function with your literals:
SELECT "Day " + TO_CHAR(SYSDATE, 'DDD') + " of " + TO_CHAR(SYSDATE, 'YYYY') AS CurrDate
FROM DUAL;|||SELECT DATE_FORMAT(SYSDATE(), 'Day %j of %Y');
That's for MySQL. Other databases may have similar functions. Tell us what db you are using if you can't find it.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment