结合使用case子句SQL Server 2014从别名列中删除Char 10,Char 13字符

时间:2018-09-07 17:21:26

标签: sql sql-server tsql ssms

我想在下面的查询(第二个方框)中删除char 10和char 13字符。我试图对下面显示的两个别名字段使用语法。我收到一个问题,其中intellisense试图将其分解为不同的查询。我可以将语法用于一个别名列,但不能用于两个别名列。我该如何做得更好?

Comments = REPLACE(REPLACE([Comments], CHAR(13), ''), CHAR(10), '') AS 'SAP REMARKS'

NOTES   = REPLACE(REPLACE([Notes], CHAR(13), ''), CHAR(10), '') AS 'WMS NOTES'

我有以下查询,它执行得很好。一旦获得要提取的数据,我将从硬编码的值更改日期。

SELECT  
    [DocEntry],
    CASE 
       WHEN DOCSTATUS = 'O' THEN 'OPEN'
       WHEN DOCSTATUS = 'C' THEN 'CLOSED/CANCELLED'
      ELSE NULL 
    END AS 'SAP STATUS',
    [STATUS] AS 'WMS STATUS',
    confirmed,
    [CardCode], [CardName],
    Comments AS 'SAP REMARKS',
    NOTES AS 'WMS NOTES',
    [DocDate], [DocDueDate] 
FROM
    [DBASE1].[ENV].[dbo].[ORDR]B
LEFT JOIN 
    [DBASE2].[ENV].[dbo].[OUTBOUNDORHEADER] A ON orderid = DocEntry
WHERE 
    DOCDATE = '2018-09-06'    


SELECT  
    [DocEntry],
    CASE
       WHEN DOCSTATUS = 'O' THEN 'OPEN'
       WHEN DOCSTATUS = 'C' THEN 'CLOSED/CANCELLED'
       ELSE NULL 
    END AS 'SAP STATUS',
    [STATUS] AS 'WMS STATUS',
    confirmed,
    [CardCode], [CardName],
    Comments = REPLACE(REPLACE([Comments], CHAR(13), ''), CHAR(10), '') AS 'SAP REMARKS',
    NOTES = REPLACE(REPLACE([Notes], CHAR(13), ''), CHAR(10), '') AS 'WMS NOTES',
    [DocDate]
FROM 
    [DBASE1].[ENV].[dbo].[ORDR]B
LEFT JOIN 
    [DBASE2].[ENV].[dbo].[OUTBOUNDORHEADER] A ON orderid = DocEntry
WHERE 
    DOCDATE = '2018-09-06'

3 个答案:

答案 0 :(得分:1)

以下应该可以正常工作:

SELECT  [DocEntry]
,CASE DOCSTATUS WHEN 'O' THEN 'OPEN' WHEN 'C' THEN 'CLOSED/CANCELLED' ELSE NULL END AS 'SAP STATUS'
,[STATUS] AS 'WMS STATUS' 
,confirmed
,[CardCode]
,[CardName]
,replace(replace([Comments],Char(13),''),CHAR(10),'') AS 'SAP REMARKS'
,replace(replace([Notes],Char(13),''),CHAR(10),'') AS 'WMS NOTES'
,[DocDate]
,[DocDueDate] 
FROM [DBASE1].[ENV].[dbo].[ORDR]B
left join [DBASE2].[ENV].[dbo].[OUTBOUNDORHEADER]A
on orderid = DocEntry
where DOCDATE = '2018-09-06'

答案 1 :(得分:1)

运行以下命令(不使用原始语法的comments =部分):

SELECT  [DocEntry]
,CASE
WHEN DOCSTATUS = 'O' 
THEN 'OPEN'
WHEN DOCSTATUS = 'C'
THEN 'CLOSED/CANCELLED'
ELSE NULL END AS 'SAP STATUS'
,[STATUS] AS 'WMS STATUS' 
,confirmed
,[CardCode]
,[CardName]
,replace(replace([Comments],Char(13),''),CHAR(10),'') AS 'SAP REMARKS'
,replace(replace([Notes],Char(13),''),CHAR(10),'') AS 'WMS NOTES'
,[DocDate]
 FROM [DBASE1].[ENV].[dbo].[ORDR]B
 left join [DBASE2].[ENV].[dbo].[OUTBOUNDORHEADER]A
on orderid = DocEntry
where DOCDATE = '2018-09-06'

通常,如果您要更新源表,设置声明的变量的值或类似的内容,则只需要使用comments =之类的东西。

答案 2 :(得分:1)

感谢您的额外查询。就是我的想法您要对列进行两次别名,一次使用xxx = ...,然后再次使用AS。在这里,您去了:

SELECT
  [DocEntry]
 ,CASE
    WHEN DOCSTATUS = 'O' THEN 'OPEN'
    WHEN DOCSTATUS = 'C' THEN 'CLOSED/CANCELLED'
    ELSE NULL
  END AS [SAP STATUS]
 ,[STATUS] AS [WMS STATUS]
 ,confirmed
 ,[CardCode]
 ,[CardName]
 ,REPLACE(REPLACE([Comments], CHAR(13), ''), CHAR(10), '') AS [SAP REMARKS]
 ,REPLACE(REPLACE(CAST([Notes] AS NVARCHAR(MAX)), CHAR(13), ''), CHAR(10), '') AS [WMS NOTES]
 ,[DocDate]
 ,[DocDueDate]
FROM
  [DBASE1].[ENV].[dbo].[ORDR] AS B
  LEFT JOIN
    [DBASE2].[ENV].[dbo].[OUTBOUNDORHEADER] AS A
      ON
      orderid = DocEntry
WHERE
  DOCDATE = '2018-09-06';

编辑:根据评论,在Notes列中添加了显式的CAST。