Display output like. [message #617043] |
Tue, 24 June 2014 07:01 |
|
vik@s
Messages: 10 Registered: January 2014 Location: India
|
Junior Member |
|
|
SELECT EXTRACT (YEAR FROM ADD_MONTHS (SYSDATE,-3))
|| '-'
|| EXTRACT (YEAR FROM ADD_MONTHS (SYSDATE, 9))
FROM DUAL;
output display :-
2014-2015
But i want output like:-
14-15
|
|
|
|
Re: Display output like. [message #617046 is a reply to message #617045] |
Tue, 24 June 2014 07:11 |
pablolee
Messages: 2882 Registered: May 2007 Location: Scotland
|
Senior Member |
|
|
Not sure that'll work (unless i am misunderstanding, which is a very possible scenario!) as extract returns a number (in this case). I think substr would be required...
|
|
|
|
|
Re: Display output like. [message #617127 is a reply to message #617048] |
Wed, 25 June 2014 06:33 |
pablolee
Messages: 2882 Registered: May 2007 Location: Scotland
|
Senior Member |
|
|
cookiemonster wrote on Tue, 24 June 2014 13:27extract does return a number, but if you concatenate a number to a - the final resulting datatype has to be a string type.
Yes, but if you to_char a number with a 'yy' format mask, you'll get an error: (-1481).
As I say, I'm probably missing something in what you are saying:
|
|
|
Re: Display output like. [message #617128 is a reply to message #617127] |
Wed, 25 June 2014 06:38 |
cookiemonster
Messages: 13920 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
We're probably talking at cross purposes. Michel demonstrated exactly what I was talking about above. Which is ditch extract and just use to_char. I assume you were thinking I meant to use to_char on the result of extract, rather than instead of it.
|
|
|
Re: Display output like. [message #617129 is a reply to message #617127] |
Wed, 25 June 2014 06:47 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
pablolee wrote on Wed, 25 June 2014 17:03
Yes, but if you to_char a number with a 'yy' format mask, you'll get an error: (-1481).
Perhaps you mean to_char on what extract returns, and it certainly throws invalid number error.
SQL> SELECT to_char(EXTRACT (YEAR FROM ADD_MONTHS (SYSDATE,-3)),'yy') dt FROM DUAL;
SELECT to_char(EXTRACT (YEAR FROM ADD_MONTHS (SYSDATE,-3)),'yy') dt FROM DUAL
ORA-01481: invalid number format model
The workaround would be to convert it into date using to_date. But that would be just too much of unnecessary work. Michel's query does the job perfectly.
But I don't understand what you mean about the error with 'yy' format mask. I think it would throw same error for 'yyyy' too.
[Updated on: Wed, 25 June 2014 06:49] Report message to a moderator
|
|
|
Re: Display output like. [message #617130 is a reply to message #617129] |
Wed, 25 June 2014 07:02 |
pablolee
Messages: 2882 Registered: May 2007 Location: Scotland
|
Senior Member |
|
|
Lalit Kumar B wrote on Wed, 25 June 2014 12:47pablolee wrote on Wed, 25 June 2014 17:03
Yes, but if you to_char a number with a 'yy' format mask, you'll get an error: (-1481).
Perhaps you mean to_char on what extract returns, and it certainly throws invalid number error.
SQL> SELECT to_char(EXTRACT (YEAR FROM ADD_MONTHS (SYSDATE,-3)),'yy') dt FROM DUAL;
SELECT to_char(EXTRACT (YEAR FROM ADD_MONTHS (SYSDATE,-3)),'yy') dt FROM DUAL
ORA-01481: invalid number format model
Yes, that is the logical inference of what i said. (note that i posted what the error would be).
Quote:The workaround would be to convert it into date using to_date. But that would be just too much of unnecessary work. One of the workarounds would be to use to_date, yes, but as you say, a waste of code.
Quote:Michel's query does the job perfectly. I don't dispute that.
Quote:But I don't understand what you mean about the error with 'yy' format mask. I think it would throw same error for 'yyyy' too.
Quote:So use to_char with a format mask of YY.
|
|
|
|
Re: Display output like. [message #617165 is a reply to message #617043] |
Wed, 25 June 2014 11:59 |
Solomon Yakobson
Messages: 3273 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
vik@s wrote on Tue, 24 June 2014 08:01But i want output like:-
14-15
SELECT MOD(EXTRACT (YEAR FROM ADD_MONTHS (SYSDATE,-3)),100)
|| '-'
|| MOD(EXTRACT (YEAR FROM ADD_MONTHS (SYSDATE, 9)),100) str
FROM DUAL
/
STR
------
14-15
SQL>
SY.
|
|
|
Re: Display output like. [message #617168 is a reply to message #617165] |
Wed, 25 June 2014 12:45 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
Mod over extract year is more of a workaround than what Michel demonstrated which looks simple and nice. How is it different than to_date over it? I think both are an extra work for Oracle and the developer. Unless I am unaware of something you suggested.
@SY, as always you post an alternative method, and it is good to know
[Updated on: Wed, 25 June 2014 21:35] Report message to a moderator
|
|
|
Re: Display output like. [message #617500 is a reply to message #617168] |
Mon, 30 June 2014 15:28 |
Bill B
Messages: 1971 Registered: December 2004
|
Senior Member |
|
|
Nice Solomon,
He is just showing that they are many many different ways to do any particular task in oracle. It was exactly what I was going to write.
|
|
|