SSRS 2008 R2 Change First Day of the Week to Monday - tsql

This report aggregates data from the top by week into days using a drill down. Now, I'm just extracting the date parts from the date so I can group on the tables, and it works beautifully however, the first day of the week is set to 7, or sunday and I need it to start at 1, or monday. The obvious solution I've read is to SET DATEFIRST 1 - incidentally, when I try to use SET, I get an error message saying that it is not supported
I've read you can change the global settings of the report to do this - but I have no clue where and would prefer to be able to do it locally in this query if possible. Also, I've tinkered around with ##DATEFIRST but all it does, of course, is report that the first day of the week is 7.
SELECT DATENAME(week, CAST(Date AS date)) AS week, DATENAME(dw, CAST(Date AS date)) AS day, CONVERT(date, Date, 103) AS date, FROM_NUMBER, DURATION,
TYPE_OF_CALL, ID, DATEPART(dw, CAST(Date - 1 AS date)) AS dayCode
FROM CallCenterStatsCDR
WHERE (Date BETWEEN #StartDate AND #EndDate)
GROUP BY DATENAME(week, CAST(Date AS date)), CONVERT(date, Date, 103), DATENAME(dw, CAST(Date AS date)), FROM_NUMBER, DURATION, TYPE_OF_CALL, ID,
DATEPART(dw, CAST(Date - 1 AS date))
ORDER BY Date DESC

The datefirst function will impact all of the processes running on the server. Could you DateAdd(dd,1,YourDate) all of your date fields in the query to get the same result?

Related

DB2- select rows with date_column of last month

I need to select count(field) from table where the date_column is within the last month, i.e. from the first of the last month till the first of this month. As this query will be automated it will need to refer to the last month of any given day.
Also the date_column has a datatype of timestamp
Thanks in advance!
SELECT (CAST(current timestamp AS DATE)) - (DAY(CAST(current timestamp AS DATE)) - 1)DAYS FROM sysibm.sysdummy1;
SELECT (CAST(current timestamp AS DATE)) - (DAY(CAST(current timestamp AS DATE)) - 1)DAYS
- 1 MONTH
FROM sysibm.sysdummy1;
Managed to find these queries, to find the first of this month and last month

Compute the number of pick-ups and drop-offs for each hour of the day and every day of the week from DB

I need to compute the number of pick-ups and drop-offs for each hour of the day and every day of the week from the below database.
I tried to apply below query but i guess it is very basic
select date, in_service from aid_atidivyakumarpatra;
click here to see the table
This should give you the results you are looking for. There are plenty of examples out there to give you a more verbose date that shows day of the week so I did not focus on that. The answer was in the Group by statement.
SELECT Convert(DATE, date) as Day, DATEPART(HOUR, date) as Hour,
COUNT (CASE inservice WHEN 'drop' THEN 1 ELSE null END) AS DropOffs,
COUNT (CASE inservice WHEN 'pick' THEN 1 ELSE null END) AS PickUps
FROM PickupDropoff
GROUP BY Convert(DATE, date), DATEPART(HOUR, date)
I made this answer in Microsoft SQL Server, if you are using Postgres hopefully there is an equivalent.

Getting a weird result for ISO Date in Postgresql

I have a table task_details. I need to select its weekly date from this table. I have used ISO Year & ISO Week to extract the weekly dates from this table as I want to extract its week number as 53 if its Dec,2015 then on the days 28 Dec,29 Dec,30 Dec,31 Dec and 1 Jan '16,2 Jan '16 since it should not separate to two different weeks for these sort of dates. The query I have used for ISO Year & ISO Week is given below.
select
name, id,
to_date(week || ' ' || yr, 'IW IYYY') week_date,
sum(worked_hours) worked_hours_per_week,
week, yr
from (
select
name, id,
newdate, hours worked_hours,
to_number(to_char(newdate, 'IW'), '99') week,
extract( year from newdate) yr
from task_details t
) sub
where worked_hours > 0
group by name, id, to_date(week || ' ' || yr, 'IW IYYY'), week, yr
order by yr, week
It is working fine for the weeks but then I am getting this weird result for one date for a record in the table. The table doesn't have the data for the year 2017.Also, the yr column is showing 2016 which is as desired but then the newdate column and week column is giving weird result. Why is this happening? How do I fix this ?
Here is the SQL fiddle for it :http://sqlfiddle.com/#!15/53abf/1
You should not mix week with year in the extract function as year is for the Gregorian calendar rather than the special ISO calendar.
See section 9.9.1 and comments about week.
to_number(to_char(newdate, 'IW'), '99') is effectively extract(week from newdate)
Changing the yr column to be extract(isoyear from newdate) solves your problem.
Adjusted SQL Fiddle
#gwaigh already cleared up your confusion of Gregorian and ISO year.
However, your query would be simpler and faster with date_trunc() to get the first day of each week:
SELECT name, id
, date_trunc('week', newdate)::date AS week_date
, extract(week FROM newdate)::int AS week
, extract(isoyear from newdate)::int AS isoyr -- must be ISO year to match
, sum(hours) AS worked_hours_per_week
FROM task_details
WHERE hours > 0
GROUP BY name, id, week_date, week, yr
ORDER BY week_date;
Also simplified your query.
SQL Fiddle.
Either way, if you work with timestamptz then year, week or date depend on your current timezone setting. (It can be a different year, depending on where you are right now.)

How to get current month and Last month in Redshift

I am wondering if I can get last month and current month in Redshift? I looked at functions at http://docs.aws.amazon.com/redshift/latest/dg/r_CURRENT_DATE_TIME_functions.html
For current month, I guess I can do the following:
LEFT(CURRENT_DATE, 7)
For previous month, I came up with the following:
LEFT(ADD_MONTHS(CURRENT_DATE, -1), 7)
But I am wondering if there is a simpler way of doing these?
SELECT EXTRACT(month FROM CURRENT_DATE);
SELECT EXTRACT(month FROM CURRENT_DATE - '1 month'::interval);

How to get the no of Days from the difference of two dates in PostgreSQL?

select extract(day from age('2013-04-06','2013-04-04'));`
gives me the no of days ! i.e.: 2 days
but it failed when I have a differnt month:
select extract(day from age('2013-05-02','2013-04-01'));
So what I need is to get the no of days as 32 days
Subtraction seems more intuitive.
select '2013-05-02'::date - '2013-04-01'::date
Run this query to see why the result is 31 instead of 32
with dates as (
select generate_series('2013-04-01'::date, '2013-05-02'::date, '1 day')::date as end_date,
'2013-04-01'::date as start_date
)
select end_date, start_date, end_date - start_date as difference
from dates
order by end_date
The difference between today and today is zero days. Whether that matters is application-dependent. You can just add 1 if you need to.