In T-SQL I can retrieve ISO week number using dateprat function:
select datepart(iso_week, sysdatetime())
Is there an equivalent in MDX? I have troubles to find it.
I can retrieve number of week in MDX e.g. like this:
with member Measures.Test as (Format(now(), "yyyy") + cstr(datepart("ww", now())))
select Measures.Test on columns
from MyCube
But how about iso_week?
Thanks,
Petr
Please try below:
WITH
MEMBER [Measures].[Iso_week] AS Datepart("ww",NOW()-WeekDay(NOW(),2)+4,2,2)
SELECT [Measures].[Iso_week] ON 0
FROM [Some_cube]
More help here: http://www.snb-vba.eu/VBA_ISO_weeknummer_en.html
Referring the comment
I need to write a MDX query for a report, containing all weeks until the current ISO week
You can try the below:
with member
Measures.CurrentWeek as cstr(datepart("ww", now()))
member
WeekValue AS [Date].[Week].CURRENTMEMBER.MEMBER_VALUE
select
Measures.dummymeasures
ON 0,
[Date].[Week].[Week].MEMBERS HAVING WeekValue <= Measures.CurrentWeek
ON 1
FROM [YourCube]
WHERE ([Date].[Year].&[2016])
Related
Using SQL Server: I am trying to find all records prior to the first day of the current month and set this as a parameter in an SSRS report (so I can't use a static value).
So, I need all records prior to the first day of the each current month going forward in column CREATEDDATETIME ('yyyy-mm-dd').
I have seen a lot of threads on how to find records for a specific month and various other searches but none specifically related to the above. Interested to see if the EOMONTH function will be of use here.
Thanks for the help and advice.
Here is an expression to use EOMONTH() function with optional parameter -1.
Explanations:
DateAdd: add 1 day to expression
getdate is current date
EOMONTH is end day of a given month; however, if you put an optional integer -1, this would mean last month
Thus: first day of current month is add one day to end of day last month
SELECT DATEADD(DAY,1,EOMONTH(getdate(),-1));
Result: 2018-04-01
SO in your query:
select *
from table
where CREATEDDATETIME < DATEADD(DAY,1,EOMONTH(getdate(),-1));
I would use datefromparts():
select t.*
from t
where CREATEDDATETIME < datefromparts(year(getdate()), month(getdate()), 1);
There's already two other answers that work, but for completeness here is a third common technique:
SELECT *
FROM [table]
WHERE CREATEDDATETIME < dateadd(month, datediff(month,0, current_timestamp), 0)
You might also get answers suggesting you build the date using strings. Don't do that. It's both the least efficient and most error prone option you could use.
all three answers are great, how ever you may find that it will select all the data prior to the 1st day of the current month until the 1st Createdate. This could cause the report to take forever to run, Maybe building in a limitation to the code I would use something like this to build a report that gives details for last month only.
Select [columns]
from [source]
where [Createdate] between
/*First day of last Month*/
DATEADD(mm, DATEDIFF(mm, 0, Getdate())-1, 0 and
/*First day of this Month*/
dateadd(mm,datediff(mm,0,Getdate()),0)
I am working in SQL Server 2012.
I am trying to collect all data for the current month (2015-07) and group them. When I run this query it selects only the current day.
SELECT
YEAR, MONTH, IDWHSE, IDLOCATION, IDCUST,
SUM(PALLETDAYS) AS PALLETDAYS,
COUNT(*) AS LOCATIONDAYS
FROM
[METRICS].[dbo].[DailyData]
WHERE
DATE = CONVERT(date, DATEADD(MM, 0, GETDATE()))
GROUP BY
YEAR, MONTH, IDWHSE, IDLOCATION, IDCUST, PALLETDAYS
Thanks in advance
Gerry
Try this:
SELECT
YEAR, MONTH, IDWHSE, IDLOCATION, IDCUST,
SUM(PALLETDAYS) AS PALLETDAYS,
COUNT(*) AS LOCATIONDAYS
FROM
[METRICS].[dbo].[DailyData]
WHERE
MONTH(DATE) = MONTH(SYSDATETIME())
GROUP BY
YEAR, MONTH, IDWHSE, IDLOCATION, IDCUST, PALLETDAYS
By using the MONTH() function, you get the month number - both of your column Date (which is a really horribly bad name for a column, since DATE is also a reserved keyword for a datatype in SQL Server 2012 - try to use something more meaningful than just Date!) and the current date (I prefer the SYSDATETIME() function over GETDATE())
I am using Hive (which is similar to SQL, but the syntax can be little different for the SQL users). I have looked at the other stackoverflow, but they seems to be in the SQL with different syntax.
I am trying to the get the first day of the month through this query. This one gives me today's day. For example, if today is 2015-04-30, then result would be 2015-04-01. Thanks!
select
cust_id,
FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd') as first_day_of_month_transaction
--DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) as first_day_of_month_transaction --SQL format. Not compatible in Hive.
from
customers;
Try this
date_format(current_date,'yyyy-MM-01')
To get the first day of the month, you can use:
date_add(<date>,
1 - day(<date>) )
Applied to your expression:
date_add(FROM_UNIXTIME(UNIX_TIMESTAMP(), 'yyyy-MM-dd'),
1 - day(FROM_UNIXTIME(UNIX_TIMESTAMP(), 'yyyy-MM-dd'))
)
But this will work for any column in the right format.
SELECT TRUNC(rpt.statement_date,'MM') will give you the first day of month.
You can use this query to calculate the First day of the current month
Select date_add(last_day(add_months(current_date, -1)),1);
Instead of current_date, you can use the date field name.
last_day => Gives the last day of current month
add_months with -1 => Gives previous month
date_add with 1 => Add one day
Another way - select concat(substring(current_date,1,7),'-01');
I have a database which has information about clinics.
Each clinic has an opening date(say, 2014-05-23 00:00:00) and a field called MIO(monthsInOperation), which basically is the number of months the clinic has operated.
For each clinic I need the Calendar Month and Year in the format (say, July-13) based on the current month of operation(MIO), which in the database can be 0,1,2,....and so on.
I know I need to use DATEADD() or DATEPART() or DATEDIFF() function. But I am unable to get the desired result.
The answer for the last Clinic should be Feb-2012
Correct me if I'm misunderstanding your question, but is there a reason you can't just do something like this?
SELECT DATEADD(MONTH, MonthsInOperation, OpenDate) FROM Clinics
That won't give you just a month and year, but there's no such type in SQL anyway. You can handle the resulting DATE or DATETIME on your client's code and ToString it to be in the format (MM-yyyy or what have you) that you want it in. You could also separate them out using the MONTH(DATE) and YEAR(DATE) functions, although I wouldn't bother, personally. It seems like more a UI matter that you want only to see the month and year.
SELECT MONTH(DATEADD(MONTH, MonthsInOperation, OpenDate)), YEAR(DATEADD(MONTH, MonthsInOperation, OpenDate))
It sounds like that will output the two columns you need to work with: the month and year.
However,
SELECT DATENAME(MONTH, DATEADD(month, MonthsInOperation, OpenDate)) + "-" + YEAR(DATEADD(MONTH, MonthsInOperation, OpenDate)))
should give exactly what you want, again, if I'm understanding you properly.
If you need a list of the months when each clinic is open, you can do that. The first thing you need is a list of numbers, the rest is pretty easy.
with n as (
select 0 as n
union all
select n + 1
from n
where n < 50
)
select c.*, year(dateadd(month, n.n, c.opening_date)),
month(dateadd(month, n.n, c.opening_date))
from clinics c join
n
on n.n <= c.mio;
I've chosen to format the year and month as two separate numeric columns. You can use datename() or convert() to get a specific format.
EDIT:
If you just want the latest month, that is much simpler:
select c.*, year(dateadd(month, c.mio, c.opening_date)),
month(dateadd(month, c.mio, c.opening_date))
from clinics c;
Hi how do I get the data by current week?
Select * from Transaction where transactionDate ....
In SQL Server based on week of year. Please see DATEPART for ##DATEFIRST etc. for example, this is all trades since Sunday in US/UK settigs:
WHERE DATEPART(week, transactionDate) = DATEPART(week, GETDATE())
Edit:
For Access, use this DatePart and use "ww" for the part of date you want.
In answer to the comment, "week" is not a variable; it's the bit of the date you want
So:
WHERE DatePart("ww", transactionDate) = DatePart("ww", GETDATE())
In Microsoft Access
Last n days:
SELECT *
FROM Transaction
WHERE transactionDate >=Date()-7
If you have indexes and this type of difference suits, it will be faster because it is sargable
This week by week difference:
SELECT *
FROM Transaction
WHERE DateDiff("w",[transactionDate],Date())=0
BTW It is considered bad practice to use *
DateDiff: http://office.microsoft.com/en-us/access/ha012288111033.aspx
Simple but portable:
SELECT *
FROM Transaction
WHERE transactionDate >= ?
AND transactionDate <= ?
Calculate the two parameters in your server-side code to whatever definition of 'week' you need.
In IBM DB2
SELECT *
FROM Transaction
WHERE transactionDate BETWEEN CURRENT TIMESTAMP - 7 days AND CURRENT TIMESTAMP;
In Access, if you want to run a query to find records that fall in the current week, use
SELECT *
FROM table
WHERE table.DateField Between (Date()-Weekday(Date())+1) And (Date()-Weekday(Date())+7);
That runs Sunday through Saturday. Use +2 and +6 instead if you want the workweek.
mySQL (standard date stamp)
SELECT *
FROM Transaction
WHERE WEEK(NOW())=WEEK(transactionDate);
mySQL (unix timestamp stamp)
SELECT *
FROM Transaction
WHERE WEEK(NOW())=WEEK(FROM_UNIXTIME(transactionDate));
Bit of a unoptimized query. Could be a more efficient way.
Note: This isn't a rolling 7 days. Just the current week of the year.
EDIT: Sorry I didn't see the ms-access tag. ignore all of this :|