我将日期时间值存储在TEXT列的SqlLite3中。数据存储为protected void SaveButton_Click(object sender, EventArgs e)
{
List<UserInfo> allUsersList = Application["AllUsersList"] as List<UserInfo>;
UserInfo newUser = new UserInfo();
newUser.FirstName += firstName.Text;
newUser.MiddleInitials = middleInitials.Text;
newUser.LastName = lastName.Text;
newUser.EmailAddress1 = emailAddress1.Text;
newUser.EmailAddress2 = emailAddress2.Text;
newUser.StreetAddress = streetAddress.Text;
newUser.AptNo = aptno.Text;
newUser.City = city.Text;
newUser.State = state.Text;
newUser.Zip = zip.Text;
newUser.HomePhone = phone.Text;
newUser.JobExperience = jobExperience.Text;
newUser.Password = password.Text;
allUsersList.Add(newUser);
Application["AllUsersList"] = allUsersList;
}
。如何将其转换回日期时间?
答案 0 :(得分:0)
假设:-
那我相信以下方法可以做到:-
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;
这使用两个通用表表达式。
第一个 cte1 提取 rowid ,并且 data 列将Jan替换为01,Feb替换为02,依此类推(命名数据列为 dc1 )
第二个 cte2 然后从cte1重新格式化中提取 rowid 和 dc1 列,以遵守YYYY-MM-DD hh:mm: ss格式。
convdt 表中的所有行均会更新,并根据 rowid 从cte2获取替换值。
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
答案 1 :(得分:0)
这将进行更新,只需将MMM
月部分更改为两位数即可。
最后一个CASE
语句检查日期数字是否只有1位数字,如果是,则将其填充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