Sunday, February 17, 2013

SQL SERVER CONCAT string function

SQL SERVER CONCAT string function

CONCAT String function concatenates 2 or more Strings into Single String

  If type is not string it will be converted to String type before concatinating.

Ex:Display Full Name with title from Employees table.

select concat(TitleofCourtesy,FirstName,' ',LastName) from Employees

Full Name with title
Ms.Nancy Davolio
Dr.Andrew Fuller
Ms.Janet Leverling
Mrs.Margaret Peacock
.
.
.

Ex:Concat 2 numbers

select concat(123,433)
O/P: 123433

Ex:Contact 2 numeric Expressions 


select concat(123-455,433-30)

O/P: -332403
 first 123-455  = -332
 second 433-30=403
contact(-332,403) gives above value.

Ex: Concat 2 integer expressions

select concat(2012-03-22,2013-03-22)

O/P: 19871988
first 2012-03  gives 2009  2009-22=1987
 same thing for second expression. Because all types are evaluated before converting to String.


Ex:Concat NameValue Pairs in T-SQL

insert into properties(nv) values (concat('domain name=','www.kicko.com')) 
insert into properties(nv) values (concat('car type'=','suv'))  


Ex:Concat unicode  strings using Space String function in

select concat(N'Ich',space(1),'komme',space(1),'aus',space(1),'Deutschland')

O/P:
german
Ich komme aus Deutschland

 Ex:Concat strings using Space String function


select concat(N'I',space(1),'am',space(1),'from',space(1),'germany') english

O/P: I am from germany


Tags: SQL Server Concat String function,SQL Server Space String function,T-SQL concat,Concat 2 numeric literals in SQL Server,Concat Unicode chars in SQL Server,Concat Ascii chars in SQL Server

No comments:

Post a Comment