SQL Server: how to concatenate a string constant with a date?

select packageid,status+' Date : '+UpdatedOn from [Shipment_Package] 

The following is an error while executing the above code on sql server. Type UpdatedOn- DateTime, and status - varchar. We wanted to link status, Date and UpdateOn.

Mistake:

Conversion error while converting date and / or time from character string.

+4
source share
3 answers

You need to convert UpdatedOnto varcharsomething like this:

select packageid, status + ' Date : ' + CAST(UpdatedOn AS VARCHAR(10))
from [Shipment_Package];

You may also need to use CONVERTit if you want to format the date-time in a specific format.

+8
source

To accomplish what you need, do you need to execute CAST Date?

Example:

:

select packageid,status+' Date : '+UpdatedOn from [Shipment_Package] 

:

select packageid,status + ' Date : ' + CAST(UpdatedOn AS VARCHAR(20))
from [Shipment_Package] 

MSDN CAST/CONVERT

, .

+1

, :

select packageid, 
    'Status: ' + isnull(status, '(null)') + ', Date : ' 
    + case when UpdatedOn is null then '(null)' else convert(varchar(20), UpdatedOn, 104) end as status_text
from [Shipment_Package]
+1
source

All Articles