描述
执行如下的SQL
select TO_DATE(\'2018-1-9 12:41:00\',\'yyyy-MM-dd hh24:mm:ss\') from dual;
然后就报了如下的错误
ORA-01810: 格式代码出现两次
01810. 00000 - "format code appears twice"
*Cause:
*Action:
select RECEIVEDATE,RECEIVETIME,TO_DATE(concat(RECEIVEDATE,concat(\' \',RECEIVETIME)),\'yyyy-MM-dd hh24:mm:ss\') from workflow_currentoperator where requestid = 42 and nodeid = 162 ;
原因及解决方法
TO_DATE函数的语法如下:
TO_DATE(char,fmt);
其中char为字符串,fmt为日期格式,如yyyy-mm-dd hh24:mi:ss
。
对于SQL的语法来说是不区分大小写的,所以日期格式的MM和mm是一样的,在oracle的日期转换中对于月份应该使用MM
,对于分钟应该使用MI
。sql应该修改为如下:
select TO_DATE(\'2018-1-9 12:41:00\',\'yyyy-MM-dd hh24:mi:ss\') from dual;
oracle的时间日期格式如下表:
元素 | 描述 |
---|---|
DD | 月的天数(1-31). |
YYYY | 年份 |
MM | 月份(01-12; 一月 = 01). |
HH | 时间(12小时制) |
HH12 | 时间(12小时制) |
HH24 | 时间(24小时制) |
MI | 分钟 |
SS | 秒 |
这个和java的时间日期格式化是不一样的,java的时间日期格式化如下表:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.format(date);
Letter | Date or Time Component | Presentation | Examples |
---|---|---|---|
G
| Era designator | Text | AD
|
y
| Year | Year | 1996 ; 96
|
Y
| Week year | Year | 2009 ; 09
|
M
| Month in year (context sensitive) | Month | July ; Jul ; 07
|
L
| Month in year (standalone form) | Month | July ; Jul ; 07
|
w
| Week in year | Number | 27
|
W
| Week in month | Number | 2
|
D
| Day in year | Number | 189
|
d
| Day in month | Number | 10
|
F
| Day of week in month | Number | 2
|
E
| Day name in week | Text | Tuesday ; Tue
|
u
| Day number of week (1 = Monday, ..., 7 = Sunday) | Number | 1
|
a
| Am/pm marker | Text | PM
|
H
| Hour in day (0-23) | Number | 0
|
k
| Hour in day (1-24) | Number | 24
|
K
| Hour in am/pm (0-11) | Number | 0
|
h
| Hour in am/pm (1-12) | Number | 12
|
m
| Minute in hour | Number | 30
|
s
| Second in minute | Number | 55
|
S
| Millisecond | Number | 978
|
z
| Time zone | General time zone | Pacific Standard Time ; PST ; GMT-08:00
|
Z
| Time zone | RFC 822 time zone | -0800
|
X
| Time zone | ISO 8601 time zone | -08 ; -0800 ; -08:00
|
参考
1.https://docs.oracle.com/cd/E11882_01/server.112/e41084/functions203.htm#SQLRF06132
2.https://docs.oracle.com/cd/E11882_01/server.112/e41084/sql_elements004.htm#SQLRF00212
3.java.text.SimpleDateFormat