简体   繁体   English

SQL Server:GETDATE 不返回今天的日期

[英]SQL Server : GETDATE not returning today's date

Below is my query.以下是我的查询。 Everything is working except for the GETDATE function in the WHERE clause.除了WHERE子句中的GETDATE函数外,一切正常。 It won't return today's date if I put the date in there like this: 7/12/22.如果我这样输入日期,它将不会返回今天的日期:22 年 7 月 12 日。 It is a DATETIME column in the backend.它是后端的DATETIME列。 Thanks in advance.提前致谢。

SELECT 
    acsMFG.dbo.production_posting_trans.item_no,
    SUM(acsMFG.dbo.production_posting_trans.good_quantity) AS [Good Qty],
    SUM(acsMFG.dbo.production_posting_trans.scrap_quantity) AS [Scrap Qty],
    acsAUTOSYS.dbo.inventory_master.selling_price
FROM
    acsAUTOSYS.dbo.inventory_master
FULL OUTER JOIN 
    acsMFG.dbo.production_posting_trans ON acsMFG.dbo.production_posting_trans.item_no = acsAUTOSYS.dbo.inventory_master.item_no
                                        AND acsAUTOSYS.dbo.inventory_master.company_code = acsMFG.dbo.production_posting_trans.company_code
WHERE  
    acsMFG.dbo.production_posting_trans.company_code = '10'
    AND acsMFG.dbo.production_posting_trans.production_date = GETDATE()
    AND acsMFG.dbo.production_posting_trans.posting_type = 'MMQ'
        OR acsMFG.dbo.production_posting_trans.posting_type = 'IRS'
        OR acsMFG.dbo.production_posting_trans.posting_type = 'PME'
GROUP BY 
    acsMFG.dbo.production_posting_trans.item_no,
    acsAUTOSYS.dbo.inventory_master.selling_price 

Well, when you say SELECT GETDATE();好吧,当你说SELECT GETDATE(); what do you see?你看到了什么? There is a time component there too, so if the data in the table is 2022-07-12 15:12 and you run the query at 2022-07-12 15:13 , that's not a match.那里也有一个时间分量,所以如果表中的数据是2022-07-12 15:12并且您在2022-07-12 15:13运行查询,那不匹配。

If you want data from today, you need a range query:如果你想要今天的数据,你需要一个范围查询:

WHERE col >= CONVERT(date, GETDATE())
  AND col <  DATEADD(DAY, 1, CONVERT(date, GETDATE()));

It is cleaner to use variables, eg使用变量更简洁,例如

DECLARE @today date = GETDATE();
DECLARE @tomorrow date = DATEADD(DAY, 1, @today);

...
WHERE col >= @today
  AND col <  @tomorrow;

Don't get tempted into doing this:不要试图这样做:

WHERE CONVERT(date, col) = CONVERT(date, GETDATE());

It will work, but it's not fantastic .它会起作用,但它并不出色

For the actual problem with OR logic, you have:对于 OR 逻辑的实际问题,您有:

  ... date clauses with AND ...
  AND acsMFG.dbo.production_posting_trans.posting_type='MMQ' 
  Or acsMFG.dbo.production_posting_trans.posting_type ='IRS' 
  Or acsMFG.dbo.production_posting_trans.posting_type ='PME'

I think you want:我想你想要:

  AND 
  (
    acsMFG.dbo.production_posting_trans.posting_type='MMQ' 
    Or acsMFG.dbo.production_posting_trans.posting_type ='IRS' 
    Or acsMFG.dbo.production_posting_trans.posting_type ='PME'
  )

As for aliases:至于别名:

FROM
  acsAUTOSYS.dbo.inventory_master AS im
FULL OUTER JOIN 
  acsMFG.dbo.production_posting_trans AS ppt

Now all your references can be:现在您的所有参考资料都可以是:

  AND 
  (
    ppt.posting_type='MMQ' 
    ppt.posting_type ='IRS' 
    Or ppt.posting_type ='PME'
  )
  GROUP BY 
    ppt.item_no, im.selling_price;

Or better:或更好:

  AND 
  (
    ppt.posting_type IN ('MMQ', 'IRS', 'PME')
  )
  GROUP BY 
    ppt.item_no, im.selling_price;

...so much more readable. ...更具可读性。

Since GETDATE() returns the time too, you will never match.由于 GETDATE() 也返回时间,因此您永远不会匹配。 You need something like:你需要类似的东西:

CAST(acsMFG.dbo.production_posting_trans.production_date AS Date) 
         = CAST(GETDATE() AS Date)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM