Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Make it look at todays date first

Status
Not open for further replies.

Cpreston

MIS
Mar 4, 2015
972
GB
Hi

I have this code (see below) and its start date is tomorrow. I need the code to show todays date also. I have another query that as todays date but cannot see the difference. Could someone tell me what I need to change to make it look at todays date please

SELECT dbo_OrderHeader.DateRequired, SUM(dbo_OrderLine.TotalVolume)
AS [Total Volume]
FROM dbo_OrderHeader INNER JOIN
dbo_OrderLine ON dbo_OrderHeader.OrderID = dbo_OrderLine.OrderID INNER JOIN
dbo.Product ON dbo_OrderLine.ProductID = dbo.Product.ProductID INNER JOIN
dbo.ProductGroup ON dbo.Product.ProductGroupID = dbo.ProductGroup.ProductGroupID INNER JOIN
dbo.ProductGroup AS ProductGroup_1 ON dbo.ProductGroup.ParentID = ProductGroup_1.ProductGroupID
WHERE (dbo_OrderHeader.StockIssued = 0) AND (ProductGroup_1.Name = 'Arborflor') AND (dbo_OrderHeader.DateRequired >= GETDATE()) OR
(dbo_OrderHeader.StockIssued = 0) AND (dbo_OrderHeader.DateRequired >= GETDATE()) AND (dbo.ProductGroup.Name IN ('TREX')) AND
(dbo_OrderHeader.DateRequired >= CAST(GETDATE() AS DATE)) AND (dbo_OrderHeader.DateRequired <= DATEADD(day, 7, CAST(GETDATE() AS DATE)))
GROUP BY dbo_OrderHeader.DateRequired, dbo.ProductGroup.Name
ORDER BY dbo_OrderHeader.DateRequired
 
OR

if the above is not possible, is it possible for this to always look at tomorrow onwards?

SELECT DateRequired, SUM(TotalVolume) AS M3
FROM dbo.[148-vwOrderHeadCP]
WHERE (DateRequired >= CAST(GETDATE() AS DATE)) AND (DateRequired <= DATEADD(day, 7, CAST(GETDATE() AS DATE))) AND (OrderStatus <> 1) AND (OrderStatus <> 0) AND
(OrderStatus <> 11)
GROUP BY DateRequired
ORDER BY DateRequired ASC

Thanks
 
Hi

I managed to get the second code to start at tomorrow by adding in +1 in

WHERE (DateRequired >= CAST(GETDATE()+1 AS DATE)) AND (DateRequired <= DATEADD(day, 7, CAST(GETDATE()+1 AS DATE)))

I would prefer for the first lot of code to look at todays date and I cannot see why it does not do this. Any ideas please.

Thanks
 
Hi

Managed it I just put in (GETDATE()-1 AS DATE))) in the areas required.

Thanks
 
Here's something to consider. Every time you code has GETDATE() in it, SQL Server has to figure out what that value is. It can change during your code run (maybe only by less than a second...but it still is changing).

Try this instead:
Code:
DECLARE @mydate DATETIME
SET @mydate = GETDATE()

<some code>
WHERE (DateRequired >= CAST(@mydate+1 AS DATE)) AND (DateRequired <= DATEADD(day, 7, CAST(@mydate+1 AS DATE)))

So everywhere you use GETDATE(), you would use @mydate instead.

-SQLBill

The following is part of my signature block and is only intended to be informational.
Posting advice: FAQ481-4875
 
I'll add one more piece of advice to SQLBill's...

Figure the actual date needed for the work AND remove the CAST x AS DATETIME. The value is already a date so there is no need to have SQL keep converting each for each row.
I'd also recommend calculating the start and end dates as separate variables and use them instead of using DATEADD in the WHERE clause wherever possible.

Again, there is nothing "wrong" with your final code. The suggestions might just speed up performance and decrease maintenance issues.
Always test these type of optimizations before moving them into production to ensure you are getting the expected performance changes.

Code:
DECLARE @startdate DATETIME
SET @startdate = DATEADD(DAY, 1, GETDATE())
DECLARE @enddate DATETIME
SET @enddate = DATEADD(DAY, 7, @startdate)

<some code>
WHERE (DateRequired >= @startdate) AND (DateRequired <= @enddate)

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
C#.NET Programmer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top