Converting from one date format to another using DB Browser for SQLite

后端 未结 1 1276
逝去的感伤
逝去的感伤 2021-01-25 03:24

I am looking to convert one date format to another using SQL. I am using DB Browser for SQLite and the dates are stored in a column of type \'TEXT\'.

Here are 2 examples

相关标签:
1条回答
  • 2021-01-25 03:48

    So I don't know much about python so had to go with the 'obscene chain of instr() and substr() commands'. It's a mess, but it seems to work now.

    instr(DATE,'/') used to find the position of the first '/'

    length (rtrim(DATE, 'PMA0123456789: ')) used to find the position of the second '/'

    instr(DATE, ':') used to find the position of the first ':'

    Well below is the mess I created :D

    UPDATE testtable
    SET DATE = 
    substr (DATE, length (rtrim(DATE, 'PMA0123456789: '))+1,4) || '-' ||
    CASE instr(DATE, '/')
        WHEN 2 THEN
            '0' 
        ELSE ''
    END 
    || substr (DATE,1,instr(DATE,'/')-1) || '-' ||
    CASE (length (rtrim(DATE, 'PMA0123456789: '))) - instr(DATE,'/')
        WHEN 2 THEN
            '0' 
        ELSE '' 
    END 
    || substr (DATE,instr(DATE,'/')+1, length (rtrim(DATE, 'PMA0123456789: ')) - instr(DATE,'/')-1)
    || ' ' || 
    CASE substr(DATE,length(DATE)-1,2)
        WHEN 'AM' THEN
            CASE substr(DATE, instr(DATE, ':')-2,2)
                WHEN '12' then
                    '00'
                ELSE
                    CASE substr(DATE, instr(DATE, ':')-2, 1)
                        WHEN ' ' THEN 
                            '0'
                        ELSE
                            substr(DATE, instr(DATE, ':')-2,1)  
                    END
                    || substr(DATE, instr(DATE, ':')-1,1) 
            END
        WHEN 'PM' THEN
            CASE substr(DATE, instr(DATE, ':')-2,2) 
                WHEN '12' THEN
                    substr(DATE, instr(DATE, ':')-2,2) 
                ELSE
                    CAST (substr(DATE,instr(DATE, ':')-2,2) AS INT) + 12 
            END
        ELSE
            'error'
    END 
    || ':' || substr(DATE,instr(DATE, ':')+1,5);
    
    0 讨论(0)
提交回复
热议问题