I'm working on a calculated measure in SQL Server Analysis Service. the case is as follows;
we have a string value that holds a 4 digit numeric value. if the value starts with 7 we want to add a value with another value, if not then we just want to display the value.
i tried to use a when statement first, with a then and an else portion;
Case
when (Left([Kostensoort].[Kosten code], 1) is "7")
Then [Measures].[Inkoop bedrag] + [Measures].[Bank bedrag]
Else [Measures].[Inkoop bedrag]
End
this doesn't seem to work.
i then changed it to an if statement like this;
if (Left([Kostensoort].[Kosten code], 1) is "7")
then [Measures].[Inkoop bedrag] + [Measures].[Bank bedrag]
else [Measures].[Inkoop bedrag]
end if
it keeps saying there is an error at the then keyword, which leads me to believe i did something wrong with the if statement
i also replaced the is with a '=' to see if that mattered, which it didn't
i found multiple sources on the internet saying what i can and can't do in a mdx expression;
http://msdn.microsoft.com/en-us/library/ms145517.aspx
http://www.iccube.com/support/documentation/mdx/functions.html
http://publib.boulder.ibm.com/infocenter/comphelp/v101v121/index.jsp?topic=/com.ibm.xlf121.aix.doc/proguide/ifclause.html
http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/index.jsp?topic=%2Fcom.ibm.dwe.cubemdx.doc%2Fmdx_expressions.html
but none seem to solve my problem.
obvouisly im missing something, is there anyone there that has expercience with MDX and SSAS, that can give me a few pointers or a direction?
In MDX you should use the IIF function; there's no IF / THEN / ELSE that I know of.
[Measures].[Your Measure] = [Measures].[Inkoop bedrag] + Iif(Left([Kostensoort].[Kosten code], 1) = "7", [Measures].[Bank bedrag], null)
Related
Based on below formula, [Yesterday] variable is holding last date value but at run time it is not returning any result.
What should be the right syntax here?
=Sum([Total Amount]) Where ([Booking Date] = [Yesterday])
Having all the code would be helpful, but with the info we have I would recommend removing the brackets and leaving the select like this:
where "BookingDate" = Yesterday;
I have a query with some case statements within it and one of them determines how much an account is to be paid and right now everything is fine but I was hoping to do formatting for the results, how it is displayed in SQL. I am using SQL Server Management Studio (SQL Server 2016).
I am trying to use '$' + Format(col_name, '#,###') to display results as currency but no decimals. The picture shows you what I currently have after I run my query.
When I tried to incorporate the above format, I get Msg 402 Error The data types nvarchar and varchar are incompatible in the subtract operator.
Not sure how to modify my codes.
CASE
WHEN (prior.amountpaid - prior.amountOwed) < 0 THEN p.amountpaid
ELSE
prior.amountpaid - prior.amountOwed + p.amountpaid
END AS 'Amount Paid',
Wrap the FORMAT function around the entire CASE expression, instead of doing it on the individual columns.
Format the Result not each column
CASE
WHEN (prior.amountpaid - prior.amountOwed) < 0 THEN '$' + Format(p.amountpaid, '#,###')
ELSE
'$' + Format((prior.amountpaid - prior.amountOwed + p.amountpaid), '#,###')
END AS 'Amount Paid'
But if you can leave the formatting to the Front End not the database call, you never know what math you may want to perform in the Business logic and working with strings is a pain.
Trying to use a Case Statement for the first time, which is why the code is so small/simple. Returning a syntax error, supposedly with my table name [Impact].
Select Case [Impact]
Case Is = 0
[New Impact] = "1"
End Select
Any assistance is appreciated. I've looked around for solutions, but most of the time answers are related to something else in their code, and not anything I have in this small test code.
In MS Access, the logic one would normally used is:
Select iif([Impact] = 0, "1", NULL) as [New Impact]
You could use switch() as well, but that seems like overkill.
The Microsoft Access Case statement can only be used in VBA code.
The structure is like so:
Select Case test_expression
Case condition_1
result_1
Case condition_n
result_n
[Case Else
result_else]
End Select
My guess, is you missed the part about using this through VBA only.
SELECT
name,
type,
(case when to_char(expiresdate)>to_char(now())
then active
else
expired end
) as 'membership status',
expiresdate
FROM
membership
I got an syntax error on this sql but I can't figure out where I did wrong
Try this:
SELECT
name,
type,
case when to_char(expiresdate)>(now())
then active else
expired end as 'membership status',
expiresdate
FROM membership
My best guess is that it's from using single quotes for the expression alias. Try "membership status" or [membership status]
(case when to_char(expiresdate)>(now())
then active
else expired end)
as [membership status]
(Broken into individual lines for better clarity)
An explanation: the single quotes make 'membership status' into a string literal. That can't be used as an alias. Aliases can be a single word (beginning with a letter), a series of words in double quotes, or a series of words in square brackets.
So all these would work:
... as membershipstatus
... as [membership status]
... as "membership status"
This may not be the only problem with your SQL statement - others have pointed out that you're doing weird things with dates and strings - but I believe this is why you're getting a syntax error specifically, which is what you asked.
I'm using a Derived Column Task to change column data using a CASE WHEN statement. However, I need to be able to say..
SQL CODE WOULD BE:
CASE WHEN Column01 LIKE '%i%' THEN '0' ELSE '1' END
In SSIS Expression Language that would be:
[Column01] == "i" ? "0" : "1" (that's for equals i, not, LIKE %i%.
Is it possible to use a LIKE operator?
I know it is an old question, but these days I found a good answer on web.
If you want a expression for Contains like '%value%' you could use :
FINDSTRING(col, "value", 1) > 0`
If you want a expression for Start with like 'value%' you could use :
FINDSTRING(col, "value", 1) == 1
And finally, if you want a expression for End with like '%value' you could use :
REVERSE(LEFT(REVERSE(col), X)) == "value"
More details look this useful resource : Basic SSIS Equivalents to T-SQL's LIKE
I believe you'll want to use the FINDSTRING function.
FINDSTRING(character_expression, searchstring, occurrence)
...
FINDSTRING returns null if either character_expression or searchstring
are null.