简体   繁体   English

如何在 SQL 中将文本列转换为日期列?

[英]How to convert Text Column to Date Column in SQL?

I was given a date column in the form of 'YYYY-MM-dd HH:mm:ss UTC'.我得到了一个格式为“YYYY-MM-dd HH:mm:ss UTC”的日期列。 I have tried using cast and convert to date, datetime, and timestamp, but they all only return the YYYY.我尝试使用强制转换并转换为日期、日期时间和时间戳,但它们都只返回 YYYY。 Is there any way to return the date correctly from this type of string?有没有办法从这种类型的字符串中正确返回日期? I also would like this NewDate column to be added to my table for future use.我还希望将此 NewDate 列添加到我的表中以备将来使用。

Below is the code I have tried下面是我试过的代码

select OrigDate, cast(OrigDate as timestamp) as NewDate from MyTable

SQLite has no special datatype for dates, which can be stored as text or numbers (julian days or epoch timestamp); SQLite 没有特殊的日期数据类型,可以存储为文本或数字(儒略日或纪元时间戳); instead, it has a set of date and time functions that operate on these values.相反,它有一组对这些值进行操作的日期和时间函数

SQLite recognizes 'YYYY-MM-DD HH:MM:SS' as a valid date format, so to make your strings valid SQLite date formats, you just need to remove the trailing " UTC": SQLite 将'YYYY-MM-DD HH:MM:SS'识别为有效的日期格式,因此要使您的字符串成为有效的 SQLite 日期格式,您只需删除尾随的“UTC”:

select OrigDate, substring(OrigDate, -4) NewDate  from mytable

You can add the new column with name NewDate to the table like this:您可以将名为NewDate的新列添加到表中,如下所示:

alter table MyTable add NewDate text;

As you can see the data type defined is text because there is no date or datetime data type in SQLite.正如您所看到的,定义的数据类型是text因为 SQLite 中没有datedatetime数据类型。
Then you can populate/update this column with the local time that each of your UTC datetimes already have in the column OrigDate with the use of the function datetime() :然后,您可以使用函数datetime()使用OrigDate列中每个UTC日期时间已经具有的本地时间填充/更新此列:

update MyTable
set NewDate = datetime(substr(OrigDate, 1, 19), 'localtime');

See a simplified demo .查看一个简化的演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL 文本列包含一个日期。 如何转换以从文本中获取实际日期? - SQL Text column holds a date. How to convert to get actual date from the text? 如何在SQL Server 2008中将month-yyyy文本列转换为日期列 - How to convert month-yyyy text column to date column in SQL Server 2008 如何在sql中将二进制日期列日期转换为公历日期格式 - How to convert a binary date column date into gregorian date format in sql 将 varchar 列转换为 SQL 中的日期 - Convert varchar column to date in SQL 从CSV导入到SQL Server时如何将日期列的文本数据类型转换为时间戳格式 - How to convert text datatype of date column to timestamp format when importing from CSV to SQL server 如何将现有表中的nvarchar列转换为日期? SQL服务器 - How convert nvarchar column from a existing table to date? SQL Server 如何将SQL中的表格列转换为日期类型并按升序排列? - How to convert table column to date type in SQL and arrange in ascending order? 如何在 SQL 服务器中将 nvarchar 日期从一列转换为另一列 - How to convert nvarchar to date from one column to another in SQL Server 如何在SQL中将DATE列转换为DATETIME,查询Ac​​cess数据库? - How to convert a DATE column to DATETIME in SQL, querying an Access database? 如何将同一列中所有项目的字符串转换为 SQL 中的日期? - How to convert string to date in SQL for all the items in the same column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM