odps timestamp转date

本文介绍了在SQL中如何进行日期时间转换,包括使用CAST函数将timestamp字段转换为日期,以及运用内置的GETDATE()、UNIX_TIMESTAMP()和FROM_UNIXTIME()函数进行当前日期时间的处理和转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1、使用cast强转

2、使用内置函数


1、使用cast强转
--timestamp 字段转日期
select cast(timestamp as date);
2、使用内置函数
--当前日期时间 当前时间转timestamp  timestamp转日期时间
select getdate(),unix_timestamp(now()),from_unixtime(1691135518);

结果展现:

2.1 查询结果
_c0_c1_c2
2023-08-04 15:51:5816911355182023-08-04 15:51:58

以下是将该代码换为ODPS Python的示例代码: ```python import time import datetime import pytz from odps import ODPS from odps.models import Schema, PartitionSpec from odps.df import DataFrame # 设置ODPS相关参数 access_id = 'your_access_id' access_key = 'your_access_key' project_name = 'your_project_name' endpoint = 'your_endpoint' table_name = 'raw_access_ad' partition_spec = PartitionSpec(dt='20220101', hour='00') # 创建ODPS对象 odps = ODPS(access_id=access_id, access_key=access_key, project=project_name, endpoint=endpoint) # 定义函数 def is_valid_date(date_str): try: time.strptime(date_str, '%Y%m%d') return True except: return False def is_valid_hour(hour_str): try: h = int(hour_str) if h >= 0 and h <= 23: return True else: return False except: return False # 获取当前时间 tz = pytz.timezone('Asia/Shanghai') last_day_time = datetime.datetime.fromtimestamp(int(time.time()), tz) + datetime.timedelta(hours=-1) last_day_year = last_day_time.strftime('%Y') last_day_month = last_day_time.strftime('%m') last_day_day = last_day_time.strftime('%d') last_day_hour = last_day_time.strftime('%H') # 判断日期和小时是否合法 if is_valid_date(last_day_year + last_day_month + last_day_day) and is_valid_hour(last_day_hour): print(f'传入的时间参数 exec_ymd为【{last_day_year + last_day_month + last_day_day}】,hour为【{last_day_hour}】') else: print('时间参数不合法') # 读取数据 df = DataFrame(odps.get_table(table_name).open_reader(partition=partition_spec)) # 打印数据 print(df.head(10)) ``` 需要注意的是,这段代码需要替换以下内容为您自己ODPS的相关参数: - access_id:您的ODPS Access ID。 - access_key:您的ODPS Access Key。 - project_name:您的ODPS项目名称。 - endpoint:您的ODPS Endpoint地址。 - table_name:您需要读取的ODPS表名称。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值