Get Data From Last 48 Hours - sql-server

I'm using PowerPivot with an SQL Server database and I'm working with a specific table that includes a DateTime column. I'd like to select the data from the table where that DateTime is within the last 48 hours. I'm using a query currently and hoping to achieve something like
... WHERE DT > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL +2 DAY)
I'm getting a syntax error at DAY in this particular way. Is this the best way to do it? If so, what's wrong with how I've written it? If not, what's a better way?

Sql-Server
WHERE DateTimeColumn >= DATEADD(HOUR, -48, GETDATE())
Mysql
WHERE DateTimeColumn > DATE_SUB(NOW(), INTERVAL 48 HOUR)

Related

How to write a select query that returns data that is older than a month specifically using EPOCH?

Need to write select query that returns data that is older than a month and I need to use EPOCH.
select *
from TABLE
where DATE < (NOW() - 604800) <- does not work.
I don't understand why SQL Server would not have something this basic.
Actually, it's easier in SQL Server: you can achieve what you want to do with the following query;
select * from Table where Date < DATEADD(month, -1, GETDATE())

SQL Server - notify 30,15, 7 days from time

I'm trying to right an SQL Query that will do the following task:
Example:
ExpirationDate = '2018-04-30 00:00:00.000'
when the property is set to expire in 30 days ... 1 month before, I want SQL Server to email me informing that the item is about to expire based upon (Expirationdate)
could someone please give me some pointers as to where I go next... this is what I have tried so far..
the query below will find the item however I now want SQL Server Agent to send me an email for each row that is returned, not when nothing is returned
SELECT *
FROM [dbo].[Property]
WHERE expirationdate <= DateAdd(day ,30 , GetDate())
Here is an excellent link on how to email results from a sql server job agent:
https://www.brentozar.com/archive/2014/10/send-query-results-sql-server-agent-job/
As for your query: I wouldn't SELECT *. Explicitly select the columns you want returned in your email response.
Next I would define a variable to hold the value of your cutoff, this will prevent you doing a calculation for each row within the query. Example:
DECLARE #day30UpperBound DATETIME = DATEADD(Day, 31, cast(getdate() as date));
DECLARE #day30LowerBound DATETIME = DATEADD(Day, 30, cast(getdate() as date));
Then you can query results with:
SELECT explicitColumnList FROM [dbo].[Property]
WHERE expirationdate BETWEEN #day30LowerBound AND #day30UpperBound
This specific example should give you those Properties with an expiration date that fall in the window of 30 days from current at midnight, to 31 days from current at midnight - so a full 24 hour window 30 days from current day.
You can easily reproduce this for your 15 day, and 7 day reports. You could put all 3 reports into one job agent. Or you could make a different job for each of the 3.
You'll have to research a little on how to do only business days if that's what you want.

SQL query to search

i am new at sqlserver and i just started learning it , and i have a question .
**i want an SQL query that can bring up all rows that were added in the past 6 months. in table 'users' the date is in field 'joined' but its in UNIX time stamp format
**and i want then like another SQL query the same as above but it also with the results sets the field 'activate' in each row to 'yes'
i tried that code :
select * from users where time_of_post is like '07/**/2011'
** = anyday but i can`t implement it right .
Thank you a lot for your help in advance .
select *
from users
where datediff(mm, time_of_post, getdate()) <= 6
What you want to use is the DATEDIFF() function, and get the difference between today's day (GETDATE()) and the stored dates (time_of_post). If that is less than or equal to 6 months (as denoted by the first parameter, mm) then that should be what you're looking for.
EDIT:
If you're looking to use this logic for an UPDATE, you'd do something like this:
update users
set activation = 'yes'
where datediff(mm, time_of_post, getdate()) <= 6
and activation <> 'yes'

SQL Date Subtraction Between Two Years

I am having an issue with a query I am trying to convert from MS Access. The query flags record for removal when it is older than 90 days but when I convert this query to sql server is is removing too many records.
UPDATE DT.SM_T_CountTotals
SET IsActive = 0
WHERE Convert(varchar, DT.SM_T_CountTotals.PostDate, 101) <
Convert(varchar, GetDate()- 90, 101)
When I run this query in MS Access I get a total of 3793 records that are flagged but in SQL server I get 69061 records that are flagged for removal. The GetDate()-90 value is correct at 10/26/2010 but it is flagging everything from this year to be removed.
I am sure it is something easy that I am overlooking. Help please?
I figured it out:
UPDATE DT.SM_T_CountTotals
SET IsActive = 0
WHERE DT.SM_T_CountTotals.PostDate < Convert(varchar, GetDate()- 90, 101)
You're comparing VARCHAR values, not DATEs.
101 converts to MM/DD/YY, so you're comparing month, then day, then year.
You should be using 112 (yymmdd)
Calculations between two dates can be easily done in the native data type rather than convert it to string. One can (and you have) get incorrect answers from such conversions.
Use DateDiff in the where clause to get the records that are more than 90 days old.
http://msdn.microsoft.com/en-us/library/ms189794.aspx
UPDATE DT.SM_T_CountTotals
SET IsActive = 0
WHERE ABS (DATEDIFF (dd, Getdate(), DT.SM_T_CountTotals.PostDate)) > 90

What's the best way to perform math on a floored date in SQL Server?

This is related to Floor a date in SQL server, but I'm taking it one step further:
I'm writing a query on a SQL Server table and I want to get all records for the current calendar year and the previous calendar year. So, right now, I'd want a query to return all records between January 1st, 2008 and today. But come January 1st, 2010, I want it to return records no older than 2009.
Essentially, I want to floor the current date to the beginning of the year and then subtract 1.
After rummaging through some SQL Server documentation, I came up with this:
WHERE create_date >= CAST((CAST(YEAR(GETDATE()) AS int) -1) AS varchar)
but it feels kind of ugly. Is there a better way?
Why not just use the year function on create_date as well?
WHERE YEAR(create_date) >= (YEAR(GETDATE()) -1)
This assumes (as you did) that there are no records in the database greater than today's date
I would suggest assigning a variable with the date lastyear-01-01, either by making an UDF for it, or something like
DECLARE #startOfLastYear DATETIME
SET #startOfLastYear = CAST(YEAR(GETDATE()) - 1 AS VARCHAR) + '-01-01'
Then do the query:
WHERE create_date >= #startOfLastYear
Because of two reasons:
Using YEAR() or any other function
on data from tables (i.e.
YEAR(create_date)) makes indices
unusable and decreases the
performance of the query
The variable name tells exactly what it is, and reading the code is easier.

Resources