String Function: Concat()

Concat():

It Concatenates two or more values. Return type is string.
Advantage of this is it can concatenate same or different data types.

Example:

Declare @ProdName as Varchar(20);
Declare @Price as Money;

set @ProdName='Book';
set @Price=20.00;

We can concatenate in two different way.

First Method:

select @ProdName+' costs' as Prods

OutPut:
Prods
Book costs

But try to add @price it will throw error because of different data types.

select @ProdName+' costs' +@Price as outputs

Msg 235, Level 16, State 0, Line 6
Cannot convert a char value to money. The char value has incorrect syntax.

Second Method:

Declare @ProdName as Varchar(20);
Declare @Price as Money;

set @ProdName='Book';
set @Price=20.00;

select CONCAT(@ProdName,' Costs ',@price) as Prods;

OutPut:
Prods
Book Costs 20.00
Share on Google Plus
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment