We help IT Professionals succeed at work.

Display date and Desc as varchar field

BobRosas
BobRosas asked
on
I'm in SQL Server and I want to update a varchar field with today's year and a desc.  Ex:
2012 Training
I've tried DATEPART(yy, { fn NOW() }) & ' Training'
but I get the error...
Conversion failed when converting the varchar value ' Training' to data type int.
How do I do this?
Comment
Watch Question

SQL Server Data Dude
BRONZE EXPERT
Most Valuable Expert 2013
Author of the Year 2015
Commented:
SELECT CAST(YEAR(GETDATE()) as char(4)) + ' Training'

Author

Commented:
Thank you for your quick response!  It worked like a charm!

Author

Commented:
Excellent!
Jim HornSQL Server Data Dude
BRONZE EXPERT
Most Valuable Expert 2013
Author of the Year 2015

Commented:
Couple of things SQL-specific:
(1)  YEAR returns an integer, so to concatenate it with a string you have to CAST it as a string.
      So does DATEPART, which is why you received the error.
(2)  SQL uses plus marks ( + ) for string concatenation.
(3)  Did you want two-digit years (yy) or four (2012)?

Author

Commented:
The four digit year was just what I needed.
Thank you so much for the explanation as well.  I really appreciate it.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.