Friday, 18 May 2018

SQLTips : CONCAT

The CONCAT function is available from SQL 2016+. It can save you a lot of messing around with adding strings together and coping with NULLS and empty strings.
SELECT CONCAT('The cat sat',' ', 'on', ' ', 'the mat')
GO
 

SELECT CONCAT(NULL, ' and void')
GO

 
DECLARE @int int = 99
DECLARE @varchar varchar(13) = ' red balloons'
SELECT CONCAT(@int,@varchar)
GO

 
DECLARE @varchar varchar(20) = 'The date today is '
DECLARE @dt datetime = GETDATE()
SELECT CONCAT(@varchar,@dt)
GO

No comments: