Home » SQL & PL/SQL » SQL & PL/SQL » Display output like.
Display output like. [message #617043] Tue, 24 June 2014 07:01 Go to next message
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 #617045 is a reply to message #617043] Tue, 24 June 2014 07:06 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
So use to_char with a format mask of YY.
Re: Display output like. [message #617046 is a reply to message #617045] Tue, 24 June 2014 07:11 Go to previous messageGo to next message
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 #617048 is a reply to message #617046] Tue, 24 June 2014 07:27 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
extract does return a number, but if you concatenate a number to a - the final resulting datatype has to be a string type.
Re: Display output like. [message #617053 is a reply to message #617046] Tue, 24 June 2014 07:42 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

SQL> select to_char(ADD_MONTHS (SYSDATE,-3),'YY')||'-'||to_char(ADD_MONTHS (SYSDATE, 9),'YY') res from dual;
RES
-----
14-15

Re: Display output like. [message #617127 is a reply to message #617048] Wed, 25 June 2014 06:33 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
cookiemonster wrote on Tue, 24 June 2014 13:27
extract 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 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
Lalit Kumar B wrote on Wed, 25 June 2014 12:47
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

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 #617132 is a reply to message #617128] Wed, 25 June 2014 07:04 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
cookiemonster wrote on Wed, 25 June 2014 12:38
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.

That is EXACTLY what i was thinking cookie, and was wondering if you had some weird and wonderful combo that I couldn't see Smile
Thanks for the clarification.
Re: Display output like. [message #617165 is a reply to message #617043] Wed, 25 June 2014 11:59 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
vik@s wrote on Tue, 24 June 2014 08:01
But 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 Go to previous messageGo to next message
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 Smile

[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 Go to previous message
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.
Previous Topic: >= date field in Oracle not retrieving expected results (merged 2 by MC)
Next Topic: Data extract from scheduled process improvement
Goto Forum:
  


Current Time: Wed Apr 24 10:48:16 CDT 2024