SQL Server日期时间未正确显示

时间:2018-07-17 04:58:31

标签: sql sql-server date datetime

我为指定日期的过去一周(即2018年7月1日至8日)的员工的FirstIN和LastOUT创建查询。这是我的查询:

select  
        CredentialId,
        Value as Department,
        UID1FirstName +' '+ UID1LastName as EmployeeName,
        FIRSTIN, LASTOUT,
        DATEDIFF(HOUR, FIRSTIN, LASTOUT) as NUMBEROFHOURS from 
(
    select 
        CAST(dtDate as date) as LogDate,
        MIN(CASE when ReaderName like '%ENTRY%' then dtDate END) as FIRSTIN,
        MAX(CASE when ReaderName like '%EXIT%' then dtDate END) as LASTOUT,
        CredentialId, 
        UID1FirstName,
        UID1LastName,
        Value

    from Log_Transactions

    inner join UserCredentials on UID1 = CredentialId
    inner join UserDefinedFields on HostUserId = UserID

    where dtDate between '2018-07-01' and '2018-07-08' and  --EDIT DATES INSIDE, FORMATTED AS YY-MM-DD
            UID1FirstName like '%JASTINE%' and              --EDIT NAME INSIDE THE PERCENT SYMBOLS
            Event = '2000' and FieldNo = '1'

    group by
          CAST(dtDate as DATE), CredentialId, UID1FirstName, UID1LastName, Value
)
as DT;

这是该查询的结果:

SEE RESULT HERE

我的问题是,当我输入人的dtDate并将日期时间转换为时间只是为了轻松查看FirstIN和LastOUT的记录时,FirstIN和LastOUT的值会显示很多,而其他输出则变成NULL

因此,这是添加的查询:

select  
        CredentialId,
        Value as Department,
        UID1FirstName +' '+ UID1LastName as EmployeeName,
        convert(varchar(18), dtDate,1) as LogDATE,
        FIRSTIN, LASTOUT,
        DATEDIFF(HOUR, FIRSTIN, LASTOUT) as NUMBEROFHOURS from 
(
    select 
        CAST(dtDate as date) as LogDate,
        MIN(CASE when ReaderName like '%ENTRY%' then convert(varchar(18), dtDate,108) END) as FIRSTIN,
        MAX(CASE when ReaderName like '%EXIT%' then convert(varchar(18), dtDate,108) END) as LASTOUT,
        CredentialId, 
        UID1FirstName,
        UID1LastName,
        Value,
        dtDate

    from Log_Transactions

    inner join UserCredentials on UID1 = CredentialId
    inner join UserDefinedFields on HostUserId = UserID

    where dtDate between '2018-07-01' and '2018-07-08' and  --EDIT DATES INSIDE, FORMATTED AS YY-MM-DD
            UID1FirstName like '%JASTINE%' and              --EDIT NAME INSIDE THE PERCENT SYMBOLS
            Event = '2000' and FieldNo = '1'

    group by
          CAST(dtDate as DATE), CredentialId, UID1FirstName, UID1LastName, Value, dtDate
)
as DT;

这是添加查询的结果:

SEE RESULT OF ADDED QUERY

这里是我想要生产的样品输出:

SAMPLE OUTPUT I WANT

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

尝试对最外面的SELECT进行格式化,因为来自子查询的数据已正确分组,并且不需要其他转换。

select  
        CredentialId,
        Value as Department,
        UID1FirstName +' '+ UID1LastName as EmployeeName,
        CONVERT(DATE, FIRSTIN) AS LogDATE,
        CONVERT(VARCHAR(18), FIRSTIN, 108) AS FIRSTIN,
        CONVERT(VARCHAR(18), LASTOUT, 108) AS LASTOUT,
        DATEDIFF(HOUR, FIRSTIN, LASTOUT) as NUMBEROFHOURS from 
(
    select 
        CAST(dtDate as date) as LogDate,
        MIN(CASE when ReaderName like '%ENTRY%' then dtDate END) as FIRSTIN,
        MAX(CASE when ReaderName like '%EXIT%' then dtDate END) as LASTOUT,
        CredentialId, 
        UID1FirstName,
        UID1LastName,
        Value

    from Log_Transactions

    inner join UserCredentials on UID1 = CredentialId
    inner join UserDefinedFields on HostUserId = UserID

    where dtDate between '2018-07-01' and '2018-07-08' and  --EDIT DATES INSIDE, FORMATTED AS YY-MM-DD
            UID1FirstName like '%JASTINE%' and              --EDIT NAME INSIDE THE PERCENT SYMBOLS
            Event = '2000' and FieldNo = '1'

    group by
          CAST(dtDate as DATE), CredentialId, UID1FirstName, UID1LastName, Value
)
as DT;