任何人都可以告诉我如何获取当月第一个日期到同月当前日期的列表吗?假设我想要获得2015年5月至2015年5月17日的第一个日期。
格式应该是这样的:ddmmyyyy
答案 0 :(得分:2)
要获取从开始到当天的月份日期:
查询1 :
SELECT TRUNC( SYSDATE, 'MM' ) + LEVEL - 1 AS DATE_OF_CURRENT_MONTH
FROM DUAL
CONNECT BY LEVEL <= EXTRACT( DAY FROM SYSDATE )
<强> Results 强>:
| DATE_OF_CURRENT_MONTH |
|-----------------------|
| May, 01 2015 00:00:00 |
| May, 02 2015 00:00:00 |
| May, 03 2015 00:00:00 |
| May, 04 2015 00:00:00 |
| . |
| . |
| . |
| May, 15 2015 00:00:00 |
| May, 16 2015 00:00:00 |
| May, 17 2015 00:00:00 |
查询2 :
WITH CTE( DATE_OF_CURRENT_MONTH, Lvl ) AS
(
SELECT TRUNC( SYSDATE, 'MM' ), 1
FROM DUAL
UNION ALL
SELECT TRUNC( SYSDATE, 'MM' ) + Lvl, Lvl + 1
FROM CTE
WHERE Lvl < EXTRACT( DAY FROM SYSDATE )
)
SELECT DATE_OF_CURRENT_MONTH FROM CTE
<强> Results 强>:
| DATE_OF_CURRENT_MONTH |
|-----------------------|
| May, 01 2015 00:00:00 |
| May, 02 2015 00:00:00 |
| May, 03 2015 00:00:00 |
| May, 04 2015 00:00:00 |
| . |
| . |
| . |
| May, 15 2015 00:00:00 |
| May, 16 2015 00:00:00 |
| May, 17 2015 00:00:00 |
要获得本月的所有日子:
查询3 :
SELECT TRUNC( SYSDATE, 'MM' ) + LEVEL - 1 AS DATE_OF_CURRENT_MONTH
FROM DUAL
CONNECT BY LEVEL <= EXTRACT( DAY FROM LAST_DAY( SYSDATE ) )
<强> Results 强>:
| DATE_OF_CURRENT_MONTH |
|-----------------------|
| May, 01 2015 00:00:00 |
| May, 02 2015 00:00:00 |
| May, 03 2015 00:00:00 |
| May, 04 2015 00:00:00 |
| . |
| . |
| . |
| May, 29 2015 00:00:00 |
| May, 30 2015 00:00:00 |
| May, 31 2015 00:00:00 |
查询4 :
WITH CTE( DATE_OF_CURRENT_MONTH, Lvl ) AS
(
SELECT TRUNC( SYSDATE, 'MM' ), 1
FROM DUAL
UNION ALL
SELECT TRUNC( SYSDATE, 'MM' ) + Lvl, Lvl + 1
FROM CTE
WHERE Lvl < EXTRACT( DAY FROM LAST_DAY( SYSDATE ) )
)
SELECT DATE_OF_CURRENT_MONTH FROM CTE
<强> Results 强>:
| DATE_OF_CURRENT_MONTH |
|-----------------------|
| May, 01 2015 00:00:00 |
| May, 02 2015 00:00:00 |
| May, 03 2015 00:00:00 |
| May, 04 2015 00:00:00 |
| . |
| . |
| . |
| May, 29 2015 00:00:00 |
| May, 30 2015 00:00:00 |
| May, 31 2015 00:00:00 |
答案 1 :(得分:1)
您可以connect by
level
伪列并通过将其结合到当前月份和年份来格式化此结果:
SELECT TO_CHAR(LEVEL, '09') || TO_CHAR(SYSDATE, 'mmyyyy')
FROM dual
CONNECT BY LEVEL < TO_NUMBER(TO_CHAR(SYSDATE, 'dd'))
编辑:
要显示当月的所有日期,您可以使用last_day
功能检查当月的天数:
SELECT TO_CHAR(LEVEL, '09') || TO_CHAR(SYSDATE, 'mmyyyy')
FROM dual
CONNECT BY LEVEL < TO_NUMBER(TO_CHAR(LAST_DAY(SYSDATE), 'dd'))