我在哪里放CASE T-SQL

时间:2014-04-14 14:54:59

标签: sql-server tsql case

我在下面有这个查询,但是我需要在那里放一个CASE,这将使'example,example'给出'exampleexample'的输出(减去逗号)。我会把它放在哪里? (我是SQL的新手)

SELECT DISTINCT i.ServerName, 'Standalone' AS Host, s.Status, s.Classification, 
                s.Location 
FROM tblSQLInstances i 
  LEFT OUTER JOIN tblServer s
  on i.ServerName = s.ServerName
WHERE i.IsClustered = 0
  AND i.InstanceStatus != 'Demised'

UNION

SELECT esr.ServerName, e.EnvironmentDescription AS Host, s.Status, s.Classification, 
       s.Location  
from tblSQLEnvironmentServerRel esr
  INNER JOIN tblSQLEnvironments e
  ON esr.EnvironmentID = e.EnvironmentID
LEFT OUTER JOIN tblServer s
  on esr.ServerName = s.ServerName
LEFT OUTER JOIN tblSQLInstances i
  on i.EnvironmentID = e.EnvironmentID
WHERE i.InstanceStatus != 'Demised'
抱歉,如果这没什么意义的话。

1 个答案:

答案 0 :(得分:1)

根据评论,如果您希望从一列或多列中删除逗号,可以使用REPLACE功能。您可以对联盟的整体结果执行此操作,如下所示:

SELECT
   REPLACE(ServerName, ',', ''), 
   Host, 
   Status, 
   Classification, 
   Location 
FROM
(
   SELECT 

   DISTINCT i.ServerName, 'Standalone' AS Host, s.Status, s.Classification, 
            s.Location
   FROM tblSQLInstances i 
      LEFT OUTER JOIN tblServer s
      on i.ServerName = s.ServerName
   WHERE i.IsClustered = 0
      AND i.InstanceStatus != 'Demised'

   UNION

   SELECT esr.ServerName, e.EnvironmentDescription AS Host, s.Status, 
          s.Classification, s.Location  
   FROM tblSQLEnvironmentServerRel esr
      INNER JOIN tblSQLEnvironments e
      ON esr.EnvironmentID = e.EnvironmentID
      LEFT OUTER JOIN tblServer s
      on esr.ServerName = s.ServerName
      LEFT OUTER JOIN tblSQLInstances i
      on i.EnvironmentID = e.EnvironmentID
   WHERE i.InstanceStatus != 'Demised'
) X;