SQL Between Two Dates Missing End Date (SSRS) - sql

I'm using SSRS 2008r2 to generate reports. Using following WHERE statement in SQL query
WHERE (NonPMJobs.npmJobCreateDate >= #Created_Parameter) AND
(NonPMJobs.npmJobCreateDate <= #Created_Parameter2)
I'm using parameters with the data type of DateTime. Users then select day from a calendar. I want to get results where jobs have been created between date 1 (#Created_Parameter) AND date 2 (#Created_Parameter2) INCLUSIVE.
But results being returned do not include the second date (#Created_Parameter2). If I select 01/07/2013 - 05/07/2013 I get 01, 02, 03, 04 but no 05. If I select 01/07/2013 - 06/07/2013 I get 01, 02, 03, 04, 05.
I've tried using:
WHERE (NonPMJobs.npmJobCreateDate BETWEEN #Created_Parameter AND #Created_Parameter2)
but get same results.
What am I missing here and why isn't WHERE statement inclusive? Any pointers would be very much appreciated.

Well, you need to think about this: a DATETIME like this: 05/07/2013 means the 5th of July (or 7th of May - depending on your locale) at midnight when the day starts (a 00:00 hours, so to speak).
So this does NOT include the events that happen on that day!
The best solution would be to use
WHERE (NonPMJobs.npmJobCreateDate >= #Created_Parameter) AND
(NonPMJobs.npmJobCreateDate < DATEADD(DAY, 1, #Created_Parameter2))
and basically getting everything that's smaller than the next day based on #Created_Parameter2. So for your 5th of July, that would be anything before the 6th of July - which includes all events from the 5th of July.

Related

After certain week of 2022 and continue with this new year

I would like to request some advice about how to set a Where Condition, but after a certain week
What I mean is:
I have dirty data before a specific week of 2022, so I made this:
DATEPART(WK, SA.FECHAE) >= 44
AND
YEAR(SA.FECHAE) >= 2022
But, We're on 2023, so, I need to add the new information of this new year year too into the query.
The query result shows me until 12-31-2022 and need it until today after the week 44 of 2022
...
WHERE (
DATEPART(WEEK, SA.FECHAE) >= 44
AND YEAR(SA.FECHAE) = 2022
)
OR (
YEAR(SA.FECHAE) >= 2023
)
In the OPs question they ask how to add an additional date range to their WHERE clause. The addition of this OR allows a second date range (in this case anything where the year is greater than or equal to 2023) to match the predicate and be returned, without impacting the original.
Plain English definition of the amended where clause:
Week 44 of 2022, or any week of any year from 2023 forward.

Getting the right date in sql

I'm executing the next query in sql server 2012.
select *
from table
where date > convert(date, '2015/02/12')
order by date asc
but I'm getting the next set:
2015-02-12 06:40:42.000
2015-02-12 06:45:44.000
2015-02-12 06:48:15.000
2015-02-12 07:06:28.000
2015-02-12 07:26:46.000
...
I can fix this by changing the date to '2015/02/13', but I have the doubt about this behavior, why am I getting dates from feb 12 when I am specifying that I need only later dates ?. I also tried using cast('2015/02/12' as date), but I could not have the answer I was looking for
Because dates without times are intepreted as 12:00 midnight on that date. If you want only dates after February 12, 2015, then select all dates greater than or equal to February 13, 2015 (which again will be interpreted as midnight on February 13th).
select *
from table
where date >= convert(date, '2015/02/13')
order by date asc
why am I getting dates from feb 12 when I am specifying that I need only later dates ?
You're not specifying that you need only dates after Feb 12. You're asking for every row in which the value in the "date" column is greater than '2015-02-12'.
The value '2015-02-12 06:40:42.000' is greater than '2015-02-12'.
When comparing the date 2015/02/12 with your datetime data, this will implicitly compare the converted date 2015-02-12 00:00:000, the date at the beginning of the day with all of your data in column date.
But you are actually comparing datetime data, which has a time part as well, which gets compared.
And because you're comparing the beginning of the day (2015-02-12 00:00:000) with a value which is after it, for example 2015-02-12 06:40:42, all of the dates from will be displayed, because 6:40 AM is after (greater than) 0:00 AM.
Try this:
SELECT *
FROM TABLE
WHERE DATE >= DATEADD(SECOND, -1, '2015/02/13')
jarlh is right, though I'll clarify a little. Each of the "dates" you show above fall after 12:00 midnight starting 2015-02-12. They are actually timestamps.
If you don't want to see anything for the day specified in the filter, you add a day and use the greater-than-or-equal-to (>=) operator.
SELECT *
FROM table
WHERE (date >= DATEADD(d, 1, CONVERT(date, '2015/02/12')))
ORDER BY date ASC

Convert date to first of current month if date is previous to current month

I have a query with an IIF() expression in one column that I am using to identify if a date is previous to the current month to then amend it if so. So if I run the query on 19th March 2014 and the EffFrom date is before 1st March 2014, I would want that column entry to now appear as 1st March 2014.
I am using the below expression which is pretty much doing what I want, however I know it is not considering the year -- i.e. it is changing an entry of 1st Jan 2015 to be 1st March 2014.
EffFrom:
IIf(Month([Table.Efffrom])"Less than symbol"Month(Date()),Date()-Day(Date())+1,[Table.Efffrom])
Can someone correct the expression for me?
I interpreted "if I run the query on 19th March 2014 and the efffrom date is before 1st March 2014 I would want that column entry to now appear as 1st March 2014" to mean you want something like this from a query run today (Mar 19th 2014):
id Efffrom adjusted_date
1 1/1/2014 3/1/2014
2 3/1/2014 3/1/2014
3 3/31/2014 3/31/2014
4 1/1/2015 1/1/2015
If that is correct, your IIf expression can use DateSerial to check whether Efffrom is before the first of this month, and transform the older dates.
SELECT
y.id,
y.Efffrom,
IIf
(
y.Efffrom < DateSerial(Year(Date()), Month(Date()), 1),
DateSerial(Year(Date()), Month(Date()), 1),
y.Efffrom
) AS adjusted_date
FROM YourTable AS y;
Here's what i do to get the first day of the next month
EffFrom: DateAdd("m",1,CDate(Format([Table.Efffrom],"m/\1/yy")))

Getting the last month date's data in sql

I have the below query to get the last month date's data. Suppose if today is 23rd Dec, then it fetches 23rd Nov data. But when it's 31st Dec, then there would be no 31st Nov. In that case it should fetch 30th Nov data.
So, I want to decrease one day and check the condition again and should fetch the data. In this scenario, suppose on March 31st, first that query even should check for feb 31st, then it should check for feb 30, then 29th and so on until the valid date of that particular month.
My current query:
select *
from dataTable
where date(datecolumn)=date(add_months(DATE(sysdate-1) ,-1));
So, this only fetches last month's date data. So, can someone please suggest me how to check for the mentioned validation in the query and fetch the data?
try this:
SELECT dateadd(mm, -1, Convert(Datetime, '2013-12-31')

SQL select maximum from two time periods

I have a query, Im trying to select the maximum value from the summer period (nov-april down here) but it only gives me values from nov-dec with this query. Any ideas why?
SELECT TOP 10 Value, DateTime
FROM history
WHERE Tagname = #Tag
AND
((DateTime >= #StartYear AND DateTime < #StartWinter)
OR
(DateTime >= #FinishWinter AND DateTime < #FinishYear))
ORDER BY Value DESC
(DateTime >= startYear AND datetime < startwinter) gives you all results between jan and april 2009.
(Datetime > finishwinter and datetime < finishyear) gives all results in nov dec 09.
So, you're selecting top 10 from Jan Feb March April Nov Dec 2009. If that's what you want to select from, and you're only getting values in Nov Dec 2009, check to see that there should be values in the other months?
If #startwinter isn't year-sensitive you might also get jan-apr 2010.
Shouldn't you use a 'ORDER BY' when using 'TOP 10'?
And what locale do you live in, or, to rephrase it: what are reasonable dates for (#StartYear, #StartWinter, #FinishWinter, #FinishYear)
In Europe I expect:
StartYear = 2010-01-01
FinishYear= 2010-12-31
StartWinter=2010-12-20 (about)
FinishWinter=2010-03-20 (about)
So the first period would go from 2010-01-01 to 2010-12-20 (about) and the second from March 2010 to End of year.
So this would include the whole year, and most of it, from 03.20 to 12.20 double.
Hey thanks for the help everyone, it seems this is a problem with our historian (a linked db from sql server) so Ill take the issue up with them. I tried the query on a regular mssql db and it seeems fine...