Link to home
Start Free TrialLog in
Avatar of maqskywalker
maqskywalker

asked on

t-sql using DateAdd function on column with datetime datatype

Hi experts,

This is related to the queries in this ticket.
https://www.experts-exchange.com/questions/29096348/using-t-sql-date-function-to-get-records-older-than-10-days.html?anchor=a42544980¬ificationFollowed=206809480&anchorAnswerId=42544980#a42544980

I'm using sql server 2008

I have a table that looks like this called Orders2:

Notice that there are 2 records for 4/16/2018.

User generated image
This is the script for this table.

USE [TestDatabase]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Orders2](
	[OrderID] [int] NULL,
	[LastName] [nvarchar](255) NULL,
	[FirstName] [nvarchar](255) NULL,
	[OrderDate] [datetime] NULL
) ON [PRIMARY]

GO
INSERT [dbo].[Orders2] ([OrderID], [LastName], [FirstName], [OrderDate]) VALUES (1, N'Smith', N'Sam', CAST(N'2018-04-16T20:10:00.000' AS DateTime))
INSERT [dbo].[Orders2] ([OrderID], [LastName], [FirstName], [OrderDate]) VALUES (2, N'Johnson', N'Tony', CAST(N'2018-04-16T00:00:00.000' AS DateTime))
INSERT [dbo].[Orders2] ([OrderID], [LastName], [FirstName], [OrderDate]) VALUES (3, N'Williams', N'Anna', CAST(N'2018-03-27T21:23:00.000' AS DateTime))
INSERT [dbo].[Orders2] ([OrderID], [LastName], [FirstName], [OrderDate]) VALUES (4, N'Westbrook', N'Kyle', CAST(N'2018-03-27T00:00:00.000' AS DateTime))
INSERT [dbo].[Orders2] ([OrderID], [LastName], [FirstName], [OrderDate]) VALUES (5, N'George', N'Norman', CAST(N'2018-03-26T23:21:00.000' AS DateTime))
INSERT [dbo].[Orders2] ([OrderID], [LastName], [FirstName], [OrderDate]) VALUES (6, N'Arnold', N'Laura', CAST(N'2018-03-26T23:21:00.000' AS DateTime))
INSERT [dbo].[Orders2] ([OrderID], [LastName], [FirstName], [OrderDate]) VALUES (7, N'Vanscyoc', N'Maria', CAST(N'2018-03-26T00:00:00.000' AS DateTime))
INSERT [dbo].[Orders2] ([OrderID], [LastName], [FirstName], [OrderDate]) VALUES (8, N'Thompson', N'Joe', CAST(N'2018-03-26T00:00:00.000' AS DateTime))
INSERT [dbo].[Orders2] ([OrderID], [LastName], [FirstName], [OrderDate]) VALUES (9, N'Earnshaw', N'Peter', CAST(N'2018-03-25T08:12:00.000' AS DateTime))
INSERT [dbo].[Orders2] ([OrderID], [LastName], [FirstName], [OrderDate]) VALUES (10, N'Steele', N'Bill', CAST(N'2018-03-25T08:10:00.000' AS DateTime))
INSERT [dbo].[Orders2] ([OrderID], [LastName], [FirstName], [OrderDate]) VALUES (11, N'Felton', N'Samantha', CAST(N'2017-10-23T09:10:13.000' AS DateTime))
INSERT [dbo].[Orders2] ([OrderID], [LastName], [FirstName], [OrderDate]) VALUES (12, N'Johnson', N'Erica', CAST(N'2017-10-10T15:23:16.000' AS DateTime))

Open in new window


I'm trying to return all the records that are 9 days or older.

If i run this query

DECLARE @TestDate as varchar(50)
SET @TestDate = '2018-04-16 20:10:00'
SELECT DATEDIFF(day, @TestDate, GETDATE()) AS 'Duration'  

I get this:

User generated image
If i run this query

DECLARE @TestDate as varchar(50)
SET @TestDate = '2018-04-16 00:00:00'
SELECT DATEDIFF(day, @TestDate, GETDATE()) AS 'Duration'  

I also get this:

User generated image

Now i try my query on my table.
This is my query:

----- only return records that are 9 days or older
DECLARE @NumberOfDays as int
SET @NumberOfDays = 9

SELECT [OrderID]
      ,[LastName]
      ,[FirstName]
      ,[OrderDate]
FROM [TestDatabase].[dbo].[Orders2] o
WHERE  O.OrderDate <= CAST(DATEADD(DAY, -@NumberOfDays, GETDATE()) AS DATE)
ORDER BY o.[OrderDate] DESC

When i run it, i get this. Notice , it only got one of the records from 4/16/2018.
It didn't return the record for Sam Smith. I believe it's because he has a time and Tony Johnson doesn't.


User generated image
How do i fix my query? So that it doesn't matter if the date has a time on it or not.
The query for 9 days or older should return both records in 4/16/2018.
SOLUTION
Avatar of Snarf0001
Snarf0001
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> If you want to leave @numberofdays as is

@Mark WillsTopic - Well, if you're going to be logical about it ....  ;-)  Seriously though, that's a good way of explaining it.
Avatar of maqskywalker
maqskywalker

ASKER

thanks. good stuff.