March 9, 2010 at 2:05 pm
I have the following query;
SELECT dbo.Product.ProductID, dbo.Product.Category, dbo.Product.ProductCode, dbo.Product.Description, dbo.Product.Price, dbo.[Order].CustomerID, dbo.[Order].OrderID,
dbo.[Order].OrderDate
FROM dbo.[Order] INNER JOIN
dbo.Product ON dbo.[Order].ProductID = dbo.Product.ProductID
and I want to return only the orders made today. If I use the GETDATE() function in a WHERE clause - nothing. I've tried the CONVERT funtion to remove the time element to no avail. Any help greatfully received.....
March 9, 2010 at 2:17 pm
rburke 87921 (3/9/2010)
I have the following query;SELECT dbo.Product.ProductID, dbo.Product.Category, dbo.Product.ProductCode, dbo.Product.Description, dbo.Product.Price, dbo.[Order].CustomerID, dbo.[Order].OrderID,
dbo.[Order].OrderDate
FROM dbo.[Order] INNER JOIN
dbo.Product ON dbo.[Order].ProductID = dbo.Product.ProductID
and I want to return only the orders made today. If I use the GETDATE() function in a WHERE clause - nothing. I've tried the CONVERT funtion to remove the time element to no avail. Any help greatfully received.....
DECLARE @Today DATE = GETDATE()
Query ...
WHERE dbo.[Order].OrderDate >= @Today
March 9, 2010 at 2:31 pm
Here is your query rewritten:
SELECT
prd.ProductID,
prd.Category,
prd.ProductCode,
prd.Description,
prd.Price,
ord.CustomerID,
ord.OrderID,
ord.OrderDate
FROM
dbo.[Order] ord
INNER JOIN dbo.Product prd
ON ord.ProductID = prd.ProductID
WHERE
ord.OrderDate >= dateadd(dd, datediff(dd, 0, getdate()), 0) and
ord.OrderDate < dateadd(dd, datediff(dd, 0, getdate()) + 1, 0);
Please note how I truncate the time from the getdate() function. Also note how I use table aliases in the from clause to alias the column names in the ON and SELECT. Three and four part names are being depreciated and may not be supported in future versions of SQL Server, so you should get in the habit of using table aliases in the FROM clause.
Hope this helps.
March 9, 2010 at 11:29 pm
thanks for the information.
March 10, 2010 at 2:07 am
Hi Lynn - thanks for that but I still get no rows returned even though there is a record with the date 2010-03-10 in the order date field. Help!
March 10, 2010 at 2:35 am
rburke 87921 (3/10/2010)
Hi Lynn - thanks for that but I still get no rows returned even though there is a record with the date 2010-03-10 in the order date field. Help!
Please post the CREATE TABLE scripts for the order table.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
March 10, 2010 at 2:41 am
USE [Development]
GO
/****** Object: Table [dbo].[Order] Script Date: 03/10/2010 09:38:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Order](
[OrderID] [int] IDENTITY(1,1) NOT NULL,
[CustomerID] [int] NOT NULL,
[ProductID] [int] NOT NULL,
[OrderDate] [date] NOT NULL,
[UserName] [nvarchar](15) NOT NULL,
CONSTRAINT [PK_Order] PRIMARY KEY CLUSTERED
(
[OrderID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Order] WITH CHECK ADD CONSTRAINT [FK_Order_Customer] FOREIGN KEY([CustomerID])
REFERENCES [dbo].[Customer] ([CustomerID])
GO
ALTER TABLE [dbo].[Order] CHECK CONSTRAINT [FK_Order_Customer]
GO
ALTER TABLE [dbo].[Order] WITH CHECK ADD CONSTRAINT [FK_Order_Product] FOREIGN KEY([ProductID])
REFERENCES [dbo].[Product] ([ProductID])
GO
ALTER TABLE [dbo].[Order] CHECK CONSTRAINT [FK_Order_Product]
GO
ALTER TABLE [dbo].[Order] WITH CHECK ADD CONSTRAINT [FK_Order_User] FOREIGN KEY([UserName])
REFERENCES [dbo].[User] ([UserName])
GO
ALTER TABLE [dbo].[Order] CHECK CONSTRAINT [FK_Order_User]
GO
ALTER TABLE [dbo].[Order] ADD CONSTRAINT [DF_Order_OrderDate] DEFAULT (getdate()) FOR [OrderDate]
GO
March 10, 2010 at 2:46 am
Thanks!
Try this first:
DECLARE @Today DATE = GETDATE()
SELECT
prd.ProductID,
prd.Category,
prd.ProductCode,
prd.Description,
prd.Price,
ord.CustomerID,
ord.OrderID,
ord.OrderDate
FROM dbo.[Order] ord
LEFT JOIN dbo.Product prd
ON ord.ProductID = prd.ProductID
WHERE ord.OrderDate = @Today
Edit: @steve-2 & Lynn - hope you don't mind me borrowing your code, lads. It helps to keep a level playing field for the OP.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
March 10, 2010 at 2:58 am
I probably should have mentioned that I'm trying to create a view. Doesn't seem to like the DECLARE statement.
March 10, 2010 at 3:06 am
rburke 87921 (3/10/2010)
I probably should have mentioned that I'm trying to create a view. Doesn't seem to like the DECLARE statement.
Why try to turn a query into a view when you're unsure whether or not the query works? That's like trying to put the windows into a house before the walls are up 😀
Let's stick with a plain 'ol query until you've figured out what's going wrong, then wrap it in a view afterwards.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
March 10, 2010 at 3:08 am
Forget that last post. I'm being a k*ob!. That works now thankyou very much.
March 10, 2010 at 3:13 am
rburke 87921 (3/10/2010)
Forget that last post. I'm being a k*ob!. That works now thankyou very much.
Heh funny! No you're not, you're learning.
Was it the inner join?
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
March 10, 2010 at 3:16 am
So how do I now incorporate;
DECLARE @Today DATE = GETDATE()
SELECT
prd.ProductID,
prd.Description,
prd.Price,
ord.OrderID,
ord.OrderDate
FROM dbo.[Order] ord
LEFT JOIN dbo.Product prd
ON ord.ProductID = prd.ProductID
WHERE ord.OrderDate = @Today
.......into a view?
March 10, 2010 at 3:26 am
This is the equivalent without using a variable:
SELECT
prd.ProductID,
prd.[Description],
prd.Price,
ord.OrderID,
ord.OrderDate
FROM dbo.[Order] ord
LEFT JOIN dbo.Product prd
ON ord.ProductID = prd.ProductID
WHERE ord.OrderDate = CAST(GETDATE() AS DATE)
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
March 10, 2010 at 3:40 am
Magic - Thanks very much Chris.
Viewing 15 posts - 1 through 15 (of 23 total)
You must be logged in to reply to this topic. Login to reply