[英]Convert timestamp to date in Oracle SQL
How can we convert timestamp to date?我们如何将时间戳转换为日期?
The table has a field, start_ts
which is of the timestamp
format:该表有一个字段
start_ts
,它是timestamp
格式:
'05/13/2016 4:58:11.123456 PM'
I need to query the table and find the maximum and min timestamp
in the table but I'm not able to.我需要查询表并找到表中的最大和最小
timestamp
,但我不能。
Select max(start_ts)
from db
where cast(start_ts as date) = '13-may-2016'
But the query is not returning any values.但是查询没有返回任何值。
Please help me in finding the max timestamp for a date.请帮助我找到日期的最大时间戳。
CAST(timestamp_expression AS DATE)
例如,查询是: SELECT CAST(SYSTIMESTAMP AS DATE) FROM dual;
Try using TRUNC
and TO_DATE
instead尝试改用
TRUNC
和TO_DATE
WHERE
TRUNC(start_ts) = TO_DATE('2016-05-13', 'YYYY-MM-DD')
Alternatively, you can use >=
and <
instead to avoid use of function in the start_ts
column:或者,您可以使用
>=
和<
来避免在start_ts
列中使用函数:
WHERE
start_ts >= TO_DATE('2016-05-13', 'YYYY-MM-DD')
AND start_ts < TO_DATE('2016-05-14', 'YYYY-MM-DD')
Format like this while selecting:选择时像这样格式化:
to_char(systimestamp, 'DD-MON-YYYY')
Eg:例如:
select to_char(systimestamp, 'DD-MON-YYYY') from dual;
If the datatype is timestamp then the visible format is irrelevant.如果数据类型是时间戳,则可见格式无关紧要。
You should avoid converting the data to date or use of to_char.您应该避免将数据转换为日期或使用 to_char。 Instead compare the timestamp data to timestamp values using TO_TIMESTAMP()
而是使用 TO_TIMESTAMP() 将时间戳数据与时间戳值进行比较
WHERE start_ts >= TO_TIMESTAMP('2016-05-13', 'YYYY-MM-DD')
AND start_ts < TO_TIMESTAMP('2016-05-14', 'YYYY-MM-DD')
你可以试试简单的
select to_date('2020-07-08T15:30:42Z','yyyy-mm-dd"T"hh24:mi:ss"Z"') from dual;
您可以使用:
select to_date(to_char(date_field,'dd/mm/yyyy')) from table
I'd go with the following:我会选择以下内容:
Select max(start_ts)
from db
where trunc(start_ts) = date'13-may-2016'
If you have milliseconds in the date string, you can use the following.如果日期字符串中有毫秒,则可以使用以下内容。
select TO_TIMESTAMP(SUBSTR('2020-09-10T09:37:28.378-07:00',1,23), 'YYYY-MM-DD"T"HH24:MI:SS:FF3')From Dual;
And then convert it to Date with:然后将其转换为 Date :
select trunc(TO_TIMESTAMP(SUBSTR('2020-09-10T09:37:28.378-07:00',1,23), 'YYYY-MM-DD"T"HH24:MI:SS:FF3')) From Dual;
It worked for me, hope it will help you as well.它对我有用,希望它也能帮助你。
This may not be the correct way to do it.这可能不是正确的方法。 But I have solved the problem using substring function.
但是我已经使用 substring 函数解决了这个问题。
Select max(start_ts), min(start_ts)from db where SUBSTR(start_ts, 0,9) ='13-may-2016'
using this I was able to retrieve the max and min timestamp.使用这个我能够检索最大和最小时间戳。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.