Convert string to datetime – SQL Server

Easiest way to convert the string into datetime, without worrying about the language settings in sql server is to use ’1-Jan-2010′ pattern.

DECLARE @Day INT = 2
DECLARE @Month INT = 11
DECLARE @Year INT = 2010
SELECT
	CONVERT(DATETIME, CONVERT(varchar,@Day) + '-' +
             LEFT(DATENAME(MONTH, DATEADD(MONTH,@Month,0) - 1),3)
	+ '-' + CONVERT(varchar,@Year))
Tags: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*