Sqlite将string转换为date

我有date存储为一个像“28/11/2010”SQLite数据库中的string。 我想将string转换为date。

具体来说,我必须在两个date之间转换大量的stringdate。

在postgresql中,我使用to_date('30/11/2010','dd/MM/yyyy') ,如何用sqlite做同样的事情?

像这样的东西:

 SELECT * FROM table WHERE to_date(column,'dd/MM/yyyy') BETWEEN to_date('01/11/2010','dd/MM/yyyy') AND to_date('30/11/2010','dd/MM/yyyy') 

由于Sqlite 没有datetypes,你将需要做string比较来实现这一点。 为了这个工作,你需要颠倒顺序 – 例如从dd / MM / yyyy到yyyyMMdd,使用类似

 where substr(column,7)||substr(column,4,2)||substr(column,1,2) between '20101101' and '20101130' 

保存date为TEXT(20/10/2013 03:26)要查询和selectdate之间的logging?

更好的版本是:

 SELECT TIMSTARTTIMEDATE FROM TIMER WHERE DATE(substr(TIMSTARTTIMEDATE,7,4) ||substr(TIMSTARTTIMEDATE,4,2) ||substr(TIMSTARTTIMEDATE,1,2)) BETWEEN DATE(20131020) AND DATE(20131021); 

从2013年10月20日的substr给出20131020date格式DATE(20131021) – 使SQL与date和使用date和时间函数工作。

要么

 SELECT TIMSTARTTIMEDATE FROM TIMER WHERE DATE(substr(TIMSTARTTIMEDATE,7,4) ||'-' ||substr(TIMSTARTTIMEDATE,4,2) ||'-' ||substr(TIMSTARTTIMEDATE,1,2)) BETWEEN DATE('2013-10-20') AND DATE('2013-10-21'); 

这里是一行

 SELECT TIMSTARTTIMEDATE FROM TIMER WHERE DATE(substr(TIMSTARTTIMEDATE,7,4)||'-'||substr(TIMSTARTTIMEDATE,4,2)||'-'||substr(TIMSTARTTIMEDATE,1,2)) BETWEEN DATE('2013-10-20') AND DATE('2013-10-21'); 

有一点你应该看看SQLite的date和时间函数 ,特别是如果你将不得不操纵很多date。 以更改内部格式(必须是ISO,即yyyy-MM-dd)为代价,使用date是一种理智的方法。

与脆弱的substr值相比,UDF方法是我的偏好。

 #!/usr/bin/env python3 import sqlite3 from dateutil import parser from pprint import pprint def date_parse(s): ''' Converts a string to a date ''' try: t = parser.parse(s, parser.parserinfo(dayfirst=True)) return t.strftime('%Y-%m-%d') except: return None def dict_factory(cursor, row): ''' Helper for dict row results ''' d = {} for idx, col in enumerate(cursor.description): d[col[0]] = row[idx] return d def main(): ''' Demonstrate UDF ''' with sqlite3.connect(":memory:") as conn: conn.row_factory = dict_factory setup(conn) ################################################## # This is the code that matters. The rest is setup noise. conn.create_function("date_parse", 1, date_parse) cur = conn.cursor() cur.execute(''' select "date", date_parse("date") as parsed from _test order by 2; ''') pprint(cur.fetchall()) ################################################## def setup(conn): ''' Setup some values to parse ''' cur = conn.cursor() # Make a table sql = ''' create table _test ( "id" integer primary key, "date" text ); ''' cur.execute(sql) # Fill the table dates = [ '2/1/03', '03/2/04', '4/03/05', '05/04/06', '6/5/2007', '07/6/2008', '8/07/2009', '09/08/2010', '2-1-03', '03-2-04', '4-03-05', '05-04-06', '6-5-2007', '07-6-2008', '8-07-2009', '09-08-2010', '31/12/20', '31-12-2020', 'BOMB!', ] params = [(x,) for x in dates] cur.executemany(''' insert into _test ("date") values(?); ''', params) if __name__ == "__main__": main() 

这会给你这些结果:

 [{'date': 'BOMB!', 'parsed': None}, {'date': '2/1/03', 'parsed': '2003-01-02'}, {'date': '2-1-03', 'parsed': '2003-01-02'}, {'date': '03/2/04', 'parsed': '2004-02-03'}, {'date': '03-2-04', 'parsed': '2004-02-03'}, {'date': '4/03/05', 'parsed': '2005-03-04'}, {'date': '4-03-05', 'parsed': '2005-03-04'}, {'date': '05/04/06', 'parsed': '2006-04-05'}, {'date': '05-04-06', 'parsed': '2006-04-05'}, {'date': '6/5/2007', 'parsed': '2007-05-06'}, {'date': '6-5-2007', 'parsed': '2007-05-06'}, {'date': '07/6/2008', 'parsed': '2008-06-07'}, {'date': '07-6-2008', 'parsed': '2008-06-07'}, {'date': '8/07/2009', 'parsed': '2009-07-08'}, {'date': '8-07-2009', 'parsed': '2009-07-08'}, {'date': '09/08/2010', 'parsed': '2010-08-09'}, {'date': '09-08-2010', 'parsed': '2010-08-09'}, {'date': '31/12/20', 'parsed': '2020-12-31'}, {'date': '31-12-2020', 'parsed': '2020-12-31'}] 

SQLite等价于任何强健的东西都是你应该避免的substrinstr调用的纠结编织。

这是为fecha(文本)格式dateYYYY-MM-dd HH:mm:ss例如我想Ene-05-2014(2014-01-05)的所有logging:

 SELECT fecha FROM Mytable WHERE DATE(substr(fecha ,1,4) ||substr(fecha ,6,2)||substr(fecha ,9,2)) BETWEEN DATE(20140105) AND DATE(20140105); 

我将date存储为“DD-MON-YYYY格式(2016年6月10日)”,下面的查询适用于我在两个date之间searchlogging。

 select date, substr(date,8,11) || '-' || substr(date,4,4) || substr(date, 1,2), case substr(date, 4,3) when 'Jan' then strftime('%s', replace(substr(date,8,11) || '-' || substr(date,4,4) || substr(date, 1,2), 'Jan' , '01')) when 'Feb' then strftime('%s', replace(substr(date,8,11) || '-' || substr(date,4,4) || substr(date, 1,2), 'Feb' , '02')) when 'Mar' then strftime('%s', replace(substr(date,8,11) || '-' || substr(date,4,4) || substr(date, 1,2), 'Mar' , '03')) when 'Apr' then strftime('%s', replace(substr(date,8,11) || '-' || substr(date,4,4) || substr(date, 1,2), 'Apr' , '04')) when 'May' then strftime('%s', replace(substr(date,8,11) || '-' || substr(date,4,4) || substr(date, 1,2), 'May' , '05')) when 'Jun' then strftime('%s', replace(substr(date,8,11) || '-' || substr(date,4,4) || substr(date, 1,2), 'Jun' , '06')) when 'Jul' then strftime('%s', replace(substr(date,8,11) || '-' || substr(date,4,4) || substr(date, 1,2), 'Jul' , '07')) when 'Aug' then strftime('%s', replace(substr(date,8,11) || '-' || substr(date,4,4) || substr(date, 1,2), 'Aug' , '08')) when 'Sep' then strftime('%s', replace(substr(date,8,11) || '-' || substr(date,4,4) || substr(date, 1,2), 'Sep' , '09')) when 'Oct' then strftime('%s', replace(substr(date,8,11) || '-' || substr(date,4,4) || substr(date, 1,2), 'Oct' , '10')) when 'Nov' then strftime('%s', replace(substr(date,8,11) || '-' || substr(date,4,4) || substr(date, 1,2), 'Nov' , '11')) when 'Dec' then strftime('%s', replace(substr(date,8,11) || '-' || substr(date,4,4) || substr(date, 1,2), 'Dec' , '12')) else '0' end as srcDate from payment where srcDate >= strftime('%s', '2016-07-06') and srcDate <= strftime('%s', '2016-09-06'); 

如果源date格式不一致substr函数有一些问题,即:

2017/1/1 1/11/2017 11/11/2017 1/1/17等。

所以我用一个临时表跟着另一个应用程序。 如果存在,此片段输出“YYYY-MM-DD”+时间。

  BEGIN; CREATE TEMP TABLE [DateconvertionTable] (Id TEXT PRIMARY KEY, OriginalDate TEXT , SepA INTEGER, DayPart TEXT,Rest1 TEXT, SepB INTEGER, MonthPart TEXT, Rest2 TEXT, SepC INTEGER, YearPart TEXT, Rest3 TEXT, NewDate TEXT); INSERT INTO [DateconvertionTable] (Id,OriginalDate) SELECT SourceIdColumn, SourceDateColumn From [SourceTable]; --day Part (If day is first) UPDATE [DateconvertionTable] SET SepA=instr(OriginalDate ,'/'); UPDATE [DateconvertionTable] SET DayPart=substr(OriginalDate,1,SepA-1) ; UPDATE [DateconvertionTable] SET Rest1=substr(OriginalDate,SepA+1); --Month Part (If Month is second) UPDATE [DateconvertionTable] SET SepB=instr(Rest1,'/'); UPDATE [DateconvertionTable] SET MonthPart=substr(Rest1, 1,SepB-1); UPDATE [DateconvertionTable] SET Rest2=substr(Rest1,SepB+1); --Year Part (3d) UPDATE [DateconvertionTable] SET SepC=instr(Rest2,' '); --Use Cases In case of time string included UPDATE [DateconvertionTable] SET YearPart= CASE WHEN SepC=0 THEN Rest2 ELSE substr(Rest2,1,SepC-1) END; --The Rest considered time UPDATE [DateconvertionTable] SET Rest3= CASE WHEN SepC=0 THEN '' ELSE substr(Rest2,SepC+1) END; -- Convert 1 digit day and month to 2 digit UPDATE [DateconvertionTable] SET DayPart=0||DayPart WHERE CAST(DayPart AS INTEGER)<10; UPDATE [DateconvertionTable] SET MonthPart=0||MonthPart WHERE CAST(MonthPart AS INTEGER)<10; --If there is a need to convert 2 digit year to 4 digit year, make some assumptions... UPDATE [DateconvertionTable] SET YearPart=19||YearPart WHERE CAST(YearPart AS INTEGER)>=44 AND CAST(YearPart AS INTEGER)<100; UPDATE [DateconvertionTable] SET YearPart=20||YearPart WHERE CAST(YearPart AS INTEGER)<44 AND CAST(YearPart AS INTEGER)<100; UPDATE [DateconvertionTable] SET NewDate = YearPart || '-' || MonthPart || '-' || DayPart || ' ' || Rest3; UPDATE [SourceTable] SET SourceDateColumn=(Select NewDate FROM DateconvertionTable WHERE [DateconvertionTable].id=SourceIdColumn); END;