How to convert text column to datetime in SQL(如何在 SQL 中将文本列转换为日期时间)
问题描述
我有一列是文本:
Remarks (text, null)
样本值为
"5/21/2013 9:45:48 AM"
如何将其转换为这样的日期时间格式:
How do I convert it to a datetime format like this:
"2013-05-21 09:45:48.000"
转换的原因是我试图获取日期时间列和备注列中的日期戳之间的总小时数.我在想这样的事情:
The reason for the conversion is that I was trying to get the total number of hours between a datetime column and the date stamp in the Remarks column. I was thinking of something like this:
Remarks (text, null) - Date_Sent (datetime, null)
为了清楚起见,这些列代表客户发送查询的日期时间 (Date_Sent
) 和代表对查询做出的最后响应 (Response
),因此对于具有 2013-05-21 08:00:00.000"
和 Response
值的 Date_Sent
样本如果值为 "5/21/2013 10:00:00 AM"
,我应该得到 2.00
(2 小时)的值.不幸的是,在我正在处理的数据库中,Remarks
是一个文本,Date_Sent
是一个日期时间.
To be clear, the columns represent the datetime an inquiry by a customer was sent (Date_Sent
) and the last response made by a representative regarding the inquiry (Response
), so for a sample of a Date_Sent
having a value of "2013-05-21 08:00:00.000"
and a Response
with a value of "5/21/2013 10:00:00 AM"
, I should get a value of 2.00
(2 hours). Unfortunately, in the database I'm working on, Remarks
is a text and Date_Sent
is a datetime.
推荐答案
使用 convert 样式为 101.
Use convert with style 101.
select convert(datetime, Remarks, 101)
如果你的列真的是text
,你需要在转换为datetime之前先转换为varchar
If your column is really text
you need to convert to varchar before converting to datetime
select convert(datetime, convert(varchar(30), Remarks), 101)
这篇关于如何在 SQL 中将文本列转换为日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 SQL 中将文本列转换为日期时间


基础教程推荐
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- 带更新的 sqlite CTE 2022-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01