Search This Blog

Monday, December 2, 2013

Add operator SQL Server Example

SQL Server > Operators > Add

+ Operator adds two numbers.





Example

1. Add values from 2 numeric columns

create table #test(value int, fee int)
insert into #test(value, fee) values (500,50),(100,10)
  select
   value + fee as 'Total'
  from
   #test
drop table #test

Result:
 Total
   550
   110


2. Add values from 1 numeric value and 1 char value

DECLARE @int_value int = 100
DECLARE @str_value varchar(10) = '10'

SELECT @str_value + @int_value

Result:
(No column name)
110


3. Using + operator to concatenate string

DECLARE @str1 varchar(10) = 'Microsoft'
DECLARE @str2 varchar(10) = 'SQL Server'

SELECT @str1 + ' ' + @str2

Result:
(No column name)
Microsoft SQL Server