1

嗨,我有一个 SQL 脚本,它将一些数据列输出到 csv 文件中,其中一个是电话号码。该脚本一切正常,但是从电话号码中删除了前导零。现在我知道为什么在包含电话号码的 excel 中打开 csv 时,为什么会从电话号码中去除前导零。

我正在努力解决的问题是 SQL 脚本将电话号码用双引号括起来,以便在打开时将其解释为文本。请参阅下面的我的 sql:

SET @query ='
SELECT
    ''MobilePhoneNumber'' AS MobilePhoneNumber
    ,''RequestNumber'' AS RequestNumber
    ,''RequestDate'' AS RequestDate
    ,''RequestType'' AS RequestType
    ,''Status'' AS Status
UNION ALL
SELECT 
    [MobilePhoneNumber] --I Want to get this column in the csv as EG "01234123456"
    ,[RequestNumber]
    ,CONVERT(VARCHAR,[RequestDate],120)
    ,[RequestType]
    ,[Status]
FROM [Mobiles].[dbo].[Requests]
WHERE [RequestType] = ''Cease''
    AND DATEDIFF(mm,Requestdate , GETDATE())=1;'
4

2 回答 2

1

试试这个:

'...
SELECT 
CHAR(34) + [MobilePhoneNumber] + CHAR(34)
...'

或者

'...
SELECT 
''"'' + [MobilePhoneNumber] + ''"''
...'
于 2013-07-05T15:35:21.740 回答
0

感谢@KekuSemau的建议,这是我最终为他人谋福利的解决方案:

SET @query ='
SELECT
    ''MobilePhoneNumber'' AS MobilePhoneNumber
    ,''RequestNumber'' AS RequestNumber
    ,''RequestDate'' AS RequestDate
    ,''RequestType'' AS RequestType
    ,''Status'' AS Status
UNION ALL
SELECT 
    CHAR(39) +[MobilePhoneNumber] -- < I simply added the CHAR(39)
    ,[RequestNumber]
    ,CONVERT(VARCHAR,[RequestDate],120)
    ,[RequestType]
    ,[Status]
FROM [Mobiles].[dbo].[Requests]
WHERE [RequestType] = ''Cease''
    AND DATEDIFF(mm,Requestdate , GETDATE())=1;'
于 2013-07-08T09:00:27.860 回答