人。我有查询,它计算每一天的一些事件的数量。 但它以错误的格式返回日期。
select trunc(action_dt) as action_dt, count(*) as cnt
from stat.jurnal_orders
where action_dt between TO_DATE('17.05.12','DD/MM/YY') AND TO_DATE('12.11.13','DD/MM/YY')
group by trunc(action_dt)
order by action_dt asc
在数据库中,我的日期在DD.MM.YYYY,但我想要YYYY / MM / DD 有人可以帮我解决这个问题吗?
答案 0 :(得分:1)
你有两个选择
首先 - 使用TO_CHAR
select to_char(trunc(action_dt),'yyyy/mm/dd') as action_dt, count(*) as cnt
from stat.jurnal_orders
where action_dt between TO_DATE('17.05.12','DD/MM/YY') AND TO_DATE('12.11.13','DD/MM/YY')
group by to_char(trunc(action_dt),'yyyy/mm/dd')
order by action_dt asc
其次,在会话级别设置nls_date_format
。
alter session set nls_date_format = 'yyyy/mm/dd';
并查询
select trunc(action_dt) as action_dt, count(*) as cnt
from stat.jurnal_orders
where action_dt between TO_DATE('2012/05/17') AND TO_DATE('2013/12/11')
group by trunc(action_dt)
order by action_dt asc
答案 1 :(得分:1)
要将日期转换为其他格式,您可以使用Oracle TO_CHAR函数。 http://www.techonthenet.com/oracle/functions/to_char.php
对于您的示例,假设您想首先TRUNC日期,您可以像这样使用它:
SELECT TO_CHAR(TRUNC(action_dt), 'YYYY/MM/DD') as action_dt
FROM some_table
答案 2 :(得分:0)
请尝试TO_CHAR
:
select TO_CHAR (sysdate, 'YYYY/MM/DD') from dual;
答案 3 :(得分:-1)
SELECT REPLACE(CONVERT(VARCHAR(10), action_dt, 102), '.', '/')
http://msdn.microsoft.com/en-us/library/ms187928.aspx
编辑:这是针对T-SQL而不是Oracle。误读了这个问题。