Conversion failed when converting date and/or time from character string in sql server -


i have function convert string datetime (101) format. gives me error when convert value.

2016-03-01 00:00:00.0000000 

and error

msg 241, level 16, state 1, line 2 conversion failed when converting date and/or time character string. 

and function ..

alter function [dbo].[converttodate]  (     @value nvarchar(max) ) returns datetime begin     if (@value <> null or @value <> '')         begin             declare @dt datetime             set @dt = convert(datetime,@value,101)             --set @dt = cast(@value datetime(101))             return @dt         end          return null  end 

what problem?

too many zeros in millisecond part. works fine

declare @value nvarchar(max)='2016-03-01 00:00:00.000' select convert(datetime,@value,101) 

you can try using left this

declare @value nvarchar(max)='2016-03-01 00:00:00.000000' select convert(datetime,left(@value,23),101) 

Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -