管道分隔输出

时间:2015-09-17 14:05:38

标签: sql-server-2012

我有以下代码,我需要管道分隔格式的格式。我尝试添加+' |'但似乎我没有正确地这样做,所以它继续抛出错误。由于权利,它不能是存储过程。此外,我无法永久更改结果输出,因此无法使用工具选项菜单。有人可以在下面的select语句中为我正确添加管道。万分感谢。我很困惑。

SELECT  DISTINCT
Name,
CAST(DOB AS DATE) AS DateOfBirth,
Address1 AS AddressStreet1,
Address2 AS AddressStreet2,
City AS AddressCity,
State AS AddressState,
Postal AS AddressZip,
Sex AS Gender,
CAST(ActivityDateTime AS DATE) AS ActivityDate,
CASE
    WHEN ID IN ('R','A')
    THEN 'Y'
ELSE 'N'
END AS Allow

1 个答案:

答案 0 :(得分:0)

我真的不确定这是否是您正在寻找的,但一种选择是将所有字段都放入其中,并在它们之间使用管道。 UNION ALL允许您在第一行显示标题。

SELECT 'Name|DateOfBirth|AddressStreet1|AddressStreet2|AddressCity|AddressState|AddressZip|Gender|ActivityDate|Allow' AS Data
UNION ALL
SELECT  DISTINCT
Name + '|' +
CAST(DOB AS DATE) + '|' +
Address1 + '|' +
Address2 + '|' +
City + '|' +
State + '|' +
Postal + '|' +
Sex + '|' +
CAST(ActivityDateTime AS DATE) + '|' +
CASE
    WHEN ID IN ('R','A')
    THEN 'Y'
ELSE 'N'
END
AS Data