sqlite 如何在sql/r中将字符串更改为日期时间格式?

hgc7kmma  于 2022-12-04  发布在  SQLite
关注(0)|答案(1)|浏览(561)

我正在尝试查找一条短信的最早和最晚时间戳。我知道我必须将以下列更改为日期格式才能排序:

Fri May 26 17:30:01 +0000 2017  
Fri May 26 17:30:05 +0000 2017  
Fri May 26 17:30:05 +0000 2017  
Fri May 26 17:30:04 +0000 2017  
Fri May 26 17:30:12 +0000 2017

我已经尝试使用'substr'函数转换为YYYY-MM-DD HH:MM:SS
这就是我现在所拥有的:

dbGetQuery(db2, "SELECT text,
  CAST(
      SUBSTR(created_at,-4) || '-' ||
      CASE SUBSTR(created_at,5,3)
         WHEN 'Jan' THEN 01
         WHEN 'Feb' THEN 02
         WHEN 'Mar' THEN 03
         WHEN 'Apr' THEN 04
         WHEN 'May' THEN 05
         WHEN 'Jun' THEN 06
         WHEN 'Jul' THEN 07
         WHEN 'Aug' THEN 08
         WHEN 'Sep' THEN 09
         WHEN 'Oct' THEN 10
         WHEN 'Nov' THEN 11
         WHEN 'Dec' THEN 12
      END || '-' ||
      SUBSTR(created_at, 9,2)
   AS date)
           FROM tweets")

我只得到YYYY。任何帮助将不胜感激。谢谢。

vfhzx4xs

vfhzx4xs1#

使用注解末尾可重复定义的tweets,提取并连接CTE中的日期时间组件(即with子句),然后使用该子句查找created_at的最小值和最大值。我们已使用sqldf以可重复的方式简洁地表示它,但相同的SQL语句应直接用于RSQLite。

library(sqldf)

sqldf("with d as (
   select format('%s-%02d-%02d %s',
     substr(created_at, -4, 4),
     instr('  JanFebMarAprMayJunJulAugSepOctNovDec', substr(created_at, 5, 3))/3,
     substr(created_at, 9, 2),
     substr(created_at, 12, 8)) as created_at
   from tweets
  )
  select min(created_at) as min, max(created_at) as max from d")
##                   min                 max
## 1 2017-05-26 17:30:01 2017-05-26 17:30:12

这种变化也起作用。

sqldf("with d as (
   select 
     substr(created_at, -4, 4) || '-' ||
     substr(0 || (instr('  JanFebMarAprMayJunJulAugSepOctNovDec', 
           substr(created_at, 5, 3))/3), -2, 2) || '-' ||
     substr(0 || substr(created_at, 9, 2), -2, 2) || ' ' ||
     substr(created_at, 12, 8) as created_at
   from tweets
  )
  select min(created_at) as min, max(created_at) as max from d")
##                   min                 max
## 1 2017-05-26 17:30:01 2017-05-26 17:30:12

注意

Lines <- "created_at 
Fri May 26 17:30:01 +0000 2017  
Fri May 26 17:30:05 +0000 2017  
Fri May 26 17:30:05 +0000 2017  
Fri May 26 17:30:04 +0000 2017  
Fri May 26 17:30:12 +0000 2017"
tweets <- read.csv(text = Lines, strip.white = TRUE)

相关问题