SQL 如何将日期值插入表中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34329852/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:15:58  来源:igfitidea点击:

How to insert date values into table

sqloracledatetimeoracle10gdate-formatting

提问by AbIr Chanda

How can I insert into table with different input using / ,with date datatype?

如何使用 / 和日期数据类型插入具有不同输入的表?

insert into run(id,name,dob)values(&id,'&name',[what should I write here?]);

I'm using oracle 10g.

我正在使用 oracle 10g。

回答by Lalit Kumar B

Since dobis DATEdata type, you need to convert the literalto DATEusing TO_DATEand the proper format model. The syntax is:

由于dobDATE数据类型,您需要将文字转换为DATEusingTO_DATE和正确的格式模型。语法是:

TO_DATE('<date_literal>', '<format_model>')

For example,

例如,

SQL> CREATE TABLE t(dob DATE);

Table created.

SQL> INSERT INTO t(dob) VALUES(TO_DATE('17/12/2015', 'DD/MM/YYYY'));

1 row created.

SQL> COMMIT;

Commit complete.

SQL> SELECT * FROM t;

DOB
----------
17/12/2015

A DATEdata type contains both date and time elements. If you are not concerned about the time portion, then you could also use the ANSI Date literal which uses a fixed format 'YYYY-MM-DD'and is NLS independent.

DATE数据类型包含日期和时间的元件。如果您不关心时间部分,那么您还可以使用 ANSI 日期文字,它使用固定格式'YYYY-MM-DD'并且与 NLS 无关。

For example,

例如,

SQL> INSERT INTO t(dob) VALUES(DATE '2015-12-17');

1 row created.

回答by Dinith

date must be insert with two apostrophes' As example if the date is 2018/10/20. It can insert from these query

日期必须插入两个撇号' 例如,如果日期是 2018/10/20。它可以从这些查询中插入

Query -

询问 -

insert into run(id,name,dob)values(&id,'&name','2018-10-20')

回答by lsandeep3

insert into run(id,name,dob)values(&id,'&name',[what should I write here?]);

insert into run(id,name,dob)values(&id,'&name',[这里应该写什么?]);

insert into run(id,name,dob)values(&id,'&name',TO_DATE('&dob','YYYY-MM-DD'));

回答by Zia Ullah

let suppose we create a table Transactions using SQl server management studio

假设我们使用 SQl 服务器管理工​​作室创建一个表 Transactions

txn_id int,

txn_type_id varchar(200),

Account_id int,

Amount int,

tDate date

);

with datedatatype we can insert values in simple format: 'yyyy-mm-dd'

使用日期数据类型,我们可以以简单格式插入值:'yyyy-mm-dd'

INSERT INTO transactions (txn_id,txn_type_id,Account_id,Amount,tDate)
VALUES (978, 'DBT', 103, 100, '2004-01-22');

Moreover we can have differet time formats like

此外,我们可以有不同的时间格式,如

DATE - format YYYY-MM-DD
DATETIME - format: YYYY-MM-DD HH:MI:SS
SMALLDATETIME - format: YYYY-MM-DD HH:MI:SS