简体   繁体   中英

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 .

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

DECLARE @limit datetime2;
SET @limit = CONCAT(CONVERT (date, GETDATE()) , ' 05:00:00')
SELECT @limit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM