Reputation: 153
I am trying cast two datas. I want the cast data within a bracket. I have tried like this:
SELECT EmpCode,
(CAST(Name AS varchar(50))+' '+CAST(EmpCode AS varchar(50))) AS Name
from ShiftAllocation
It gives output with Name and EmpCode like
Tom Varghees 12345
But I want this 12345 (EmpCode) in a bracket. Expected output is
Tom Varghees [12345]
What changes I should do in my sample querym
Upvotes: 1
Views: 179
Reputation: 9063
You can use QUOTENAME.
SELECT EmpCode,
(CAST(Name AS VARCHAR(50)) + ' ' +
QUOTENAME(CAST(EmpCode AS VARCHAR(50)))) AS Name
FROM ShiftAllocation
If you don't have ability to use QUOTENAME
you can go in following:
SELECT EmpCode,
(CAST(Name AS VARCHAR(50)) +
' [' + CAST(EmpCode AS VARCHAR(50)) + ']') AS Name
FROM ShiftAllocation
Upvotes: 2
Reputation: 62488
Simply add parenthesis, no rocket science needed:
SELECT 'Peter'+' ['+ '12345'+']' AS Name
in your query:
SELECT EmpCode,(CAST(Name AS varchar(50))+' ['+CAST(EmpCode AS varchar(50)) +']') AS Name from ShiftAllocation
Upvotes: 1