How to concatenate a string and GETDATE() in MSSQL

Kjartan picture Kjartan · Aug 9, 2011 · Viewed 37.7k times · Source

I need to insert a string (a comment) which should include a date. What I need is basically the following simple operation:

INSERT INTO [Table_1]
           ([textColumn])
     VALUES
           ('Date: ' + GETDATE())
GO

This however, returns the following error: Conversion failed when converting date and/or time from character string.

Any quick fixes?

Answer

Damith picture Damith · Aug 9, 2011

what is the date time format you need?

select one from here http://www.sql-server-helper.com/tips/date-formats.aspx and convert it to a char as bellow

INSERT INTO [Table_1]
           ([textColumn])
     VALUES
           ('Date: ' +CONVERT(CHAR(10),  GETDATE(), 120))
GO