SQL query to search - sql-server

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'

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.

SSRS date range parameter

I have a report in SSRS 2016 that contains one SP which has a date field called startdate. I'd like to create a parameter where the user can select between two ranges: startdate >='2017'or startdate < '2017'. This seems like it should be simple, but I can't see to find an easy way to do this.
I've tried to create a parameter in SSRS where I chose "Specify Values" and manually added these values but I get an error that the "expression references the parameter "startdate" which does not exist in the Parameters collection.
I'm sure I can create this by creating a stored procedure with these dates, but that seems like more work than is needed.
Does anyone know of a better way to do this?
If you are looking to have a parameter that has two options, 0 - Before 2017, and 1 - After 2017 then you should just create a Date parameter which has two options, Before 1/1/2017 in the label field with a 0 in the value field and After 1/1/17 in the label field with a 1 in the value field. Then in your report you just have to filter your data based upon the 1 or 0 value.
For example:
DECLARE #DateFilter INT = 1
IF ( #DateFilter = 0 )
BEGIN
SELECT * FROM dbo.MyTable t
WHERE t.DateFilter < '1/1/17'
END
IF ( #DateFilter = 1 )
BEGIN
SELECT * FROM dbo.MyTable t
WHERE t.DateFilter >='1/1/17'
END
I don't know why you would filter your data this way. I recommend to use Larnu's suggestion that you have two parameters, a Start Date and an End Date. Otherwise this could return a lot of rows for the second option as time goes on.
Add a couple of parameters in SSRS say, FromDate and ToDate and make its data type as Date/Time.
You can specify the default values for these parameters using DateAdd functions and samples give below:
Today:
=Today()
last week from today:
=DateAdd(DateInterval.Day, -7,Today())
You can add/reduce year, quarter month etc. in a similar way as shown below. Just change the number to the required length
=DateAdd(DateInterval.Year,-1,Today())
=DateAdd(DateInterval.Quarter,-1,Today())
=DateAdd(DateInterval.Month,-1,Today())
=DateAdd(DateInterval.DayOfYear,-1,Today())
=DateAdd(DateInterval.WeekOfYear,-1,Today())
=DateAdd(DateInterval.WeekDay,-1,Today())
=DateAdd(DateInterval.Hour,-1,Today())
=DateAdd(DateInterval.Minute,-1,Today())
=DateAdd(DateInterval.Second,-1,Today())

SSRS graph: Retrieve values from different dates in SQL query

I am using the following query to retrieve snapshot values from 4 different dates: -1 day (latest), -2 days, -3 days and -40 days (not yet implemented).
SELECT [SnapshotDate]
,[SnapshotKey]
,[info1]
,[info2]
,[info3]
FROM [Database].[dbo].[Values]
WHERE [SnapshotDate] >= DATEADD(day,-3, GETDATE())
AND [SnapshotKey] = 'Some text here'
This results in the following graph:
The query is not quite right firstly since it is showing 4 values and should only be showing 3 at this point. Secondly I would like to show the last snapshot from 40 days ago as shown in the graph.
I have tried a few different queries but have not managed to figure out how to do this properly.
[SnapshotKey] = SELECT DATEADD(day,-40,getdate())
The above query gives me the correct answer in theory. However, when I use this in my query there is no result. I believe this might be due to not having a date conversion or the fact that I'm using "day" in my query. I'm not sure.
Any suggestions?
EDIT:
I also tried using the following with no luck (no result):
CONVERT(date, [SnapshotDate]) = CONVERT(date, DATEADD(day,-40, GETDATE()))
I'm not sure what your date values are but I'm guessing this report was run on 2nd May.
If this is correct then you need to change the range to exclude where the difference in dates is zero. Personally I use DATEDIFF in situations like this as it's easier to visualise for me.
Try changing the where clause to something like this.
WHERE (DATEDIFF(day, getdate(),[SnapshotDate]) BETWEEN -3 AND -1
OR DATEDIFF(day, getdate(), [SnapshotDate]) = -40)
AND [SnapshotKey] = 'Some text here'

How to use - within like '%'% query

I have a table column in SQL 2012 called transitiontime that contains dates and times in the format 2017-02-02 21:00:34.847. I'm trying to query all results within the last 3 months, which I would think would just require me to look for 2017-02, or something to that effect. However if I leave the hyphen in, the query fails.
Examples:
WHERE transitiontime like '%2017%' <- works but returns all values from this year
WHERE transitiontime like '%2017-02%' <- Does not work at all
WHERE transitiontime like '%2017%02%' <- Works but pulls in anything with 2017 and 02 in it, even if 02 is just in the time.
I would love to get the past 3 months in one query, but right now I'd like to just be able to pull from 1 month.
I'm assuming, since you are using the keyword "like", that the field "transitiontime" is a char oder varchar field?
Change the type of the field to DateTime and use the following query, to get all results for the last three months (applying to SQL):
SELECT * FROM table WHERE transitiontime >= DATEADD(month, -3, GETDATE())

Resources