Amid Gol
Amid Gol

Reputation: 325

Error in Converting varchar to datetime

I am trying to convert varchar to datetime, everything is ok when I use the convert function like this:

SELECT CONVERT(Datetime, '2016-01-01 00:00:00.000', 120)

but when I try to declare a varchar parameter and pass it to the function, I encounter the error

declare @input varchar = '2016-01-01 00:00:00.000'
SELECT CONVERT(Datetime, @input, 120)

Any Help will be appreciated

Upvotes: 2

Views: 126

Answers (1)

Abdul Rasheed
Abdul Rasheed

Reputation: 6729

Please specify the size while using a varchar.

varchar = '2016-01-01 00:00:00.000' is '2' --SELECT @input

use

declare @input varchar(30) = '2016-01-01 00:00:00.000'
SELECT CONVERT(Datetime, @input, 120)

Upvotes: 5

Related Questions