Tag Archives: calendar query

[Oracle] Creating calendar

with x
as (
select *
from (
select to_char(trunc(sysdate,’mm’)+level-1,’w') wk,
to_char(trunc(sysdate,’mm’)+level-1,’dd’) dm,
to_number(to_char(trunc(sysdate,’mm’)+level-1,’d')) dw,
to_char(trunc(sysdate,’mm’)+level-1,’mm’) curr_mth,
to_char(sysdate,’mm’) mth
from dual
connect by level <= 31
)
where curr_mth = mth
)
select
max(case dw when 1 then dm end) Su,
max(case dw when 2 then dm end) Mo,
max(case dw when 3 then dm end) Tu,
max(case dw when 4 then dm end) We,
max(case dw when 5 then dm end) Th,
max(case dw when 6 then dm end) Fr,
max(case dw when 7 then dm end) Sa
from x
group by wk
order by wk;

SQL>

SU MO TU WE TH FR SA
-- -- -- -- -- -- --
01 02 03 04 05 06 07
08 09 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31

to_char(datetime, format)
Oracle date format

With the functions to_char and to_date, a date format can be used. Example:
select to_char(sysdate,’DD/MM/YYYY HH24:MI:SS’) from dual;
will return something like: 24/03/2006 14:36:43

Here is a list of all the formats that can be used:

Format mask	Description
-----------    -------------------------------
CC	 Century
SCC	 Century BC prefixed with -
YYYY	 Year with 4 numbers
SYYY	 Year BC prefixed with -
IYYY	 ISO Year with 4 numbers
YY	 Year with 2 numbers
RR	 Year with 2 numbers with Y2k compatibility
YEAR	Year in characters
SYEAR	Year in characters, BC prefixed with -
BC	 BC/AD Indicator *
Q	 Quarter in numbers (1,2,3,4)
MM	 Month of year 01, 02...12
MONTH	Month in characters (i.e. January)
MON	 JAN, FEB
WW	 Weeknumber (i.e. 1)
W	 Weeknumber of the month (i.e. 5)
IW	 Weeknumber of the year in ISO standard.
DDD	 Day of year in numbers (i.e. 365)
DD	 Day of the month in numbers (i.e. 28)
D	 Day of week in numbers(i.e. 7)
DAY	 Day of the week in characters (i.e. Monday)
FMDAY	 Day of the week in characters (i.e. Monday)
DY	 Day of the week in short character description (i.e. SUN)
J	 Julian Day (number of days since January 1 4713 BC, where January 1 4713 BC is 1 in Oracle)
HH	 Hournumber of the day (1-12)
HH12	 Hournumber of the day (1-12)
HH24	 Hournumber of the day with 24Hours notation (0-23)
AM	 AM or PM
PM	 AM or PM
MI	 Number of minutes (i.e. 59)
SS	 Number of seconds (i.e. 59)
SSSSS	 Number of seconds this day.
DS	 Short date format. Depends on NLS-settings. Use only with timestamp.
DL	 Long date format. Depends on NLS-settings. Use only with timestamp.
E	 Abbreviated era name. Valid only for calendars: Japanese Imperial, ROC Official and Thai Buddha.. (Input-only)
EE	 The full era name
FF	 The fractional seconds. Use with timestamp.
FF1..FF9	 The fractional seconds. Use with timestamp. The digit controls the number of decimal digits used for fractional seconds.
FM	 Fill Mode: suppresses blianks in output from conversion
FX	 Format Exact: requires exact pattern matching between data and format model.
IYY or IY or I	 the last 3,2,1 digits of the ISO standard year. Output only
RM	 The Roman numeral representation of the month (I .. XII)
RR	 The last 2 digits of the year.
RRRR	 The last 2 digits of the year when used for output. Accepts fout-digit years when used for input.
SCC	 Century. BC dates are prefixed with a minus.
CC	 Century
SP	 Spelled format. Can appear of the end of a number element. The result is always in english. For example month 10 in format MMSP returns "ten"
SPTH	 Spelled and ordinal format; 1 results in first.
TH	 Converts a number to it's ordinal format. For example 1 becoms 1st.
TS	 Short time format. Depends on NLS-settings. Use only with timestamp.
TZD	 Abbreviated time zone name. ie PST.
TZH	 Time zone hour displacement.
TZM	 Time zone minute displacement.
TZR	 Time zone region
X	 Local radix character. In america this is a period (.)

[MySQL] Creating a calendar

참조: SQL Cookbook

select
max(case dw when 1 then dm end) as Su,
max(case dw when 2 then dm end) as Mo,
max(case dw when 3 then dm end) as Tu,
max(case dw when 4 then dm end) as We,
max(case dw when 5 then dm end) as Th,
max(case dw when 6 then dm end) as Fr,
max(case dw when 7 then dm end) as Sa
from (
select date_format(dy,’%U’) wk,
date_format(dy,’%d’) dm,
date_format(dy,’%w’)+1 dw
from (
select adddate(x.dy,T500.id-1) dy,
x.mth
from (
select adddate(current_date,-dayofmonth(current_date)+1) dy,
date_format(
adddate(current_date,
-dayofmonth(current_date)+1),
‘%m’) mth
from T1
) x,
T500
where T500.id <= 31
and date_format(adddate(x.dy,T500.id-1),’%m’) = x.mth
) y
) z
group by wk
order by wk;

mysql>
+------+------+------+------+------+------+------+
| Su   | Mo   | Tu   | We   | Th   | Fr   | Sa   |
+------+------+------+------+------+------+------+
| 01   | 02   | 03   | 04   | 05   | 06   | 07   |
| 08   | 09   | 10   | 11   | 12   | 13   | 14   |
| 15   | 16   | 17   | 18   | 19   | 20   | 21   |
| 22   | 23   | 24   | 25   | 26   | 27   | 28   |
| 29   | 30   | 31   | NULL | NULL | NULL | NULL |
+------+------+------+------+------+------+------+
mysql> select * from T1;
+------+
| ID   |
+------+
|    1 |
+------+


mysql> select * from T500;
+------+
| ID   |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
|    5 |
.....
.....
|  495 |
|  496 |
|  497 |
|  498 |
|  499 |
|  500 |
+------+
500 rows in set (0.01 sec)

MySQL DATE_FORMAT() Function

Syntax:

DATE_FORMAT(date,format)

The formats that can be used are:

Format	Description
------ -----------------------------
%a	Abbreviated weekday name
%b	Abbreviated month name
%c	Month, numeric
%D	Day of month with English suffix
%d	Day of month, numeric (00-31)
%e	Day of month, numeric (0-31)
%f	Microseconds
%H	Hour (00-23)
%h	Hour (01-12)
%I	Hour (01-12)
%i	Minutes, numeric (00-59)
%j	Day of year (001-366)
%k	Hour (0-23)
%l	Hour (1-12)
%M	Month name
%m	Month, numeric (00-12)
%p	AM or PM
%r	Time, 12-hour (hh:mm:ss AM or PM)
%S	Seconds (00-59)
%s	Seconds (00-59)
%T	Time, 24-hour (hh:mm:ss)
%U	Week (00-53) where Sunday is the first day of week
%u	Week (00-53) where Monday is the first day of week
%V	Week (01-53) where Sunday is the first day of week, used with %X
%v	Week (01-53) where Monday is the first day of week, used with %x
%W	Weekday name
%w	Day of the week (0=Sunday, 6=Saturday)
%X	Year of the week where Sunday is the first day of week, four digits, used with %V
%x	Year of the week where Monday is the first day of week, four digits, used with %v
%Y	Year, four digits
%y	Year, two digits