[英]How to convert varchar to date in SQL Server
I would like to have a datetime variable composed of the current date and a time that I would have defined.我想要一个由当前日期和我定义的时间组成的日期时间变量。
I tried this:我试过这个:
DECLARE @limit datetime2;
SET @limit = CONVERT (date, GETDATE()) + ' 05:00:00'
But this way doesn't work well because I can't add a varchar
to a date
.但是这种方式效果不佳,因为我无法将
varchar
添加到date
。
You can try:你可以试试:
DECLARE @limit datetime2;
SET @limit = DATEADD(day, DATEDIFF(day, 0, GETDATE()), '05:00:00')
select @limit
You can also achieve by using CONCAT
in SQL您也可以通过在 SQL 中使用
CONCAT
来实现
DECLARE @limit datetime2;
SET @limit = CONCAT(CONVERT (date, GETDATE()) , ' 05:00:00')
SELECT @limit
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.