Reputation: 383
I currently have a Storedproc t hat is returning values for me to bind with a gridview in asp.net. One thing I want to do is combine two fields into one seperated by a / but I cannot seem to get this working.
Here is my stored proc
DECLARE @SQL VARCHAR(MAX);
BEGIN
SET @SQL = N'SELECT SYSTEM AS ''System''
, ENTN AS ''Entity''
, MED_CTR AS ''Medical Center''
, MED_CTR_LEVEL AS ''Medical Center Level''
, DEPT_ID AS ''Department ID''
FROM V_ROLLUP_SYSTEM_DESC_REV1
WHERE (DEPT_ID = '+CONVERT(VARCHAR,@DEPTCODE)+')
AND SYSTEM IN ('+@SYS+')'
PRINT(@SQL)
EXEC(@SQL)
So I want to combine MED_CTR with MED_CTR_LEVEL seperated by a "/" and labeled as Medical Center/ Level. I cant seem to combine it thus far since every method I have tried to add a "/" in it has returned me nothing.
Upvotes: 0
Views: 90
Reputation: 63964
This should work:
SET @SQL = N'SELECT SYSTEM AS [System]
, ENTN AS [Entity]
, (MED_CTR +''/''+ MED_CTR_LEVEL) as [Medical Center/ Level]
, DEPT_ID AS [Department ID]
FROM V_ROLLUP_SYSTEM_DESC_REV1
WHERE (DEPT_ID = '+CONVERT(VARCHAR,@DEPTCODE)+')
AND SYSTEM IN ('+@SYS+')'
And if you want to make sure the concat of the 2 columns always succeeds, you can do this:
SET @SQL = N'SELECT SYSTEM AS [System]
, ENTN AS [Entity]
, ISNULL(MED_CTR,''N/A'') +''/''+ ISNULL(MED_CTR_LEVEL,''N/A'') ) as [Medical Center/ Level]
, DEPT_ID AS [Department ID]
FROM V_ROLLUP_SYSTEM_DESC_REV1
WHERE (DEPT_ID = '+CONVERT(VARCHAR,@DEPTCODE)+')
AND SYSTEM IN ('+@SYS+')'
Upvotes: 1