ToDate paramenter not showing the current date records in SSRS report - sql

I am using FromDate and ToDate parameter to filter the records in SSRS report. If I choose from date as 1st Oct 2020 and Todate as 15th Oct 2020, then it is showing the records till 14th Oct 2020 even though I have data for 15th Oct 2020.
The query looks like below and really appreciate your support.
SELECT * FROM table1
WHERE to_date(created_date) >= '2020-10-01'
AND to_date(created_date) <= '2020-10-15'
Thank you.
Regards,
Viresh

Related

Get the data for the full penultimate calendar month + data for the same month, but in 2 previous years

I'm working in PostgreSQL environment and I have the following problem: let's say today is April 8th, 2019 and I want to pull the full list of user IDs who opened their account in the penultimate month (so from Feb 1st till Feb 28th, 2019) + list of user IDs who opened their account in the same month, but in years 2017 and 2018 (so in Feb'18 and Feb'17).
If we assumed that today is June 25th 2019, I would want to pull the list for the following periods:
- 1st till 30th April 2019;
- 1st till 30th April 2018;
- 1st till 30th April 2017.
I have this SQL code at the moment. As you can see, I have to alter the dates in the WHERE clause every month. Can someone advise me on how I could solve this problem?
SELECT
deposit_id,
to_char(activation_date, 'yyyy-mm-dd') as placement_start_date,
FROM fixed_term_plan
WHERE
activation_date BETWEEN Date '2019-01-01' AND Date '2019-01-31' or activation_date BETWEEN Date '2018-01-01' AND Date '2018-01-31' or activation_date BETWEEN date '2017-01-01' AND Date '2017-01-31'
Hmmm. I am thinking:
SELECT deposit_id,
to_char(activation_date, 'yyyy-mm-dd') as placement_start_date,
FROM fixed_term_plan ftp
WHERE extract(month from activiation_date) = extract(month from (now() - interval '2 month'));

SQL to separate YYYY MM to fiscal year

I have a column which states month and year YYYY MM. I've separated those into two columns (Year and Month). The problem is, the year is the calendar year whereas ideally I need the fiscal year I use (Apr 01 to Mar 31 - This will never change).
Other solutions I've seen are based on date format, whereas my original column is string.
I need a statement that returns the fiscal year for my new year column instead of the calendar year.
My current statement is:
Select Month,
parsename(replace(Month,' ','.'),1) as MonthM,
parsename(replace(Month,' ','.'),2) as Year
FROM TblTrade
Which works to separate the columns.
So expected results would be for example:
Feb 15 becomes Feb and 2015.
Apr 15 becomes Apr and 2016.
Please advise.
Sql server:
declare #date datetime = getdate();
select
(YEAR(DATEADD(Month,-((DATEPART(Month,#date)+5) %12),#date))) AS Financial_Year
Assuming April is month 1
Try this
select case
when to_char(to_date(column_name,'yyyy mm'),'mm') between 01 and 03
then to_char(trunc(to_date(column_name,'yyyy mm')),'yyyy')-1
else to_number(to_char(trunc(to_date(column_name,'yyyy mm')),'yyyy')) end
fiscal_year
from table_name
I'm using oracle db
This will work when column is string and has valid data i.e date in format like yyyy mm
Since you've read those other articles (you should really mention what research you've done in your question) and you're still having problems, I've had a play for you.
If I understand correctly, you have a varchar with YYYY MM eg
2015 01
2015 02
2015 03
2015 04
etc And you want
Jan 2014
Feb 2014
Mar 2014
Apr 2015
Here goes...
Setup some test data
DROP TABLE IF EXISTS #Test;
WITH Dates AS (
SELECT CAST(GETDATE() AS DATE) AS Date
UNION ALL
SELECT DATEADD(MONTH, -1, Date) FROM Dates
WHERE Date > '20140101'
)
SELECT DISTINCT
CONVERT(VARCHAR(4), YEAR(Date)) + ' ' +RIGHT(CONVERT(VARCHAR(6), Date, 112), 2) YearMonth
INTO #Test
FROM Dates
OPTION (MAXRECURSION 0);
SELECT * FROM #Test
YearMonth
---------
2013 12
2014 01
2014 02
2014 03
2014 04
2014 05
etc
Find Fiscal Year
SELECT
LEFT(YEARMONTH, 4) Year
,RIGHT(YEARMONTH, 2) Month
,LEFT(DATENAME(MONTH , DateAdd( month , CONVERT(INT,RIGHT(YEARMONTH, 2)) , -1 )), 3) MonthName
,IIF(CONVERT(INT, RIGHT(YEARMONTH, 2)) >= 4, CONVERT(INT,LEFT(YEARMONTH, 4)), CONVERT(INT,LEFT(YEARMONTH, 4)-1 )) FiscalYear
FROM #TEST
Year Month MonthName FiscalYear
---- ----- --------- -----------
2013 12 Dec 2013
2014 01 Jan 2013
2014 02 Feb 2013
2014 03 Mar 2013
2014 04 Apr 2014
2014 05 May 2014
2014 06 Jun 2014
etc
You could put the year/month parsing in a sub query just to make the code cleaner and some of the nasty formatting could be replaced with FORMAT since you're on 2012.
Hope this is what you're after and helps.
Since you included the Tableau tag, I'll describe the Tableau approach -- which is a little different than the other answers since you tend to specify what you want to Tableau, and let its driver generate the necessary SQL for your database.
First, it will work best if you have a single field that has datatype DATE instead of separate fields for month and year.
You can then roll up dates to the nearest year, month, day etc (actually truncating to the beginning of the period) or extract specific parts of dates year, month, day etc as needed for grouping/display.
The added benefit of working with a true DATE datatype is that you can tell Tableau the beginning of your fiscal year for each data source, and it will sort dates appropriately. Just right click on a data source and set the date properties. You can also set the start of the week and the date format.

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 Between Two Dates Missing End Date (SSRS)

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.

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...