How to convert DateTime to VarChar SQL 1

How to convert DateTime to VarChar SQL

-- Create test case
DECLARE @myDateTime DATETIME
SET @myDateTime = '2008-05-03'
-- Convert string
SELECT LEFT(CONVERT(VARCHAR, @myDateTime, 120), 10)

Here is what the above code is Doing:
1. Declare a variable of type DATETIME
2. Set the variable to a date
3. Convert the variable to a string
4. Take the first 10 characters of the string

The result is:
2008-05-03

Similar Posts