Ratha
Ratha

Reputation: 9692

How to convert string to Date in T-SQL?

I have defined my update query as follows to update a Date column.

declare @fromDate date;
set @fromDate = '2019-04-31'

update [dbo].[sites] 
  set [FromDate]= @fromDate 
where siteid=2832

Above gives following error:

Conversion failed when converting date and/or time from character string.

What Im doing wrong here?

Upvotes: 0

Views: 66

Answers (2)

Raka
Raka

Reputation: 427

April doesn't have 31 days, if you pass invalid date you will get error. Try with some valid date like 2019-04-30

Upvotes: 3

someRandomSerbianGuy
someRandomSerbianGuy

Reputation: 491

If it will work is most likely dependent on what settings you run on your server for dates. You can use CONVERT(datetime, '2019-04-31') this will convert any string to datetime type that your server supports.

Upvotes: 0

Related Questions