Several oracle tips

Posted on 2007-09-12 19:50 17th 阅读(98) 评论(0)  编辑  收藏

This query will return a string in a vertical format.

SELECT SUBSTR('&&String', ROWNUM, 1)
FROM all_objects
WHERE ROWNUM <= LENGTH(TRIM('&STRING'));

Prints a Calendar for the Current Year

This tip comes from Robert Ware, a DBA at TekSystems, in St. Louis, Missouri.
This tip prints a calendar for the current year. It can be modified to return other years as well.
select lpad( Month, 20-(20-length(month))/2 ) month,
"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"
from (select to_char(dt,'fmMonthfm YYYY') month,
CASE when to_char(dt,'fmMonthfm YYYY') like 'Dec%' AND to_char(dt+1,'iw') = '01' then '53'
when to_char(dt,'fmMonthfm YYYY') like 'Jan%' AND to_char(dt+1,'iw') = '53' then '.5'
else to_char(dt+1,'iw')
END week,
max(decode(to_char(dt,'d'),'1',lpad(to_char(dt,'fmdd'),2))) "Su",
max(decode(to_char(dt,'d'),'2',lpad(to_char(dt,'fmdd'),2))) "Mo",
max(decode(to_char(dt,'d'),'3',lpad(to_char(dt,'fmdd'),2))) "Tu",
max(decode(to_char(dt,'d'),'4',lpad(to_char(dt,'fmdd'),2))) "We",
max(decode(to_char(dt,'d'),'5',lpad(to_char(dt,'fmdd'),2))) "Th",
max(decode(to_char(dt,'d'),'6',lpad(to_char(dt,'fmdd'),2))) "Fr",
max(decode(to_char(dt,'d'),'7',lpad(to_char(dt,'fmdd'),2))) "Sa"
from (select trunc(sysdate,'y')-1+rownum dt
from all_objects
where rownum <= add_months(trunc(sysdate,'y'),12) - trunc(sysdate,'y'))
group by to_char(dt,'fmMonthfm YYYY'), to_char( dt+1, 'iw' )
order by to_char(dt+1,'iw'))
order by to_date( month, 'Month YYYY' ), to_number(week)
/

只有注册用户登录后才能发表评论。


网站导航: