1

I am trying to display a combination of String MM + int DD + int YYYY in SQL Management Studio. However, i encountered an error like this which says

Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the nvarchar value 'November ' to data type int.

My codes are:

SELECT DATENAME(MM, Check_in_date) + ' ' + DAY(Check_in_date) + ' ' +
YEAR(Check_in_date)     
FROM Book_Details

Afterwards, i tried casting the Check_in_date into a varchar but encountered an error as well. Any help will be appreciated

1 Answers1

2

You are trying to concatenate a string and an integer. You need to cast @ID as a string. try:

SELECT DATENAME(MM, Check_in_date) + ' ' + CAST(DAY(Check_in_date) AS NVARCHAR(2)) + ' ' +
CAST(YEAR(Check_in_date)  AS NVARCHAR(4))     
FROM Book_Details

if you don't like the space, you may use LTRIM to remove the space

Refer : link


UPDATED on year part

Community
  • 1
  • 1
ah_hau
  • 768
  • 4
  • 11