sql-convert

How to convert DateTime to VarChar

若如初见. 提交于 2019-11-26 01:44:05
问题 I am working on a query in Sql Server 2005 where I need to convert a value in DateTime variable into a varchar variable in yyyy-mm-dd format (without time part). How do I do that? 回答1: With Microsoft Sql Server: -- -- Create test case -- DECLARE @myDateTime DATETIME SET @myDateTime = '2008-05-03' -- -- Convert string -- SELECT LEFT(CONVERT(VARCHAR, @myDateTime, 120), 10) 回答2: Here's some test sql for all the styles. DECLARE @now datetime SET @now = GETDATE() select convert(nvarchar(MAX), @now