1

所以我有一个日期时间列,我想在报告中将其转换为smalldatetime 。我期待的是这样的:

2011-07-13 09:51:21.087 to 2011-07-13 09:51:21

简单吧?那么datetime列返回一个 T 而不是一个空格,所以我认为转换应该删除它但无济于事......想法?

SELECT LockoutDate
CONVERT(smalldatetime,LockoutDate)[Small Date]

       LockoutDate         |       Small Date
2011-07-13T09:51:21.087    |   2011-07-13T09:51:00

那个“T”是什么,为什么会出现?同样,实际列是 datetime 而不是 nvarchar 或任何东西......

编辑:这是我正在处理的代码。

    DECLARE @tableLockPass NVARCHAR(MAX);
SET @tableLockPass = --Statement to check active accounts that are locked
        N'<H3>Locked SQL Accounts</H3>' +
        N'<table border="2" style="font-size:12px">' +
        N'<tr>' +
            N'<th>Instance</th>' +
            N'<th>Instance Purpose</th>' +          
            N'<th>Domain Account</th>' +
            N'<th>Name</th>' +
            N'<th>Account Lock</th>' +
            N'<th>Lockout Time</th>' +
        N'</tr>' +
            CAST ((
                SELECT
                [td/@align] = 'center',
                td = [Instance], '',
                [td/@align] = 'center',
                td = [Instance Purpose], '',                
                [td/@align] = 'center',
                td = Name, '',
                [td/@align] = 'center',
                td = [Display Name], '',
                [td/@align] = 'center',
                td = [Account Lock],  '',
                [td/@align] = 'center',
                td = [Lockout Time], ''
                FROM(
                    SELECT Name
                            ,ADDisplayName [Display Name]
                            ,Instance
                            ,InstancePurpose [Instance Purpose]
                            ,replace(IsLocked,1,'In Place') as [Account Lock]
                            ,convert(smalldatetime,LockoutTime,120) [Lockout Time]
                        FROM dbo.Security
                        WHERE IsLocked = 1
                    ) AS DS
                ORDER BY Instance, [Display Name]
                        FOR XML PATH('tr'), TYPE
                    )AS NVARCHAR(MAX) ) +
        N'</table>';
IF @tableLockPass IS NOT NULL
        EXEC msdb.dbo.sp_send_dbmail
            @profile_name = 'SQL_DBMail',
            @importance = 'NORMAL',
            @recipients = '',   
            @subject = '',
            @body = @tableLockPass, 
            @body_format = 'HTML';

我不知道这是否是弱代码。我对 SQL 还很陌生,所以这是我拼凑起来的。

结果是一个像这样的小表格:

实例 [实例用途] 名称 [帐户锁定] [锁定时间]

DEV5 一般使用 BMMcClure 到位 2011-07-13T09:51:00

4

1 回答 1

2

T 只是一种标准(ISO 8601)方式来划分时间。

将 STYLE 添加到CONVERT函数以指定如何翻译表达式:

SELECT LockoutDate, 
CONVERT(smalldatetime,LockoutDate,120) [Small Date]

看更多

编辑:在您的完整代码上

   DECLARE @tableLockPass NVARCHAR(MAX);
SET @tableLockPass = --Statement to check active accounts that are locked
        N'<H3>Locked SQL Accounts</H3>' +
        N'<table border="2" style="font-size:12px">' +
        N'<tr>' +
            N'<th>Instance</th>' +
            N'<th>Instance Purpose</th>' +          
            N'<th>Domain Account</th>' +
            N'<th>Name</th>' +
            N'<th>Account Lock</th>' +
            N'<th>Lockout Time</th>' +
        N'</tr>' +
            CAST ((
                SELECT
                [td/@align] = 'center',
                td = [Instance], '',
                [td/@align] = 'center',
                td = [Instance Purpose], '',                
                [td/@align] = 'center',
                td = Name, '',
                [td/@align] = 'center',
                td = [Display Name], '',
                [td/@align] = 'center',
                td = [Account Lock],  '',
                [td/@align] = 'center',
         td = convert(smalldatetime,[Lockout Time],120), ''
                FROM(
                    SELECT Name
                            ,ADDisplayName [Display Name]
                            ,Instance
                            ,InstancePurpose [Instance Purpose]
                            ,replace(IsLocked,1,'In Place') as [Account Lock]
                            ,convert(smalldatetime,LockoutTime,120) [Lockout Time]
                        FROM dbo.Security
                        WHERE IsLocked = 1
                    ) AS DS
                ORDER BY Instance, [Display Name]
                        FOR XML PATH('tr'), TYPE
                    )AS NVARCHAR(MAX) ) +
        N'</table>';
IF @tableLockPass IS NOT NULL
        EXEC msdb.dbo.sp_send_dbmail
            @profile_name = 'SQL_DBMail',
            @importance = 'NORMAL',
            @recipients = '',   
            @subject = '',
            @body = @tableLockPass, 
            @body_format = 'HTML';
于 2014-07-15T15:39:02.283 回答