how to use CASE WHEN in access syntax error - ms-access

First post here so go easy, I am new to access and queries,
I have the below which I want to create a case to change a 1 or 0 into an on or off in a merged document,
Below is what I have tried but it shows a syntax error? Is there another better way? I have had a search but cant see a solution,
NZ((SELECT TOP 1 left(CASE chrConfigValue WHEN 'TRUE' THEN 'ON' ELSE 'OFF' END, 255) FROM dbo_*Table*_parts_config WHERE idpart= dbo_*Table*_parts.idconfig AND left(chrConfigName, 255) = 'DHCP_SERVER_STATE'),

Access doesn't support CASE, use IIF instead:
SELECT TOP 1
LEFT(IIF(chrConfigValue = 'TRUE', 'ON, 'OFF'), 255)
FROM dbo_Table_parts_config
WHERE
idpart = dbo_Table_parts.idconfig AND
LEFT(chrConfigName, 255) = 'DHCP_SERVER_STATE'

Related

How to do if else condition in laravel when else case is need to skip

Actually I don't want to check the else condition of below code and status 1 is compulsory only in the case of co_order_paymenttype_id is 1( both are in same table)
this is my code
DeliveryOrder::where('cb_order_type' , 0)->whereRaw('IF (`co_order_paymenttype_id` = 1,
`status` = 1,`status` = 0)')->paginate($page);
Ok as I commented above if you don't need else then it's not the case that you should use if statment.
you shoud use only where to get your needs and to make that in laravel you need to use advanced where
DeliveryOrder::where('cb_order_type' , 0)->where(function($query){
$query->where('co_order_paymenttype_id', 1)
->where('status' = 1);
})->paginate($page);
if you need to and any other condition you need to change it as you want, you can add another orWhere for expample.
please read the documentaion in the link above.

Access remove line break in string, program tells it's NULL

I have code that does something then you press enter in textfield, problem is when you use Ctrl+Enter, i can capture that event but access tells me in next line that that field is apparently NULL
Private Sub Text5_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Or KeyAscii = 10 Then
If Len(Me.Text5) = 0 Then Exit Sub
If Val(Right(Me.Text5, 1)) > 2 Then Me.Text5 = Left(Me.Text5, Len(Me.Text5) - 1) & "0"
So 'Len' works fine, but the 'Right' function gives out 'Invalid use of null', when i hit debug and check the value it is NULL
I can't figure it out
I guess i need to remove new line characters but how to do that when the text box is null and every function for strings spits out that error
The problem with your check is that Len(Null) is not 0, it's Null.
There are a couple ways to get around this. First, as mentioned in the comments, you can simply add a check for IsNull:
If IsNull(Me.Text5) Or Len(Me.Text5) = 0 Then
The other way you can do this is force it to coalesce by concatenating vbNullString:
If Len(Me.Text5 & vbNullString) = 0 Then
Also you could use Nz and set a return value of your wish in case if the expression is null, in this example also vbNullString and check the result of this function:
If Nz(Me.Text5, vbNullString) = vbNullString Then
or
If Len(Nz(Me.Text5, vbNullString) = 0) Then
or
If Nz(Me.Text5, 0) = 0 Then
or
If Not Nz(Me.Text5, False) Then
For sure you can store the result in a variable first and then check and work with this later on.
Whatever fulfills your needs.
Well, i test it as much as i can and it's just that when you use Ctrl+Enter on a field and capture the key press, field will be null for some reason, i don't see possible way around this

SSRS - IIF error in Report Builder

I am having an issue with a divide by zero error that I have half way worked through.
Basically I need (EstimatedValuePlanned - EAC) / EstimatedValuePlanned
I have the following, however I am still getting #Error on some:
= IIF (Fields!EstimatedValuePlanned.Value = 0 , 0,
Fields!EstimatedValuePlanned.Value - Fields!EAC.Value)
/
SUM(Fields!EstimatedValuePlanned.Value)
I have changed the code around several times but I still either get Error or NaN
Thank you
IIF will always evaluate both parts of the function, so when SUM(Fields!EstimatedValuePlanned.Value) is zero you will get the error even though that's not what will be returned.
Try using a SWITCH statement. SWITCH stops once an expression returns True.
Something like this
= SWITCH(
SUM(Fields!EstimatedValuePlanned.Value) = 0 , 0,
True, (Fields!EstimatedValuePlanned.Value - Fields!EAC.Value) / SUM(Fields!EstimatedValuePlanned.Value)
)
The True expression simply acts like an else
UPDATE WITH SAMPLE
I created a new report added a dataset and set the dataset query to be the following (just to generate some dummy data).
DECLARE #t TABLE (EVP float, EAC float)
INSERT INTO #t VALUES
(1,2),
(2,3),
(5,4),
(0,2),
(1,0),
(0,0)
SELECT * FROM #t
I then added a table, set the first to columns to be EVP and EAC respectively and set the 3rd column to an expression as follows.
=SWITCH (
Fields!EVP.Value = 0, 0
, True, (Fields!EVP.Value - Fields!EAC.Value) / Fields!EVP.Value
)
The report design looks like this.
And when it's run this is the result....
Try replicating the above steps and see if you can get it to work like this first then review difference to your report.
I got it to work with:
=IIF (Fields!EstimatedValuePlanned.Value = 0 , 0,
Fields!EstimatedValuePlanned.Value - Fields!EAC.Value)
/
IIF(Fields!EstimatedValuePlanned.Value =
0,1,Fields!EstimatedValuePlanned.Value)
Thank you both

Filling a column with Case When Statement in reference to another column

I am fairly new to SQL language and I am trying to do some work for Uni in Access. I have three columns: Bindugen, with values shown below, and then blank Columns Code and 33/SDGL:
Bindungen: 33,811, / 811,SDGL,33,/ 33,812,SDGL, / 812,SDGL,/ 811, (etc.)
My main column is Bindungen and I want to fill the others in relation to that one. Basically in the column 33/SDGL I tried to do the Update Case When Statement shown below. It obviously didn't work.
SELECT
FROM Codes;
UPDATE (Codes)
SET 33/SDGL ( CASE
WHEN Bindungen = "*," THEN Allein
WHEN Bindungen = "33,*," Or "*,33," THEN 33
WHEN Bindungen = "SDGL,*," Or "*,SDGL," THEN SDGL
ELSE Both
END);
My goal for the 33/SDGL column is that when Bindungen is: "," = Allein / ",33," Or "33,," = 33 / ",SDGL," Or "SDGL,*," = SDGL
and for the rest = BOTH
Could someone help me write this SQL code?
Also for the Code column, I would like it to find from Bindungen which code is there (eg: 811 or 812). Can someone point me in the right direction?
MS Access does not use the CASE statement. You need to write a SWITCH statement instead, like this:
SWITCH(Bindungen = "*,", Allein, Bindungen = "33,*,", 33, Bindungen = "*,33,", 33,
Bindungen = "SDGL,*,", SDGL, Bindungen = "*,SDGL,", SDGL, BOTH)
Also note that in this situation, Allein, SDGL, and BOTH must ALL be fields that you are selecting from. If you just want the text instead of a field value, wrap those words in "double quotes".

MySQL referencing, to avoid repeating myself?

I have this snippet:
SELECT
CASE WHEN
AVG(UNIX_TIMESTAMP(tDone)-UNIX_TIMESTAMP(tIPN))/3600 >= 10
THEN
ROUND(AVG(UNIX_TIMESTAMP(tDone)-UNIX_TIMESTAMP(tIPN))/3600,0)
ELSE
ROUND(AVG(UNIX_TIMESTAMP(tDone)-UNIX_TIMESTAMP(tIPN))/3600,1)
END
FROM
...
Can I do anything to remove the duplication from this? Something along these lines, for instance: (Hypothetical code follows):
SET var = AVG(UNIX_TIMESTAMP(tDone)-UNIX_TIMESTAMP(tIPN))/3600
SELECT
CASE WHEN
var > 10
THEN
ROUND(var,0)
ELSE
ROUND(var,1)
END
FROM
...
With a subquery you can do something like this :
SELECT
CASE WHEN avgtiPN >= 10 THEN ROUND(avgtiPN,0) ELSE ROUND(avgtiPN,1) END
FROM
(SELECT
AVG(UNIX_TIMESTAMP(tDone)-UNIX_TIMESTAMP(tIPN))/3600 AS avgtiPN
FROM
...) AS AVGQuery
But I am still uncertain if it is more readable.
Yes, you can, but variable processing order is undefined for user-defined variables. This reference in the MySQL documentation explains when this works and when it doesnt.