如何在oracle中以dd / mm / yyyy格式的DATE字段中插入当前日期 [英] how to insert current date into a DATE field in dd/mm/yyyy format in oracle
问题描述
我的表中有一个字段,数据类型为Oracle的DATE。
我想以DD / MM / YYYY格式的格式将当前日期插入该字段。
I have a field in my table with datatype as DATE in Oracle. I want to insert the current date into that field, in format DD/MM/YYYY format.
我尝试了以下查询:
select to_date(to_char(sysdate,'dd/mm/yyyy'),'dd/mm/yyyy') from dual
但它给了
1/8/2011 12:00:00 AM.
我希望它插入并显示为
08/01/2011 12:00:00 AM.
任何人都可以帮我吗?
推荐答案
DATE
是Oracle中的内置类型,以固定的方式表示,您无法控制。
DATE
is a built-in type in Oracle, which is represented in a fixed way and you have no control over it.
所以:
我希望它插入[...] 08/01/2011 12:00:00 AM
I want it to insert [...] as 08/01/2011 12:00:00 AM
以上是荒谬的。你不要插入一个字符串,你插入一个日期。
The above is nonsensical. You don't insert a string, you insert a date.
格式只有在你想要的时候才有用:
Format is useful only when you want:
- 使用
TO_DATE
(格式掩码:如何解析字符串)将字符串转换为日期的内部表示形式; - 将日期的内部表示转换为
TO_CHAR
(格式掩码:如何呈现日期)的字符串。
- to convert a string to an internal representation of date with
TO_DATE
(format mask: how to parse the string); - to convert an internal representation of date to a string with
TO_CHAR
(format mask: how to render the date).
所以基本上,在你的例子中,你采用DATE,你将它转换成一个格式为STRING,并以相同的格式将其转换回DATE。这是一个no-op。
So basically, in your example you take a DATE, you convert it to a STRING with some format, and convert it back to DATE with the same format. This is a no-op.
现在,您的客户端显示:这是因为您的Oracle Client不会直接显示DATE字段,NLS层将转换任何DATE字段被选中。所以这取决于您的区域设置。
Now, what your client displays: this is because your Oracle Client won't display DATE fields directly and the NLS layer will convert any DATE field that is selected. So it depends on your locale by default.
你想要的是 SELECT TO_CHAR(SYSDATE,'DD / MM / YYYY')FROM dual;
将显式执行转换并返回一个字符串。
What you want is SELECT TO_CHAR(SYSDATE,'DD/MM/YYYY') FROM dual;
which will explicitly perform the conversion and return a string.
当您要在数据库中插入日期时,可以使用 TO_DATE
或日期文字。
And when you want to insert a date in a database, you can use TO_DATE
or date literals.
这篇关于如何在oracle中以dd / mm / yyyy格式的DATE字段中插入当前日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!