Getting above mentioned error for this mdx query :
With
member [Measures].[MTD1] as
Sum({StrToMember('[Year Month Day].[Date].&[' + FORMAT(Now(), 'yyyy') + '-' + FORMAT(Now(),'mm') + '-01' + ']')
: StrToMember('[Year Month Day].[Date].&[' + FORMAT(Now(), 'yyyy') + '-' + FORMAT(Now(),'mm') + FORMAT(Now(),'dd') + ']')}
, [Measures].[Amount])
member [Measures].[MTD2] as
Sum({StrToMember('[Year Month Day].[Date].&[' + FORMAT(Now(), 'yyyy') + '-' + FORMAT(dateadd('mm',-1,now())) + '-01' + ']')
: StrToMember('[Year Month Day].[Date].&[' + FORMAT(Now(), 'yyyy') + '-' + FORMAT(dateadd('mm',-1,now())) + FORMAT(Now(),'dd') + ']')}
, [Measures].[Amount])
member [Measures].[%MTD Growth] as
([Measures].[MTD1]-[Measures].[MTD2])/[Measures].[MTD1], FORMAT_STRING='0.00%'
Select {[Measures].[Amount],[Measures].[MTD1],[Measures].[MTD2],[Measures].[%MTD Growth]} ON COLUMNS,
[Transaction Place].Members ON ROWS
from [TXNSumCube]
Date is stored as yyyy-mm-dd
For mondrian instead of '+', use '||'.
Related
I am trying to convert the time format from YYYYMMDD-HHMM and YYYYMMDD to mm/dd/yyyy hh:mm. My issue is that the data is not consistent. Below is some exemple:
20200814-1230
20200814-1230
20200814
20200814
I tried to use:
CONVERT(datetime,LEFT(id,8),120) + ' ' +
LEFT(RIGHT(ID , 6) ,2) + ':' +
SUBSTRING(RIGHT(ID , 6) , 3,2),
but the issue is that some data does not have time.
You can do it like this:
declare #dt varchar(100) = '20200814-1230'
select
substring(#dt, 5,2) + '/' +
substring(#dt, 7,2) + '/' +
substring(#dt, 1,4) +
case
when len(#dt) > 8 then
' ' +
substring(#dt, 10,2) + ':' +
substring(#dt, 12,2)
else ''
end
I have to deal with the following columns:
StartTime (nvarchar(10), null)
StartDate (nvarchar(10), null)
StartTime contains the Time as following: hhmmss
StartDate contains the Date as following: yyyymmdd (m=month)
To concatenate and convert StartDate and StartTime to a datetime i use the following code inside a stored procedure:
TRY_CAST(SUBSTRING(xy.StartDate,7,2) + '.' + SUBSTRING(xy.StartDate,5,2) + '.' + SUBSTRING(xy.StartDate,1,4) + ' ' + SUBSTRING(xy.StartTime,1,2) + ':' + SUBSTRING(xy.StartTime,3,2) + ':' + SUBSTRING(xy.StartTime,5,2) as datetime) as FULLSTARTDATE
Now imagine these values for two cases:
Case 1:
StartTime = 000000
StartDate = 20191201
FULLSTARTDATE = 2019-01-12 00:00:00.000
Case 2:
StartTime = 000000
StartDate = 20191220
FULLSTARTDATE = NULL
Why does Case 2 not output 2019-20-12? It may be a trivial issue but i can't seem to figure it out for almost an hour now.
TSQL DATETIME ISO 8601
select
TRY_CAST(SUBSTRING(xy.StartDate,1,4) + '-' + SUBSTRING(xy.StartDate,5,2) + '-' + SUBSTRING(xy.StartDate,7,2)+ 'T' + SUBSTRING(xy.StartTime,1,2) + ':' + SUBSTRING(xy.StartTime,3,2) + ':' + SUBSTRING(xy.StartTime,5,2)+'Z' as datetime) as FULLSTARTDATE,
try_cast(StartDate + ' ' + stuff(stuff(StartTime, 5, 0, ':'), 3, 0, ':') as datetime)
from
(values('20191201', '000000' ), ('20191220', '000000')) as xy(StartDate, StartTime);
It seems to me, that you are composing a date/time string in the form "DD.MM.YYYY HH:MM:SS", which seems to get interpreted as "MM.DD.YYYY HH:MM:SS" instead.
I see two possible solutions (but there might be more):
1: use "YYYY.MM.DD HH:MM:SS", which cannot be interpreted "incorrectly":
TRY_CAST(
SUBSTRING(xy.StartDate,1,4) + '.' +
SUBSTRING(xy.StartDate,5,2) + '.' +
SUBSTRING(xy.StartDate,7,2) + ' ' +
SUBSTRING(xy.StartTime,1,2) + ':' +
SUBSTRING(xy.StartTime,3,2) + ':' +
SUBSTRING(xy.StartTime,5,2) as datetime) as FULLSTARTDATE
2: use CONVERT instead of TRY_CAST:
CONVERT(DATETIME, xy.StartDate, 104) +
CONVERT(DATETIME,
SUBSTRING(xy.StartTime,1,2) + ':' +
SUBSTRING(xy.StartTime,3,2) + ':' +
SUBSTRING(xy.StartTime,5,2), 108) as FULLSTARTDATE
I'm struggling with an Access database:
I have a large database with the headers TAG, ZST, KL and R1H00 to R1H23 (24 of them, each one stands for one hour of a day) and I need to get a specific dataset out of it:
SELECT TAG, ZST, (R1H00 + R1H01 + R1H02 + R1H03 + R1H04 + R1H05 + R1H06 + R1H07 + R1H08 + R1H09 + R1H10 + R1H11 + R1H12 + R1H13 + R1H14 + R1H15 + R1H16 + R1H17 + R1H18 + R1H19 + R1H20 + R1H21 + R1H22 + R1H23) AS TOTAL
FROM Klassendaten
WHERE KL = "SWISS7_PW"
So far so good, but the result contains many items with the same ID (ZST). I need to sum all entries with the same ZST, but I couldn't manage to do it so far. (I tried to use the GROUP BY statement, but that only results in errors)
Any experienced SQL people here that could help me with this?
use group by
SELECT TAG, ZST, sum(R1H00 + R1H01 + R1H02 + R1H03 + R1H04 + R1H05 + R1H06 + R1H07 + R1H08 + R1H09 + R1H10 + R1H11 + R1H12 + R1H13 + R1H14 + R1H15 + R1H16 + R1H17 + R1H18 + R1H19 + R1H20 + R1H21 + R1H22 + R1H23) AS TOTAL
FROM Klassendaten
WHERE KL = "SWISS7_PW"
GROUP BY TAG, ZST;
Use the Sum() function
SELECT TAG, ZST, Sum(R1H00 + R1H01 + R1H02 + R1H03 + R1H04 + R1H05 + R1H06 + R1H07 + R1H08 + R1H09 + R1H10 + R1H11 + R1H12 + R1H13 + R1H14 + R1H15 + R1H16 + R1H17 + R1H18 + R1H19 + R1H20 + R1H21 + R1H22 + R1H23) AS TOTAL
FROM Klassendaten
where ZST = '(Whatever it is)'
group by tag, zst
is it possible to convert this date time string
'20150819130706'
to something like this
'2015-08-19 13:07:06'
Here is one option:
DECLARE #DatetimeString char(14) = '20150819130706'
SELECT LEFT(#DatetimeString, 4) + '-' +
SUBSTRING(#DatetimeString, 5, 2) + '-' +
SUBSTRING(#DatetimeString, 7, 2) + ' ' +
SUBSTRING(#DatetimeString, 9, 2) +':' +
SUBSTRING(#DatetimeString, 11, 2) +':' +
RIGHT(#DatetimeString, 2)
If you want an actual datetime value, you can simply cast the entire thing to datetime:
SELECT CAST( LEFT(#DatetimeString, 4) + '-' +
SUBSTRING(#DatetimeString, 5, 2) + '-' +
SUBSTRING(#DatetimeString, 7, 2) + 'T' +
SUBSTRING(#DatetimeString, 9, 2) +':' +
SUBSTRING(#DatetimeString, 11, 2) +':' +
RIGHT(#DatetimeString, 2) As datetime)
Note: for converting to datetime you need to change the ' ' to 'T', see Lad2025's comment to this answer.
Select Convert(varchar(19),
cast(Substring('20150819130706', 1,8)
+ ' ' + Substring('20150819130706',9,2)
+ ':' + Substring('20150819130706',11,2)
+ ':' + Substring('20150819130706',13,2) as datetime),121);
Use this query.
My table has single column info
Info
+aname + + + + + + +ano+ + + + + + + + + +agender+ + + + + + + + + + + + +arace
I should get output like
aname+ano+agender+arace
I have to eliminate multiple delimiters and replace with single +
I tried with regexp_replace and trim and working as below
select trim(REGEXP_REPLACE('+aname + + + + + + +
+ano + + + + + + + +
+agender+ + + + + + + + + + +
+arace', '\ + + ', '+'),'+') from dual;
i get output as
aname+++++++ano+++++++agender++++++++++arace
This regexp does the trick: '\++'
select REGEXP_REPLACE('+aname++++++ano+++++++++agender++++++++++++arace', '\++', '+') from dual;
To get rid of the leading + (like in your example), use ltrim (or trim to remove trailing + too).
select ltrim(REGEXP_REPLACE('+aname++++++ano+++++++++agender++++++++++++arace', '\++', '+'),'+') from dual;