Christian Vibora
Christian Vibora

Reputation: 113

SQL concatenations

Say I have

SELECT [id] as [Customer's ID] FROM [users];

How how can I put a string in front of [id] value?

I've tried

SELECT + "CTMR-ID-" + [id] as [Customer's ID] 

but it don't work.

Upvotes: 2

Views: 52

Answers (2)

ThisIsAjanthan
ThisIsAjanthan

Reputation: 1

Alternatively you can use CONCAT function if you are using SQL Server (starting with 2008).

SELECT CONCAT ( 'CTMR-ID-', id) AS [Customer's ID] 
FROM [users];

Upvotes: -1

Pரதீப்
Pரதீப்

Reputation: 93754

You have a unnecessary + after SELECT keyword

Try this way, Given ID is of Varchar type else you may have to cast the ID explicitly to Varchar

SELECT  "CTMR-ID-" & [id] as [Customer's ID] 

& will avoid NULL values. Even when ID is NULL you will get CTMR-ID- in result

SELECT  "CTMR-ID-" + [id] as [Customer's ID] 

+ will consider NULL values. When ID is NULL the result of concatenation will be NULL

Upvotes: 4

Related Questions