Labels

Wednesday, April 3, 2013

CONCAT

/*
CONCAT function can be used to concatenate two or more strings.
This function is available before in other databases like Oracle,MySQL etc.
This feature will offer below advantages over standard concatenation operator(+)

    If any of the string is null, standard concatenation operator will return null as output.
    However, CONCAT() function will ignore null values while concatenating.
    As CONCAT() function is already available in other databases such as Oracle and MySql, while porting from one DB to other,
    this will gives more flexibility compared to standard operator.
*/


DECLARE
@firstname VARCHAR(20) = 'Pardha Saradhi',
@middlename VARCHAR(10) = NULL,
@lastname VARCHAR(20) = 'V'

-- Without CONCAT function
SELECT @lastname + ' ' + @middlename + '' + @firstname


-- Using CONCAT function
SELECT CONCAT(@lastname, ' ', @middlename, ' ', @firstname)

No comments:

Post a Comment