This has been answered by a lot of people, but I feel like the simplest solution has been left out.
SQL SERVER (I believe its 2012+) has implicit string equivalents for DATETIME2 as shown here
Look at the section on "Supported string literal formats for datetime2"
To answer the OPs question explicitly:
DECLARE @myVar NCHAR(32)
DECLARE @myDt DATETIME2
SELECT @myVar = @GETDATE()
SELECT @myDt = @myVar
PRINT(@myVar)
PRINT(@myDt)
output:
Jan 23 2019 12:24PM
2019-01-23 12:24:00.0000000
Note:
The first variable (myVar
) is actually holding the value '2019-01-23 12:24:00.0000000'
as well. It just gets formatted to Jan 23 2019 12:24PM
due to default formatting set for SQL SERVER that gets called on when you use PRINT
. Don't get tripped up here by that, the actual string in (myVer)
= '2019-01-23 12:24:00.0000000'