CamilleChlc
CamilleChlc

Reputation: 91

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.

Upvotes: 0

Views: 98

Answers (2)

Thiyagu
Thiyagu

Reputation: 1340

You can also achieve by using CONCAT in SQL

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

Upvotes: 1

Hong Van Vit
Hong Van Vit

Reputation: 2986

You can try:

DECLARE @limit datetime2;
SET @limit = DATEADD(day, DATEDIFF(day, 0, GETDATE()), '05:00:00')
select  @limit

Upvotes: 4

Related Questions