SQL Server: How to concatenate string constant with date? SQL Server: How to concatenate string constant with date? sql sql

SQL Server: How to concatenate string constant with date?


You need to convert UpdatedOn to varchar something like this:

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

You might also need to use CONVERT if you want to format the datetime in a specific format.


To achive what you need, you would need to CAST the Date?

Example would be;

Your current, incorrect code:

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

Suggested solution:

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

MSDN article for CAST / CONVERT

Hope this helps.


To account for null values, try this:

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