Date UserID
1/1/2013 Jo1234
2/1/2013 Bob1234
2/2/2013 Bob1234
2/3/2013 Cal5678
1/4/2013 Bob1234
2/1/2013 Bob1234
2/2/2013 Bob1234
2/3/2013 Cal5678
Hi I would like to find the users who didn't log in for two weeks or more
SELECT DISTINCT [UserID] FROM UsersTable
I cant use WEEK()
as I am using an older version of sql I appreciate your help
SELECT DISTINCT [UserID] FROM UsersTable WHERE DATEDIFF(DAY, myDateCol, getDate()) >= 14
Basic date functions are easily found for SQL Server; please check Google for date calculations if you need something more precise.
Since you want to filter based on two weeks (14 days), you can use the following to filter the results out :
DATEADD(DAY, -14, GETDATE()
Consider the following dataset :
CREATE TABLE Test(LastDateOfLogin DATE, UserID NVARCHAR(20))
INSERT INTO Test VALUES('11-Nov-2020', 'Jo1234')
INSERT INTO Test VALUES('01-Nov-2020', 'Bob1234')
INSERT INTO Test VALUES('02-Nov-2020', 'Bob1234')
INSERT INTO Test VALUES('03-Nov-2020', 'Cal5678')
INSERT INTO Test VALUES('03-Nov-2020', 'Bob1234')
INSERT INTO Test VALUES('12-Nov-2020', 'Bob1234')
INSERT INTO Test VALUES('15-Nov-2020', 'Bob1234')
INSERT INTO Test VALUES('05-Nov-2020', 'Cal5678')
The table will look as :
Write the query :
SELECT LastDateOfLogin, UserID
FROM Test
WHERE LastDateOfLogin <= DATEADD(DAY, - 14, GETDATE())
The result will be :
Hope, this helps!!
To answer your part about WEEK(), you are correct. No such thing exists. You have to use DATEPART().
SELECT DATEPART(WW, GETDATE());
I think what you are looking for though is
SELECT DISTINCT [UserID]
FROM UsersTable
GROUP BY [UserID]
HAVING MAX(Date) < CAST(DATEADD(WEEK, -1, GETDATE()) AS DATE)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.