oracle时间字符串格式转换,练习题25道(答案)

             

一、建表语句

create table test_payment(
    year varchar(10),
    month varchar(10),
    day varchar(10),
    hour varchar(10),
    product_code varchar(30),
    money varchar(20)
);
CREATE TABLE test_payment_two  ( 
    paytime         varchar(20) NULL,
    product_code    varchar(30) NULL,
    money           varchar(20) NULL 
    );
CREATE TABLE test_payment_three  ( 
    paytime         varchar(20) NULL,
    product_code    varchar(30) NULL,
    money           varchar(20) NULL 
    );
create table test_payment_four(
    paytime date,
    product_code varchar(30),
    money varchar(20)
);
create table test_payment_five(
    paytime date,
    product_code varchar(30),
    money varchar(20)
);
insert  into test_payment values('2019','1','3','12','M2202','89');
insert  into test_payment values('2019','2','4','13','M2201','90');
insert  into test_payment values('2019','3','5','14','M2201','91');
insert  into test_payment values('2019','4','3','12','M2202','89');
insert  into test_payment values('2019','5','4','13','M2201','90');
insert  into test_payment values('2019','5','4','16','M2201','99');
insert  into test_payment values('2019','6','5','14','M2201','91');
insert  into test_payment values('2019','7','5','14','M2201','91');
insert  into test_payment values('2019','8','3','12','M2202','89');
insert  into test_payment values('2019','8','24','12','M2201','89');
insert  into test_payment values('2019','9','4','13','M2201','90');

insert into test_payment_two
select (year||'-'||month||'-'||day||' '||hour)paytime,product_code,money 
from test_payment;

insert into test_payment_three
SELECT  to_char(TO_DATE(year||'-'||month||'-'||day, 'yyyy-mm-dd'),'yyyymmdd')paytime,
product_code,money from test_payment;

insert into test_payment_four
SELECT 
TO_DATE(year||'-'||month||'-'||day,'yyyy-mm-dd') paytime,
product_code,money from test_payment;

insert into test_payment_five
SELECT 
TO_DATE(year||'-'||month||'-'||day||'-'||hour,'yyyy-mm-dd hh24')paytime,
product_code,money from test_payment;

二、答案

#1、将test_payment中year、month day hour四个字段拼接在一起,显示为“YYYY-MM-DD HH:MM:SS”的时间格式。
select to_date(year||'-'||month||'-'||day||'-'||hour,'yyyy-mm-dd hh24') from test_payment;

#2、将test_payment中year、month、day个字段拼接在一起,显示为“YYYYMMDD”的字符串格式。
select to_char(to_date(year||'-'||month||'-'||day,'yyyy-mm-dd'),'yyyymmdd') from test_payment;

#3、查询test_payment中2019-5-1以后的数据
select * from test_payment
where to_date(year||'-'||month||'-'||day,'yyyy-mm-dd')>to_date('2019-5-1','yyyy-mm-dd');

#4、查询test_payment中2019-5-1至2019-8-1之间的数据
select * from test_payment
where to_date(year||'-'||month||'-'||day,'yyyy-mm-dd') between to_date('2019-5-1','yyyy-mm-dd') and to_date('2019-8-1','yyyy-mm-dd');

#5、将表test_payment_two的paytime字段显示为“YYYY-MM-DD HH:MM:SS”的时间格式。
select to_date(paytime,'yyyy-mm-dd  hh24:mi:ss')paytime,product_code,money from test_payment_two;

#6、将表test_payment_two的paytime字段显示为“YYYY-MM-DD HH:MM:SS”的字符串格式。
select to_char(to_date(paytime,'yyyy-mm-dd  hh24:mi:ss'),'yyyy-mm-dd  hh24:mi:ss')paytime,product_code,money from test_payment_two;

#7、将表test_payment_two的paytime字段显示为“YYYY-MM-DD”的的时间格式。
select trunc(to_date(paytime,'YYYY-MM-DD hh24:mi:ss'))paytime,product_code,money from test_payment_two;

select to_char(to_date(paytime,'YYYY-MM-DD hh24:mi:ss'),'YYYY-MM-DD')paytime,product_code,money from test_payment_two;
#8、将表test_payment_two的paytime字段显示为“YYYYMMDD”的的字符串格式。
select to_char(to_date(paytime,'yyyy-mm-dd hh24:mi:ss'), 'yyyymmdd')paytime,product_code,money from test_payment_two;

#10、查询表test_payment_two中 “2019-5-1 9点” 以后的数据
select * from test_payment_two
where paytime>'2019-5-1 9';

#11、查询表test_payment_two中“2019-5-1”至“2019-8-1”之间的数据
select * from test_payment_two
where paytime between '2019-5-1' and '2019-8-1';

#12、将表test_payment_three的paytime字段显示为“YYYY-MM-DD HH:MM:SS”的时间格式。
select to_date(paytime,'yyyy-mm-dd hh24:mi:ss')paytime,product_code,money from test_payment_three;

#13、将表test_payment_three的paytime字段显示为“YYYY-MM-DD”的时间格式。
select to_date(paytime,'yyyy-mm-dd')paytime,product_code,money from test_payment_three;

#14、将表test_payment_three的paytime字段显示为“YYYYMMDD”的字符串格式。
select * from test_payment_three;

#15、将表test_payment_three的paytime字段显示为“YYYY-MM-DD HH:MM:SS”的字符串格式。
select  to_char(to_date(paytime,'yyyy-mm-dd  hh24:mi:ss'),'yyyy-mm-dd  hh24:mi:ss')paytime,product_code,money from test_payment_three;

#16、查询表test_payment_three中 “2019-3-1” 以后的数据
select * from test_payment_three
where to_date(paytime,'yyyy-mm-dd')>to_date('2019-3-1','yyyy-mm-dd');

#17、查询表test_payment_three中“2019-5-1”至“2019-8-1”之间的数据
select * from test_payment_three
where to_date(paytime,'yyyy-mm-dd')between to_date('2019-5-1','yyyy-mm-dd') and to_date('2019-8-1','yyyy-mm-dd');

#18、将表test_payment_four的paytime字段显示为“YYYY-MM-DD”的字符串格式。
select to_char(paytime,'yyyy-mm-dd ')paytime,product_code,money from test_payment_four;

#19、将表test_payment_four的paytime字段显示为“YYYYMMDD”的字符串格式。
select to_char(paytime,'yyyymmdd')paytime,product_code,money from test_payment_four;

#20、查询表test_payment_four中 “2019-3-1” 以后的数据
select * from test_payment_four
where paytime>to_date('2019-3-1','yyyy-mm-dd');
#21、查询表test_payment_four中“2019-5-1”至“2019-8-1”之间的数据。
select * from test_payment_four
where paytime between to_date('2019-5-1','yyyy-mm-dd') and to_date('2019-8-1','yyyy-mm-dd');

#22、将表test_payment_five的paytime字段显示为“YYYY-MM-DD  HH:MM:SS”的字符串格式。
select to_char(paytime,'yyyy-mm-dd  hh24:mi:ss')paytime,product_code,money from test_payment_five;

#23、将表test_payment_five的paytime字段显示为“YYYYMMDD”的字符串格式。
select to_char(paytime,'yyyymmdd')paytime,product_code,money from test_payment_five;

#24、查询表test_payment_five中 “2019-3-1” 以后的数据
select * from test_payment_five
where paytime>to_date('2019-3-1','yyyy-mm-dd');

#25、查询表test_payment_five中“2019-5-1”至“2019-8-1”之间的数据
select * from test_payment_five
where paytime between to_date('2019-5-1','yyyy-mm-dd') and to_date('2019-8-1','yyyy-mm-dd');

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值