T-SQL更改DateTime的时间部分

时间:2014-01-03 13:07:39

标签: sql sql-server datetime

我有一个存储过程,我传给它一个参数:

现在,我如何更改@filterDateFrom@filterDateTo中的时间部分,以设置为@filterTimeFrom@filterTimeTo

这是整个存储过程

USE [DBName]
GO
/****** Object:  StoredProcedure [dbo].[AdminGetAdminBusinessLogList]    Script Date: 1/3/2014 12:17:53 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[AdminGetAdminBusinessLogList]
@UserContextUserId INT, 
@UserContextCompanyId INT, 
@UserContextSessionId UNIQUEIDENTIFIER, 
@UserContextProcessId UNIQUEIDENTIFIER,
@UserContextIPAddress NVARCHAR (100),
@filterDateFrom DATETIME, 
@filterDateTo DATETIME, 
@filterAffectedCompanyId INT, 
@filterUserId INT, 
@FilterEventIdTableType FilterEventIdTableType READONLY, 
@filterTableName NVARCHAR(100), 
@filterEntityId INT,
@filterTimeFrom INT,
@filterTimeTo INT
AS
BEGIN
    SET NOCOUNT ON;

    BEGIN TRY
        EXEC CheckRights @UserContextUserId, NULL, @@PROCID

        DECLARE @MaxRowCount INT
        SELECT @MaxRowCount = CAST([Value] AS INT) FROM Configuration WHERE [Key] = 'System.Sql.MaxRowCount'


        DECLARE @EventIdTableTypeCount INT
        SET @EventIdTableTypeCount = (SELECT COUNT(*) FROM @FilterEventIdTableType)

        SET @filterDateFrom = CAST(FLOOR(CAST(@filterDateFrom AS FLOAT)) AS DATETIME);
        SET @filterDateFrom = DATEADD(hh, @filterTimeFrom, @filterDateFrom);

        SET @filterDateTo = CAST(FLOOR(CAST(@filterDateTo AS FLOAT)) AS DATETIME);
        SET @filterDateTo = DATEADD(hh, @filterTimeTo, @filterDateTo);

        CREATE TABLE #Temp
        (
            [Id] INT NOT NULL
            PRIMARY KEY (Id)                        
        )

        SET ROWCOUNT @MaxRowCount

        --Temp table
      IF (@filterTableName IS NULL OR @filterTableName = '')
        BEGIN
         INSERT INTO #Temp      
            SELECT
                BusinessLog.Id
            FROM
                dbo.BusinessLog  
            WHERE
                (@filterDateFrom IS NULL OR EventDate >= @filterDateFrom) AND 
                (@filterDateTo IS NULL OR EventDate < @filterDateTo) AND
                (@filterAffectedCompanyId IS NULL OR (UserContextCompanyId = @filterAffectedCompanyId OR AffectedCompanyId = @filterAffectedCompanyId)) AND
                (@filterUserId  IS NULL OR BusinessLog.UserContextUserId = @filterUserId) AND
                (@UserContextIPAddress IS NULL OR UserContextIPAddress LIKE @UserContextIPAddress + '%') AND
                (@EventIdTableTypeCount = 0 OR BusinessLog.EventId IN (SELECT Value FROM @FilterEventIdTableType))
        END

      ELSE

        BEGIN
         INSERT INTO #Temp      
            SELECT
                BusinessLog.Id
            FROM
                dbo.BusinessLog  
            INNER JOIN dbo.AuditLog ON AuditLog.BusinessLogId = BusinessLog.Id
            WHERE
                (AuditLog.TableName = @filterTableName AND AuditLog.EntityId = @filterEntityId)
        END     

        --ROWCOUNT visszaállítása
        SET ROWCOUNT 0

        --BusinessLog       
        SELECT
            BusinessLog.Id, EventId, Audience, EventDate, UserContextUserId, UserAccount.UserFullName AS UserContextUserFullName,  UserContextCompanyId, cForUserContext.Name AS UserContextCompanyName, AffectedCompanyId, cForAffected.Name AS AffectedCompanyName, UserContextIPAddress, UserAccount.UserUniqueId AS UserContextUserUniqueId, BusinessLog.MessageFormatVersion
        FROM
            dbo.BusinessLog
        LEFT JOIN dbo.Company cForAffected ON cForAffected.Id = BusinessLog.AffectedCompanyId
        LEFT JOIN dbo.Company cForUserContext ON cForUserContext.Id = BusinessLog.UserContextCompanyId
        LEFT JOIN dbo.UserAccount ON UserAccount.Id = BusinessLog.UserContextUserId
        INNER JOIN #Temp ON #Temp.Id = BusinessLog.Id

        --BusinessLogParameter
        SELECT
            BusinessLogId, IsResource, Value
        FROM
            dbo.BusinessLogParameter
        INNER JOIN #Temp ON #Temp.Id = BusinessLogParameter.BusinessLogId
        ORDER BY #Temp.Id ASC

    END TRY
    BEGIN CATCH
        DECLARE @error NVARCHAR(100)
        DECLARE @severity INT

        SELECT @error = ERROR_MESSAGE()
        SELECT @severity = ERROR_SEVERITY()

        RAISERROR (@error, @severity, 1)
    END CATCH
END

由于

1 个答案:

答案 0 :(得分:1)

首先从@filterDateFrom变量

中删除任何现有的时间部分
SET @filterDateFrom = CAST(FLOOR(CAST(@filterDateFrom AS FLOAT)) AS DATETIME)

然后将小时部分添加到日期

SET @filterDateFrom = DATEADD(hh, @filterTimeFrom, @filterDateFrom)

重复“到”变量的过程