Postgresql:我们如何使用时间戳将时间和日期仅插入到变量中?

标签 postgresql

错误:插入时出现接近时间格式错误。

示例:

--Table "Test"  

create table test  
(  
name varchar(10),  
date1 timestamp,  
time1 timestamp  
);  

-- Insertion  
insert into test values('xyz','03/03/2014','02:03:04');  

最佳答案

“timestamp”类型包含时间和日期。您正在尝试分别插入日期和时间。使用日期和时间类型:

--Table "Test"

create table test
(
name varchar(10),
date1 date,
time1 time
);

-- Insertion
insert into test values('xyz','03/03/2014','02:03:04');

或者,只使用一个字段:

--Table "Test"

create table test
(
name varchar(10),
datetime timestamp

);

-- Insertion
insert into test values('xyz','03/03/2014 02:03:04');

我推荐第二种方法,因为可用的运算符和函数集要大得多,而且比较时间戳更容易。

关于Postgresql:我们如何使用时间戳将时间和日期仅插入到变量中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22140542/

相关文章:

c - 如何临时将 printf 输出重定向到 C 字符串?

ruby-on-rails - 如何在使用 has_many :through association 时创建关联模型的实例

r - PostgreSQL dbConnect 到 ec2 实例中 Shiny 的应用程序

python - 使用 SQLAlchemy session 处理 Flask 和并发问题

sql - 在一个范围内分组和排序的多个不同值

java - 连接到 Postgres 数据库失败

database-design - 计算两列之间的百分比变化

sql - 使用选定的列创建另一个字符串插值组合 Postgresql 中的所有字段?

python - SQLAlchemy(Postgres) 和事务

sql - 在 Postgres 中将记录分组到三个预定义的桶中