日期时间格式不能完成预期的操作

时间:2014-07-15 15:20:46

标签: sql-server tsql datetime smalldatetime

所以我有一个 datetime 列,我希望在我的报告中转换为 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

1 个答案:

答案 0 :(得分:2)

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

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

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

See More

编辑关于完整代码

   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';