In SSRS, how would I return the prior month data of a selected month.
month(lis.s_date=month(getdate()) -1 would get me the prior month data of the current month but I want it to be the prior month of any month selected in SSRS. What would I change getdate() to in order to achieve that?
The equivalent SSRS expression would be:
=DateAdd(DateInterval.Month, -1, Now())
Edit after comment:
You can substitute any date value into the T-SQL expression, e.g a few ways here:
create table dates (dateValue date)
insert into dates select '01-jan-2013'
insert into dates select '01-feb-2013'
insert into dates select '15-feb-2013'
select dateValue
, lastMonthDate = dateadd(mm, -1, dateValue)
, lastMonthValue = month(dateadd(mm, -1, dateValue))
, lastMonthName = datename(mm, dateadd(mm, -1, dateValue))
from dates
This just uses a table column instead of the getdate() function.
SQL Fiddle demo.
Related
I would like to update the contents of the Date1 column to reflect the oldest date in each row, unless the date has already passed (Date1 < current date), in which case i'd like Date1 to be populated with the 2nd oldest date in the row.
ID
Date 1
Date 2
Date 3
Date 4
001
01/14/2022
01/14/2022
01/15/2022
01/16/2022
002
04/15/2019
04/15/2019
01/10/2021
01/10/2021
I am currently using
update mytable t
set date1 = (
select min(date)
from (values (date2), (date3), (date4)) d(dt)
where dt >= current_date
)
The only problem I run into is when all available dates are prior to the current date. In this case it overwrites the value in the date1 column with null, which is not ideal. I'd like the query to leave the date1 field intact in these instances.
Figured it out:
update mytable t
set date1 = coalesce ((
select min(date)
from (values (date2), (date3), (date4)) d(dt)
where dt >= current_date
), date 1);
I have a select statement that returns the following:
SELECT
StartDate
,EndDate
FROM
MyTable
And the result returns the following data:
What I want is change the time portion of StartDate using the value from EndDate. So that only the time portion of both dates is the same, taken from EndDate. I have to keep identical, down to milliseconds.
Is it possible using DATEADD function?
Using DateAdd and DateDiff:
SELECT DATEADD(DAY, DATEDIFF(DAY, EndDate, startDate), EndDate) As StartDate,
EndDate
FROM MyTable
I'm adding the days difference between the start date and the end date.
Since the DateDiff have the later date before the earlier date, it returns the days difference as a negative number.
Another option that I'm guessing some people will find more readable is this:
SELECT DATEADD(DAY, -DATEDIFF(DAY, startDate, EndDate), EndDate) As StartDate,
EndDate
FROM MyTable
My table:
create table example
(
code varchar(7),
date date,
CONSTRAINT pk_date PRIMARY KEY (code)
);
Dates:
insert into example(code, date)
values('001','2016/05/12');
insert into example(code, date)
values('002','2016/04/11');
insert into example(code, date)
values('003','2017/02/03');
My problem: how to select the previous dates to six month from today ?
In MySQL I can use PERIOD_DIFF,but, in PostgreSQL?
You can try INTERVAL instruction :
SELECT date
FROM example
WHERE date < CURRENT_DATE + INTERVAL '6 months'
AND date > CURRENT_DATE;
You will get the dates from today to six months.
I want to just select data for the current week. So...
If the current date is a Monday just select Monday
If the current date is a Tuesday select Monday and Tuesday's data
If the current date is Wednesday select Monday, Tuesday and Wednesday
...and so on. I want it to reset on Sunday and I believe it's some kind of "where" clause just don't know what. As you can see below I'm just counting the number of pieces into the oven and want it to accumulate as the week goes on and then reset on Sunday.
select
count(*) as PiecesIntoOven
from ovenfeederfloat
where...??
Thanks for the help.
If you're looking to do this in Sql Server, see below. Essentially this converts the current date to its numeric (0-6) value, then finds the 0th date for that week and uses it to set the lower bound of the where clause.
select sum(numberofpieces)
from Test
where dateofwork <= getdate()
and dateofwork >= (DATEADD(DAY, DATEPART(WEEKDAY,getdate()) * -1, getdate()) + 1)
Note that the '0' value is impacted by DATEFIRST. https://stackoverflow.com/a/1113891/4824030
I'm not certain how to do this in Oracle. Something like the below should work, but it's being finicky in sqlfiddle.
select sum(numberofpieces)
from Test
where dateofwork <= current_timestamp
and dateofwork >= (((to_char(level+trunc(current_timestamp,'D'),'Day') * -1) + current_timestamp) + 1)
I would like to create a bar chart displaying the number of objects that were a available on a monthly base. All rows have a start and end date. I know how to do the count for a single month:
SELECT COUNT(*) As NumberOfItems
FROM Items
WHERE DATEPART(MONTH, Items.StartDate) <= #monthNumber
AND DATEPART(MONTH, Items.EndDate) >= #monthNumber
Now I would like do create the SQL to get the month number and the number of items using a single SELECT statement.
Is there any elegant way of accomplishing this? I am aware I have to take the year number into account.
Assuming Sql Server 2005 or newer.
CTE part will return month numbers spanning years between #startDate and #endDate. Main body joins month numbers with items performing the same conversion on Items.StartDate and Items.EndDate.
; with months (month) as (
select datediff (m, 0, #startDate)
union all
select month + 1
from months
where month < datediff (m, 0, #endDate)
)
select year (Items.StartDate) Year,
month (Items.StartDate) Month,
count (*) NumberOfItems
from months
inner join Items
on datediff (m, 0, Items.StartDate) <= months.month
and datediff (m, 0, Items.EndDate) >= months.month
group by
year (Items.StartDate),
month (Items.StartDate)
Note: if you intend to span more than hundred months you will need option (maxrecursion 0) at the end of query.