[英]Compare the date if the columns (day, month and year) are stored separately in SQL Server
[英]Concatenate and convert data stored separately as date and time and compare to another piece of data stored similarly in SQL Server
我正在使用 SQL 服务器。 我在一个表中有两列。 日期列将日期存储为varchar
。 存储在日期列中的示例数据是2006-05-29 00:00:00.000
。 时间列也存储为varchar
。 存储在时间列中的样本数据是02:00 PM
。 我必须连接数据和时间,并以类似的格式与另一个日期和时间进行比较。
本质上,我必须连接2006-05-29 00:00:00.000
和02:00 PM
,然后连接另一条数据2006-05-29 00:00:00.000
和08:00 PM
并比较它们。
将日期/时间值存储为字符串不是 go 的方法。 SQL 服务器(和其他数据库)具有日期/时间值的内置类型。 使用它们。 他们在那里是有原因的。
也就是说,您的字符串很容易转换为日期/时间值。 您可以添加两个datetime
。 所以你可以这样做:
select try_convert(datetime, datecol) + convert(datetime, try_convert(time, timecol))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.