Convert daytime in string to datetime in sqllite 3?
I'm storing datetime value in SqlLite3 in TEXT column. data is stored as 2018 Nov 16 15:53:01
. How do I convert it back to datetime ?
sqlite datetime timestamp
add a comment |
I'm storing datetime value in SqlLite3 in TEXT column. data is stored as 2018 Nov 16 15:53:01
. How do I convert it back to datetime ?
sqlite datetime timestamp
1
AFAIK, there is no way to convert a date string withNov
, even usingstrftime
. You should always store your dates in SQLite using an ISO format which will sort properly. So, you should have stored2018-11-16 15:53:01
.
– Tim Biegeleisen
Nov 22 '18 at 8:53
You could do it withsubstr()
and a bigCASE
expression to turn the month name into a number, but, yeah, it's better to store your timestamp in one of the formats understood by sqlite date & time functions. This also has extra benefits like being able to sort them.
– Shawn
Nov 22 '18 at 9:12
You could use a scripting language like Python to query the values, parse them as datetimes and write back to database. Syntax would bedt = datetime.strptime(s, '%Y %b %d %H:%M:%S')
– SuperShoot
Nov 22 '18 at 9:18
add a comment |
I'm storing datetime value in SqlLite3 in TEXT column. data is stored as 2018 Nov 16 15:53:01
. How do I convert it back to datetime ?
sqlite datetime timestamp
I'm storing datetime value in SqlLite3 in TEXT column. data is stored as 2018 Nov 16 15:53:01
. How do I convert it back to datetime ?
sqlite datetime timestamp
sqlite datetime timestamp
asked Nov 22 '18 at 8:44
kumarakumara
145
145
1
AFAIK, there is no way to convert a date string withNov
, even usingstrftime
. You should always store your dates in SQLite using an ISO format which will sort properly. So, you should have stored2018-11-16 15:53:01
.
– Tim Biegeleisen
Nov 22 '18 at 8:53
You could do it withsubstr()
and a bigCASE
expression to turn the month name into a number, but, yeah, it's better to store your timestamp in one of the formats understood by sqlite date & time functions. This also has extra benefits like being able to sort them.
– Shawn
Nov 22 '18 at 9:12
You could use a scripting language like Python to query the values, parse them as datetimes and write back to database. Syntax would bedt = datetime.strptime(s, '%Y %b %d %H:%M:%S')
– SuperShoot
Nov 22 '18 at 9:18
add a comment |
1
AFAIK, there is no way to convert a date string withNov
, even usingstrftime
. You should always store your dates in SQLite using an ISO format which will sort properly. So, you should have stored2018-11-16 15:53:01
.
– Tim Biegeleisen
Nov 22 '18 at 8:53
You could do it withsubstr()
and a bigCASE
expression to turn the month name into a number, but, yeah, it's better to store your timestamp in one of the formats understood by sqlite date & time functions. This also has extra benefits like being able to sort them.
– Shawn
Nov 22 '18 at 9:12
You could use a scripting language like Python to query the values, parse them as datetimes and write back to database. Syntax would bedt = datetime.strptime(s, '%Y %b %d %H:%M:%S')
– SuperShoot
Nov 22 '18 at 9:18
1
1
AFAIK, there is no way to convert a date string with
Nov
, even using strftime
. You should always store your dates in SQLite using an ISO format which will sort properly. So, you should have stored 2018-11-16 15:53:01
.– Tim Biegeleisen
Nov 22 '18 at 8:53
AFAIK, there is no way to convert a date string with
Nov
, even using strftime
. You should always store your dates in SQLite using an ISO format which will sort properly. So, you should have stored 2018-11-16 15:53:01
.– Tim Biegeleisen
Nov 22 '18 at 8:53
You could do it with
substr()
and a big CASE
expression to turn the month name into a number, but, yeah, it's better to store your timestamp in one of the formats understood by sqlite date & time functions. This also has extra benefits like being able to sort them.– Shawn
Nov 22 '18 at 9:12
You could do it with
substr()
and a big CASE
expression to turn the month name into a number, but, yeah, it's better to store your timestamp in one of the formats understood by sqlite date & time functions. This also has extra benefits like being able to sort them.– Shawn
Nov 22 '18 at 9:12
You could use a scripting language like Python to query the values, parse them as datetimes and write back to database. Syntax would be
dt = datetime.strptime(s, '%Y %b %d %H:%M:%S')
– SuperShoot
Nov 22 '18 at 9:18
You could use a scripting language like Python to query the values, parse them as datetimes and write back to database. Syntax would be
dt = datetime.strptime(s, '%Y %b %d %H:%M:%S')
– SuperShoot
Nov 22 '18 at 9:18
add a comment |
2 Answers
2
active
oldest
votes
Assuming :-
- that the table is named convdt, and
- that you wish to change(UPDATE) all the rows in the table itself, and
- that the table is not defined using the WITHOUT ROWID keywords
then I believe that the following will do that :-
WITH
cte1(rowid,dc1) AS (
SELECT
rowid,
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(data,'Jan','01'),
'Feb','02'),
'Mar','03'),
'Apr','04'),
'May','05'),
'Jun','06'),
'Jul','07'),
'Aug','09'),
'Sep','09'),
'Oct','10'),
'Nov','11'),
'Dec','12')
FROM convdt
),
cte2(rowid,dc2) AS ( SELECT rowid,substr(dc1,1,4)||'-'||substr(dc1,6,2)||'-'||substr(dc1,9) FROM cte1)
UPDATE convdt SET data = (SELECT dc2 FROM cte2 WHERE cte2.rowid = rowid);
SELECT * FROM convdt;
This uses two Common Table Expressions.
The first cte1 extracts the rowid and the data columns replace Jan with 01, Feb with 02 and so on (naming the data column as dc1)
The second cte2 then extracts the rowid and the dc1 column from cte1 reformatting to adhere to YYYY-MM-DD hh:mm:ss format.
All rows of the convdt table are then updated, obtaining the replacement value from cte2 according to the rowid.
Working Example/Test Code
DROP TABLE IF EXISTS convdt; -- For testing only drop the table
CREATE TABLE IF NOT EXISTS convdt (data TEXT); -- For testing only create the table
INSERT INTO convdt VALUES -- For testing only populate the table
('2018 Jan 16 15:53:01'),('2018 Feb 16 15:53:01'),('2018 Mar 16 15:53:01'),('2018 Apr 16 15:53:01'),
('2018 May 16 15:53:01'),('2018 Jun 16 15:53:01'),('2018 Jul 16 15:53:01'),('2018 Aug 16 15:53:01'),
('2018 Sep 16 15:53:01'),('2018 Oct 16 15:53:01'),('2018 Nov 16 15:53:01'),('2018 Dec 16 15:53:01')
;
SELECT * FROM convdt; -- Extract all the data for comparison before the updates
WITH
cte1(rowid,dc1) AS (
SELECT
rowid,
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(data,'Jan','01'),
'Feb','02'),
'Mar','03'),
'Apr','04'),
'May','05'),
'Jun','06'),
'Jul','07'),
'Aug','09'),
'Sep','09'),
'Oct','10'),
'Nov','11'),
'Dec','12')
FROM convdt
),
cte2(rowid,dc2) AS ( SELECT rowid,substr(dc1,1,4)||'-'||substr(dc1,6,2)||'-'||substr(dc1,9) FROM cte1)
UPDATE convdt SET data = (SELECT dc2 FROM cte2 WHERE cte2.rowid = rowid);
SELECT * FROM convdt; -- Extract all the data to show the result of the conversion
Results
Before the table is updated :-
After the conversion :-
add a comment |
This will do the update, simply by changing the MMM
month part with a double digit number.
The last CASE
statement checks if the day number is only 1 digit and if so it pads it left with 1 0
.
UPDATE test SET datecol =
substr(datecol, 1, 4) || '-' ||
CASE substr(datecol, 6, 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'
ELSE substr(datecol, 6, 3)
END || '-' ||
CASE substr(datecol, 11, 1)
WHEN ' ' THEN '0' || substr(datecol, 10)
ELSE substr(datecol, 10)
END
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426932%2fconvert-daytime-in-string-to-datetime-in-sqllite-3%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Assuming :-
- that the table is named convdt, and
- that you wish to change(UPDATE) all the rows in the table itself, and
- that the table is not defined using the WITHOUT ROWID keywords
then I believe that the following will do that :-
WITH
cte1(rowid,dc1) AS (
SELECT
rowid,
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(data,'Jan','01'),
'Feb','02'),
'Mar','03'),
'Apr','04'),
'May','05'),
'Jun','06'),
'Jul','07'),
'Aug','09'),
'Sep','09'),
'Oct','10'),
'Nov','11'),
'Dec','12')
FROM convdt
),
cte2(rowid,dc2) AS ( SELECT rowid,substr(dc1,1,4)||'-'||substr(dc1,6,2)||'-'||substr(dc1,9) FROM cte1)
UPDATE convdt SET data = (SELECT dc2 FROM cte2 WHERE cte2.rowid = rowid);
SELECT * FROM convdt;
This uses two Common Table Expressions.
The first cte1 extracts the rowid and the data columns replace Jan with 01, Feb with 02 and so on (naming the data column as dc1)
The second cte2 then extracts the rowid and the dc1 column from cte1 reformatting to adhere to YYYY-MM-DD hh:mm:ss format.
All rows of the convdt table are then updated, obtaining the replacement value from cte2 according to the rowid.
Working Example/Test Code
DROP TABLE IF EXISTS convdt; -- For testing only drop the table
CREATE TABLE IF NOT EXISTS convdt (data TEXT); -- For testing only create the table
INSERT INTO convdt VALUES -- For testing only populate the table
('2018 Jan 16 15:53:01'),('2018 Feb 16 15:53:01'),('2018 Mar 16 15:53:01'),('2018 Apr 16 15:53:01'),
('2018 May 16 15:53:01'),('2018 Jun 16 15:53:01'),('2018 Jul 16 15:53:01'),('2018 Aug 16 15:53:01'),
('2018 Sep 16 15:53:01'),('2018 Oct 16 15:53:01'),('2018 Nov 16 15:53:01'),('2018 Dec 16 15:53:01')
;
SELECT * FROM convdt; -- Extract all the data for comparison before the updates
WITH
cte1(rowid,dc1) AS (
SELECT
rowid,
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(data,'Jan','01'),
'Feb','02'),
'Mar','03'),
'Apr','04'),
'May','05'),
'Jun','06'),
'Jul','07'),
'Aug','09'),
'Sep','09'),
'Oct','10'),
'Nov','11'),
'Dec','12')
FROM convdt
),
cte2(rowid,dc2) AS ( SELECT rowid,substr(dc1,1,4)||'-'||substr(dc1,6,2)||'-'||substr(dc1,9) FROM cte1)
UPDATE convdt SET data = (SELECT dc2 FROM cte2 WHERE cte2.rowid = rowid);
SELECT * FROM convdt; -- Extract all the data to show the result of the conversion
Results
Before the table is updated :-
After the conversion :-
add a comment |
Assuming :-
- that the table is named convdt, and
- that you wish to change(UPDATE) all the rows in the table itself, and
- that the table is not defined using the WITHOUT ROWID keywords
then I believe that the following will do that :-
WITH
cte1(rowid,dc1) AS (
SELECT
rowid,
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(data,'Jan','01'),
'Feb','02'),
'Mar','03'),
'Apr','04'),
'May','05'),
'Jun','06'),
'Jul','07'),
'Aug','09'),
'Sep','09'),
'Oct','10'),
'Nov','11'),
'Dec','12')
FROM convdt
),
cte2(rowid,dc2) AS ( SELECT rowid,substr(dc1,1,4)||'-'||substr(dc1,6,2)||'-'||substr(dc1,9) FROM cte1)
UPDATE convdt SET data = (SELECT dc2 FROM cte2 WHERE cte2.rowid = rowid);
SELECT * FROM convdt;
This uses two Common Table Expressions.
The first cte1 extracts the rowid and the data columns replace Jan with 01, Feb with 02 and so on (naming the data column as dc1)
The second cte2 then extracts the rowid and the dc1 column from cte1 reformatting to adhere to YYYY-MM-DD hh:mm:ss format.
All rows of the convdt table are then updated, obtaining the replacement value from cte2 according to the rowid.
Working Example/Test Code
DROP TABLE IF EXISTS convdt; -- For testing only drop the table
CREATE TABLE IF NOT EXISTS convdt (data TEXT); -- For testing only create the table
INSERT INTO convdt VALUES -- For testing only populate the table
('2018 Jan 16 15:53:01'),('2018 Feb 16 15:53:01'),('2018 Mar 16 15:53:01'),('2018 Apr 16 15:53:01'),
('2018 May 16 15:53:01'),('2018 Jun 16 15:53:01'),('2018 Jul 16 15:53:01'),('2018 Aug 16 15:53:01'),
('2018 Sep 16 15:53:01'),('2018 Oct 16 15:53:01'),('2018 Nov 16 15:53:01'),('2018 Dec 16 15:53:01')
;
SELECT * FROM convdt; -- Extract all the data for comparison before the updates
WITH
cte1(rowid,dc1) AS (
SELECT
rowid,
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(data,'Jan','01'),
'Feb','02'),
'Mar','03'),
'Apr','04'),
'May','05'),
'Jun','06'),
'Jul','07'),
'Aug','09'),
'Sep','09'),
'Oct','10'),
'Nov','11'),
'Dec','12')
FROM convdt
),
cte2(rowid,dc2) AS ( SELECT rowid,substr(dc1,1,4)||'-'||substr(dc1,6,2)||'-'||substr(dc1,9) FROM cte1)
UPDATE convdt SET data = (SELECT dc2 FROM cte2 WHERE cte2.rowid = rowid);
SELECT * FROM convdt; -- Extract all the data to show the result of the conversion
Results
Before the table is updated :-
After the conversion :-
add a comment |
Assuming :-
- that the table is named convdt, and
- that you wish to change(UPDATE) all the rows in the table itself, and
- that the table is not defined using the WITHOUT ROWID keywords
then I believe that the following will do that :-
WITH
cte1(rowid,dc1) AS (
SELECT
rowid,
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(data,'Jan','01'),
'Feb','02'),
'Mar','03'),
'Apr','04'),
'May','05'),
'Jun','06'),
'Jul','07'),
'Aug','09'),
'Sep','09'),
'Oct','10'),
'Nov','11'),
'Dec','12')
FROM convdt
),
cte2(rowid,dc2) AS ( SELECT rowid,substr(dc1,1,4)||'-'||substr(dc1,6,2)||'-'||substr(dc1,9) FROM cte1)
UPDATE convdt SET data = (SELECT dc2 FROM cte2 WHERE cte2.rowid = rowid);
SELECT * FROM convdt;
This uses two Common Table Expressions.
The first cte1 extracts the rowid and the data columns replace Jan with 01, Feb with 02 and so on (naming the data column as dc1)
The second cte2 then extracts the rowid and the dc1 column from cte1 reformatting to adhere to YYYY-MM-DD hh:mm:ss format.
All rows of the convdt table are then updated, obtaining the replacement value from cte2 according to the rowid.
Working Example/Test Code
DROP TABLE IF EXISTS convdt; -- For testing only drop the table
CREATE TABLE IF NOT EXISTS convdt (data TEXT); -- For testing only create the table
INSERT INTO convdt VALUES -- For testing only populate the table
('2018 Jan 16 15:53:01'),('2018 Feb 16 15:53:01'),('2018 Mar 16 15:53:01'),('2018 Apr 16 15:53:01'),
('2018 May 16 15:53:01'),('2018 Jun 16 15:53:01'),('2018 Jul 16 15:53:01'),('2018 Aug 16 15:53:01'),
('2018 Sep 16 15:53:01'),('2018 Oct 16 15:53:01'),('2018 Nov 16 15:53:01'),('2018 Dec 16 15:53:01')
;
SELECT * FROM convdt; -- Extract all the data for comparison before the updates
WITH
cte1(rowid,dc1) AS (
SELECT
rowid,
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(data,'Jan','01'),
'Feb','02'),
'Mar','03'),
'Apr','04'),
'May','05'),
'Jun','06'),
'Jul','07'),
'Aug','09'),
'Sep','09'),
'Oct','10'),
'Nov','11'),
'Dec','12')
FROM convdt
),
cte2(rowid,dc2) AS ( SELECT rowid,substr(dc1,1,4)||'-'||substr(dc1,6,2)||'-'||substr(dc1,9) FROM cte1)
UPDATE convdt SET data = (SELECT dc2 FROM cte2 WHERE cte2.rowid = rowid);
SELECT * FROM convdt; -- Extract all the data to show the result of the conversion
Results
Before the table is updated :-
After the conversion :-
Assuming :-
- that the table is named convdt, and
- that you wish to change(UPDATE) all the rows in the table itself, and
- that the table is not defined using the WITHOUT ROWID keywords
then I believe that the following will do that :-
WITH
cte1(rowid,dc1) AS (
SELECT
rowid,
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(data,'Jan','01'),
'Feb','02'),
'Mar','03'),
'Apr','04'),
'May','05'),
'Jun','06'),
'Jul','07'),
'Aug','09'),
'Sep','09'),
'Oct','10'),
'Nov','11'),
'Dec','12')
FROM convdt
),
cte2(rowid,dc2) AS ( SELECT rowid,substr(dc1,1,4)||'-'||substr(dc1,6,2)||'-'||substr(dc1,9) FROM cte1)
UPDATE convdt SET data = (SELECT dc2 FROM cte2 WHERE cte2.rowid = rowid);
SELECT * FROM convdt;
This uses two Common Table Expressions.
The first cte1 extracts the rowid and the data columns replace Jan with 01, Feb with 02 and so on (naming the data column as dc1)
The second cte2 then extracts the rowid and the dc1 column from cte1 reformatting to adhere to YYYY-MM-DD hh:mm:ss format.
All rows of the convdt table are then updated, obtaining the replacement value from cte2 according to the rowid.
Working Example/Test Code
DROP TABLE IF EXISTS convdt; -- For testing only drop the table
CREATE TABLE IF NOT EXISTS convdt (data TEXT); -- For testing only create the table
INSERT INTO convdt VALUES -- For testing only populate the table
('2018 Jan 16 15:53:01'),('2018 Feb 16 15:53:01'),('2018 Mar 16 15:53:01'),('2018 Apr 16 15:53:01'),
('2018 May 16 15:53:01'),('2018 Jun 16 15:53:01'),('2018 Jul 16 15:53:01'),('2018 Aug 16 15:53:01'),
('2018 Sep 16 15:53:01'),('2018 Oct 16 15:53:01'),('2018 Nov 16 15:53:01'),('2018 Dec 16 15:53:01')
;
SELECT * FROM convdt; -- Extract all the data for comparison before the updates
WITH
cte1(rowid,dc1) AS (
SELECT
rowid,
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(data,'Jan','01'),
'Feb','02'),
'Mar','03'),
'Apr','04'),
'May','05'),
'Jun','06'),
'Jul','07'),
'Aug','09'),
'Sep','09'),
'Oct','10'),
'Nov','11'),
'Dec','12')
FROM convdt
),
cte2(rowid,dc2) AS ( SELECT rowid,substr(dc1,1,4)||'-'||substr(dc1,6,2)||'-'||substr(dc1,9) FROM cte1)
UPDATE convdt SET data = (SELECT dc2 FROM cte2 WHERE cte2.rowid = rowid);
SELECT * FROM convdt; -- Extract all the data to show the result of the conversion
Results
Before the table is updated :-
After the conversion :-
answered Nov 22 '18 at 10:45
MikeTMikeT
16.8k112642
16.8k112642
add a comment |
add a comment |
This will do the update, simply by changing the MMM
month part with a double digit number.
The last CASE
statement checks if the day number is only 1 digit and if so it pads it left with 1 0
.
UPDATE test SET datecol =
substr(datecol, 1, 4) || '-' ||
CASE substr(datecol, 6, 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'
ELSE substr(datecol, 6, 3)
END || '-' ||
CASE substr(datecol, 11, 1)
WHEN ' ' THEN '0' || substr(datecol, 10)
ELSE substr(datecol, 10)
END
add a comment |
This will do the update, simply by changing the MMM
month part with a double digit number.
The last CASE
statement checks if the day number is only 1 digit and if so it pads it left with 1 0
.
UPDATE test SET datecol =
substr(datecol, 1, 4) || '-' ||
CASE substr(datecol, 6, 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'
ELSE substr(datecol, 6, 3)
END || '-' ||
CASE substr(datecol, 11, 1)
WHEN ' ' THEN '0' || substr(datecol, 10)
ELSE substr(datecol, 10)
END
add a comment |
This will do the update, simply by changing the MMM
month part with a double digit number.
The last CASE
statement checks if the day number is only 1 digit and if so it pads it left with 1 0
.
UPDATE test SET datecol =
substr(datecol, 1, 4) || '-' ||
CASE substr(datecol, 6, 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'
ELSE substr(datecol, 6, 3)
END || '-' ||
CASE substr(datecol, 11, 1)
WHEN ' ' THEN '0' || substr(datecol, 10)
ELSE substr(datecol, 10)
END
This will do the update, simply by changing the MMM
month part with a double digit number.
The last CASE
statement checks if the day number is only 1 digit and if so it pads it left with 1 0
.
UPDATE test SET datecol =
substr(datecol, 1, 4) || '-' ||
CASE substr(datecol, 6, 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'
ELSE substr(datecol, 6, 3)
END || '-' ||
CASE substr(datecol, 11, 1)
WHEN ' ' THEN '0' || substr(datecol, 10)
ELSE substr(datecol, 10)
END
answered Nov 22 '18 at 11:30
forpasforpas
14.8k3625
14.8k3625
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426932%2fconvert-daytime-in-string-to-datetime-in-sqllite-3%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
AFAIK, there is no way to convert a date string with
Nov
, even usingstrftime
. You should always store your dates in SQLite using an ISO format which will sort properly. So, you should have stored2018-11-16 15:53:01
.– Tim Biegeleisen
Nov 22 '18 at 8:53
You could do it with
substr()
and a bigCASE
expression to turn the month name into a number, but, yeah, it's better to store your timestamp in one of the formats understood by sqlite date & time functions. This also has extra benefits like being able to sort them.– Shawn
Nov 22 '18 at 9:12
You could use a scripting language like Python to query the values, parse them as datetimes and write back to database. Syntax would be
dt = datetime.strptime(s, '%Y %b %d %H:%M:%S')
– SuperShoot
Nov 22 '18 at 9:18