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?
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
Depending on the column's definition, you can try to cast or convert the date to the desired type:
INSERT INTO [Table_1]
([textColumn])
VALUES
('Date: ' + CAST(GETDATE() as nvarchar(max)))
GO
To format the date, use Convert, e.g.
INSERT INTO [Table_1]
([textColumn])
VALUES
('Date: ' + convert(nvarchar(max), GETDATE(), 101))
GO
The last Parameter defines the format - see msdn for details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With