Understanding a Mumps statement - mumps

What is the meaning of this statement: S A=$P(P,,2) I S?

This may not be valid syntax based on the MUMPS implementation. For example, Intersystem Cache will generate a syntax error, since the second parameter passed to the piece function is blank.
The $P or $PIECE(str, delim, num), function will return the num-th segment of str when delimited by delim. So, p("a^b^c","^",2) returns "b". When delim is the empty string, $P will return the empty string. However, there can be a difference between passing nothing and an empty string.
S A=$P(P,,2) says to set the variable A to the value returned by the piece function.
Finally, I S, says that if the value of variable S evaluates to true, continue executing the rest of this line. The I or IF command also has the side effect of setting the $T variable to 1 if the expression is true, or 0, if the expression is false. This is important if your line of code is followed by an else statement, which uses $T to determine whether the previous if statement returned false.

It means
Set A = $PIECE(P,,2)
$PIECE(string,delimiter,from) returns
the substring which is the nth piece
of string, where the integer n is
specified by the from parameter, and
pieces are separated by a delimiter.
The delimiter is not returned.
The links to the documentation is here: http://docs.intersystems.com/cache20102/csp/docbook/DocBook.UI.Page.cls?KEY=RCOS_fpiece

Related

Changing order of where clauses breaks the query

I ran into this case using MySQL 5.6:
This query works and returns expected results:
select *
from some_table
where a = 'b'
and metadata->>"$.country" is not null;
However, this query (the only difference is the order of where clauses) returns an error
select *
from some_table
where metadata->>"$.country" is not null
and a = 'b';
The error MySQL returns is
Invalid JSON text in argument 1 to function json_extract: "Invalid value." at position 0.
Why?
The value of the metdata column contains malformed JSON for at least one row in the table.
We'd expect that entirely removing the a = 'b' condition, we would also observe the same error.
I suspect that the difference in behavior is due to the order of operations being performed. When the a = 'b' condition is evaluated first, that excludes rows before the JSON_EXTRACT(metadata) expression is evaluated. Since the row doesn't match the a = 'b' condition, MySQL takes a shortcut, it doesn't evaluate the JSON_EXTRACT, it already knows the row is going to be excluded.
When the comparisons are done in a different order, with the JSON_EXTRACT function executed first, the error is raised when the expression is evaluated for a rows with invalid JSON in metadata.
Summary:
There's at least one row in the table that has malformed JSON stored in metadata column.
The difference in the observed behavior of the two queries is due to a different order of operations.
Suggestion:
Consider using the JSON_VALID function to identify rows with invalid values.
Excerpt from MySQL Reference Manual
JSON_EXTRACT
Returns data from a JSON document, selected from the parts of the document matched by the path arguments. Returns NULL if any argument is NULL or no paths locate a value in the document. An error occurs if the json_doc argument is not a valid JSON document or any path argument is not a valid path expression.
https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html#function_json-extract
JSON_VALID
https://dev.mysql.com/doc/refman/5.7/en/json-attribute-functions.html#function_json-valid

Script does not work when place the function inside the WHERE clause

I am trying to create a stored procedure, which receives a string and places it in the WHERE clause after processing it.
I created the function and when I call it from the body of a SQL statement, it returns the correct values:
('J1245',j3456','j1098')
However, when I call the function inside the WHERE clause as shown below, SQL does not show any records.
(Altcode in dbo.myfunction(#codes))
When I hard code it inside the WHERE clause, the SQL statement shows records.
(Altcode IN ('J1245',j3456','j1098'))
I suspect the function is returning a string (VARCHAR). If we reference the function anywhere in a SQL statement, the value returned by the function serves as a scalar value. The contents of the value don't change that. Any punctuation, single quotes, backticks, parens, commas, keywords, identifiers, et al. that happen to appear in the string are just part of the value.
The parser sees the return from the function simply as a single value, not as part of the SQL text.
It's not possible (in a single statement) to make the value returned by a function "become" part of the SQL text to be parsed.
As an example, in the WHERE clause, if we write:
WHERE altcode IN ( myfunc() )
The function myfunc is going to be evaluated, and the return will be single value, of a certain datatype, for example maybe a VARCHAR. Any parens or commas within the value are not interpreted as part of the SQL text. Those are just characters that are within the value.
It's as if we wrote SQL like this:
WHERE altcode IN ( ? )
And supplied a single value in place of the question mark placeholder. The SQL parser isn't seeing a list of values, it's not seeing any SQL to be executed. All the statement is seeing is a value.
It matters not one whit that we might supply a value that looks like SQL text, for example:
WHERE altcode IN ( '(SELECT code FROM all_altcodes)' )
That would be equivalent to writing
WHERE altcode = '(SELECT code FROM all_altcodes)'
And that is going to look for an exact match of altcode to the string literal.
Seems like there's a single quote missing in the value returned by the function, but maybe that's a typo in the question.
To get the string value returned by the function included as part of the SQL text, we would need to use dynamic SQL.
We would have to first call the function, and return the string. And then do some string concatenation to come up with another string that contains the SQL statement we want to execute, and then execute that string as a SQL statement.
So that would be two separate statement executions... one to get the function evaluated; and a second statement that is dynamically constructed, as a string, incorporating the value returned by the function.
(The question is tagged "MySQL", but I suspect this question is actually regarding SQL Server (given the reference to dbo. ?)
Preparing and executing dynamic SQL is similar in MySQL and SQL Server, but there are differences in the syntax.

why do we give >0 in instr function ssrs. The InStr function works without giving the greater than 0 value

why do we give >0 in instr function ssrs.
The InStr function works without giving the greater than 0 value.
Because InStr returns index of first appearance of search string in string that is searched. Index is 1-based, and when string not found, the returned index is lowerbound(string as array of chars) - 1 = 1 - 1 = 0. This is in VB, in C# f.e. it will be 0 - 1 = -1.
When you're using InStr to determine if string is found, you need boolean result, and to get it you use comparison > 0, which returns True when string found and False otherwise.
But, since there is implicit conversion exist between boolean and int, you can use InStr directly, and return value 0 (not found) will be converted to False, while any non-zero value (found) will be converted to True.
Although this is correct and works, this way is less obvious, and taking a look at the code =InStr(...) you can't quickly say, real index needed (integer) or the fact that this index exist (boolean). Finally it's all about code readability and maintainability, don't forget that implicit conversions (especially in not-debuggable SSRS code) may bring you a lot of problems.

No keyword with name '=' found in robot

I'm writing a test case in robot framework. I'm getting the response in below json string:
{"responseTimeStamp":"1970-01-01T05:30:00",
"statusCode":"200",
"statusMsg":"200",
"_object":{"id":"TS82",
"name":"newgroup",
"desc":"ttesteste",
"parentGroups":[],
"childGroups":[],
"devices":null,
"mos":null,
"groupConfigRules" {
"version":null,
"ruleContents":null
},
"applications":null,"type":0
}
}
From that I want to take "_object" using:
${reqresstr} = ${response['_object']}
... but am getting the error "No keyword with name '=' found" error
If I try the following:
${reqresstr}= ${response['_object']}
... I'm getting the error "Keyword name cannot be empty." I tried removing the '=' but still get the same error.
How can I extract '_object' from that json string?
When using the "=" for variable assignment with the space-separated format, you must make sure you have no more than a single space before the "=". Your first example shows that you've got more than one space on either side of the "=". You must have only a single space before the = and two or more after, or robot will think the spaces are a separator between a keyword and argument.
For the "keyword must not be empty" error, the first cell after a variable name must be a keyword. Unlike traditional programming languages, you cannot directly assign a string to a variable.
To set a variable to a string you need to use the Set Variable keyword (or one of the variations such as Set Test Variable). For example:
${reqresstr}= Set variable ${response['_object']}
${reqresstr}= '${response["_object"]}'
wrap it inside quotes and two spaces after =
There is a syntax error in your command. Make sure there is a space between ${reqresstr} and =.
Using your example above:
${reqresstr} = ${response['_object']}

Expression issue in SSIS

I have a string like this format. In my case I need to extract only the characters from string case
1)12AB
2)SD12
3)1WE4
output
1)AB
2)SD
3)WE
I need to extract only the characters. I am using this expression in the derived column in SSIS package
SUBSTRING(MediaIDCode,1,2)
but this expression works for only this condition
1)12AB
I need to get an expression that works for all the conditions above. I have tried using REPLACE ( '' , '1', '') but it becomes a big expression.
The SUBSTRING function code you've posted will extract characters from a string without considering their content.
Have a look at using Regular Expressions to filter out unwanted characters:
http://consultingblogs.emc.com/jamiethomson/archive/2005/07/04/SSIS-Nugget_3A00_-The-script-component-and-regular-expressions.aspx
You could also use a synchronous script transformation to filter characters this way:
Dim NewMediaIDCode As String = ""
For Each c As Char In Row.MediaIDCode
If Not Char.IsDigit(c) Then
NewMediaIDCode += c
End If
Next
Row.MediaIDCode = NewMediaIDCode
I've used the IsDigit method here, but there's plenty of other methods to choose from.