oracle 如何在oracle中以dd/mm/yyyy格式将当前日期插入到DATE字段中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4633271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to insert current date into a DATE field in dd/mm/yyyy format in oracle
提问by cherit
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.
我的表中有一个字段,其数据类型为 Oracle 中的 DATE。我想将当前日期插入到该字段中,格式为 DD/MM/YYYY。
I tried the below query:
我尝试了以下查询:
select to_date(to_char(sysdate,'dd/mm/yyyy'),'dd/mm/yyyy') from dual
But it gives
但它给
1/8/2011 12:00:00 AM.
I want it to insert and show as
我希望它插入并显示为
08/01/2011 12:00:00 AM.
Can anyone help me in this please ?
任何人都可以帮助我吗?
回答by Benoit
DATE
is a built-in type in Oracle, which is represented in a fixed way and you have no control over it.
DATE
是 Oracle 中的内置类型,它以固定方式表示,您无法控制它。
So:
所以:
I want it to insert [...] as 08/01/2011 12:00:00 AM
我希望它插入 [...] 作为 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 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).
- 使用
TO_DATE
(格式掩码:如何解析字符串)将字符串转换为日期的内部表示; - 将日期的内部表示转换为带有
TO_CHAR
(格式掩码:如何呈现日期)的字符串。
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.
所以基本上,在您的示例中,您使用 DATE,将其转换为某种格式的 STRING,然后将其转换回具有相同格式的 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.
现在,您的客户端显示的内容:这是因为您的 Oracle 客户端不会直接显示 DATE 字段,并且 NLS 层将转换任何选定的 DATE 字段。因此,默认情况下这取决于您的语言环境。
What you want is SELECT TO_CHAR(SYSDATE,'DD/MM/YYYY') FROM dual;
which will explicitly perform the conversion and return a string.
您想要的是SELECT TO_CHAR(SYSDATE,'DD/MM/YYYY') FROM dual;
显式执行转换并返回字符串。
And when you want to insert a date in a database, you can use TO_DATE
or date literals.
当您想在数据库中插入日期时,您可以使用TO_DATE
或 日期文字。
回答by Raymond Tau
Alternatively, if you want to retrieve the date part of the DATE field, you may use truncate, i.e.
或者,如果要检索 DATE 字段的日期部分,可以使用 truncate,即
select to_char(trunc(sysdate),'dd/mm/yyyy') from dual;
回答by bernd_k
When the column is of type DATE, you can use something like:
当列的类型为 DATE 时,您可以使用以下内容:
Insert into your_table(your_date_column) Select TRUNC(SYSDATE) from DUAL;
This removes the time part from SYSDATE.
这将从 SYSDATE 中删除时间部分。
回答by user3086349
Maybe this can help
也许这可以帮助
insert into pasok values ('&kode_pasok','&kode_barang','&kode_suplier',
to_date('&tanggal_pasok','dd-mm-yyyy'),&jumlah_pasok);
note: '&' help we to insert data again, insert / end than enter to insert again example: Enter value for kode_pembelian: BEL-E005 Enter value for kode_barang: ELK-02 Enter value for kode_customer: B-0001 old 2: '&kode_pembelian','&kode_barang','&kode_customer', new 2: 'BEL-E005','ELK-02','B-0001', Enter value for tanggal_pembelian: 24-06-2002 Enter value for jumlah_pembelian: 2 old 3: to_date('&tanggal_pembelian','dd-mm-yyyy'),&jumlah_pembelian) new 3: to_date('24-06-2002','dd-mm-yyyy'),2)
1 row created.
SQL> / (enter)
注意:'&' 帮助我们再次插入数据,插入/结束而不是回车再次插入示例:输入 kode_pembelian 的值:BEL-E005 输入 kode_barang 的值:ELK-02 输入 kode_customer 的值:B-0001 old 2:' &kode_pembelian','&kode_barang','&kode_customer', new 2: 'BEL-E005','ELK-02','B-0001', 输入 tanggal_pembelian 的值:24-06-2002 输入 jumlah_pembelian 的值: : to_date('&tanggal_pembelian','dd-mm-yyyy'),&jumlah_pembelian) 新 3: to_date('24-06-2002','dd-mm-yyyy'),2)
已创建 1 行。
SQL> /(输入)