在sql中添加hh / mm / ss到目前为止

时间:2014-11-10 10:38:43

标签: sql sql-server

在下面的查询中我想在date中添加hh / mm / ss。我想将00:00:00和23:59:59添加到我的日期.pls帮助我这样做。

@i_StartDate VARCHAR(50)

    SELECT Distinct p.ProductID,  
       p.ProductName,  
       ISNULL((SELECT top 1 ISNULL( CurrentStock,0.00)   
        FROM   Productstatus PS  
        WHERE  PS.ProductID =p.ProductID  
               AND PS.LocationID = 1  
               AND  PS.StatusDateTime>= @i_StartDate+'00:00:00' AND  PS.StatusDateTime<= @i_StartDate+'23:59:59'
               and PS.productid=p.productid),0) OpeningStockQuantity
     from Product p

4 个答案:

答案 0 :(得分:0)

转换为Datetime可以帮助你..

SELECT Distinct p.ProductID,  
       p.ProductName,  
       ISNULL((SELECT top 1 ISNULL( CurrentStock,0.00)   
        FROM   Productstatus PS  
        WHERE  PS.ProductID =p.ProductID  
               AND PS.LocationID = 1  
               AND  PS.StatusDateTime>= convert(datetime,@i_StartDate+' 00:00:00') AND  PS.StatusDateTime<= convert(datetime,@i_StartDate+' 23:59:59')
               and PS.productid=p.productid),0) OpeningStockQuantity
     from Product p

答案 1 :(得分:0)

在日期中添加时间并投射到日期时间

SELECT Distinct p.ProductID,  
   p.ProductName,  
   ISNULL((SELECT top 1 ISNULL( CurrentStock,0.00)   
    FROM   Productstatus PS  
    WHERE  PS.ProductID =p.ProductID  
           AND PS.LocationID = 1  
           AND  
CAST(PS.StatusDateTime as DATETIME)>= 
CAST(DATEADD(day, DATEDIFF(day, 0,@i_StartDate), '00:00:00') as DATETIME) AND 
CAST(PS.StatusDateTime as DATETIME)<= 
CAST(DATEADD(day, DATEDIFF(day, 0,@i_StartDate), '23:59:59') as DATETIME) AND
PS.productid=p.productid),0) OpeningStockQuantity
 from Product p

答案 2 :(得分:0)

AND  PS.StatusDateTime>= CONCAT(@i_StartDate,' 00:00:00')

 AND 

 PS.StatusDateTime<= CONCAT(@i_StartDate,' 23:59:59')

- &GT;

答案 3 :(得分:0)

假设您使用的是SQL 2008+,只需将StatusDateTime转换为Date并与您的@i_StartDate进行比较。

DECLARE @i_StartDate date = '2014-11-01'

    SELECT DISTINCT 
        p.ProductID,  
        p.ProductName,  
        ISNULL((SELECT top 1 ISNULL(CurrentStock,0.00)   
        FROM   Productstatus PS  
        WHERE  PS.ProductID =p.ProductID  
            AND PS.LocationID = 1  
            AND  CAST(PS.StatusDateTime AS DATE) = @i_StartDate
            AND PS.productid=p.productid),0) OpeningStockQuantity
        FROM Product p