简体   繁体   English

Oracle — 根据 varchar 时间戳提取月度数据

[英]Oracle — Pulling monthly data based on varchar timestamp

I have an oracle database that I'm trying to extract data from on a month by month basis.我有一个 oracle 数据库,我正在尝试按月从中提取数据。

The table and field I'm querying has the following structure我正在查询的表和字段具有以下结构

select ID, lastmodifieddate as LMDate from salesforce.X_TASK WHERE ROWNUM <= 10
('ID', <cx_Oracle.DbType DB_TYPE_VARCHAR>, 18, 18, None, None, 1)
('LMDATE', <cx_Oracle.DbType DB_TYPE_VARCHAR>, 25, 25, None, None, 1)
('00T4N00002QhFvjUAF', '2019-09-06T21:05:04.000Z')
('00T4N00002QhFvmUAF', '2019-09-06T21:05:04.000Z')
('00T4N00002QhFvnUAF', '2019-09-06T21:05:04.000Z')
('00T4N00002QhFvsUAF', '2019-09-06T21:05:04.000Z')
('00T4N00002QhFvtUAF', '2019-09-06T21:05:04.000Z')
('00T4N00002QhFw0UAF', '2019-09-06T21:05:04.000Z')
('00T4N00002QhFw1UAF', '2019-09-06T21:05:04.000Z')
('00T4N00002QhFwAUAV', '2019-09-06T21:05:09.000Z')
('00T4N00002QhFwBUAV', '2019-09-06T21:05:09.000Z')
('00T4N00002QhFwMUAV', '2019-09-06T21:05:09.000Z')

What is the best way to extract only the date from the varchar timestamp field?仅从 varchar 时间戳字段中提取日期的最佳方法是什么?

I have tried changing the field to_char我尝试将字段更改为_char

select ID, to_char(LASTMODIFIEDDATE, 'YYYY-MM-DD') from salesforce.X_TASK 
WHERE LASTMODIFIEDDATE >= to_date('2020-05-31', 'YYYY-MM-DD') 
and  LASTMODIFIEDDATE < to_date('2020-06-30', 'YYYY-MM-DD')

But end up with this error但最终出现此错误

ORA-01861: literal does not match format string

Same if I try to change it to a date如果我尝试将其更改为日期,则相同

select ID, to_date(LASTMODIFIEDDATE, 'YYYY-MM-DD') from salesforce.X_TASK 
WHERE LASTMODIFIEDDATE >= to_date('2020-05-31', 'YYYY-MM-DD') 
and  LASTMODIFIEDDATE < to_date('2020-06-30', 'YYYY-MM-DD')

ORA-01861: literal does not match format string

What do you call a "timestamp", exactly?究竟什么是“时间戳”? How does it look like?它看起来怎样? Date, time up to... seconds?日期、时间到...秒?

Anyway: as you're storing timestamp as a string (bad idea, generally), and if all of its values share the same format (eg 25.02.2021 20:02:00 which is dd.mm.yyyy hh24:mi:ss ), then the simplest way to do that is to use a SUBSTR function.无论如何:因为您将时间戳存储为字符串(通常是坏主意),并且如果它的所有值共享相同的格式(例如25.02.2021 20:02:00dd.mm.yyyy hh24:mi:ss ),那么最简单的方法是使用SUBSTR function。 In your case:在你的情况下:

select id,
       substr(lastmodifieddate, 1, 10) as datum
from salesforce.X_TASK 
where to_date(lastmodifieddate, 'dd.mm.yyyy') >= to_date('2020-05-31', 'YYYY-MM-DD') 
  and to_date(lastmodifieddate, 'dd.mm.yyyy') <  to_date('2020-06-30', 'YYYY-MM-DD');

Of course , if format isn't dd.mm.yyyy (as in my example), you'd use yours.当然,如果格式不是dd.mm.yyyy (如我的示例),你会使用你的。 Note that query will fail if there are value that don't follow such a format, contain letters, whatnot.请注意,如果存在不遵循这种格式的值,包含字母等,查询将失败。 That's why I said that storing timestamps (or dates) as strings is a bad, BAD idea.这就是为什么我说将时间戳(或日期)存储为字符串是一个坏主意。


After reading your comment: it seems that column contains whole lot of data, not only the timestamp.阅读您的评论后:似乎该列包含大量数据,而不仅仅是时间戳。 OK then - extract the date piece out of it.好的然后 - 从中提取日期片段。 SUBSTR + INSTR would do: SUBSTR + INSTR会做:

SQL> with test (col) as
  2    (select q'[<cx_Oracle.DbType DB_TYPE_VARCHAR>, 25, 25, None, None, 1) ('2016-10-27T19:27:14.000Z')]'
  3     from dual
  4    )
  5  select substr(col, instr(col, '(') + 2, 10) datum
  6  from test
  7  /

DATUM
----------
2016-10-27

SQL>

Now, apply TO_DATE to it with appropriate format mask and use it in WHERE clause:现在,使用适当的格式掩码对其应用TO_DATE并在WHERE子句中使用它:

select id,
       to_date(substr(lastmodifieddate, instr(lastmodifieddate, '(') + 2, 10), 'yyyy-mm-dd') datum
from x_task
where to_date(substr(lastmodifieddate, instr(lastmodifieddate, '(') + 2, 10), 'yyyy-mm-dd') >= to_date('2020-05-31', 'YYYY-MM-DD') 
  and to_date(substr(lastmodifieddate, instr(lastmodifieddate, '(') + 2, 10), 'yyyy-mm-dd') <  to_date('2020-06-30', 'YYYY-MM-DD');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM