Assigning values to numbers in a text string using sql in mysql - mysql

This is an example of a string inserted into a mysql db field after a multiple choice checkbox form field has been submitted. (including the brackets): [1,0,1]
1=box checked
When the first number in the string = 1 value = pie, when the second number in this string =1 value= cookies, when the third number in this string =1 value = fruit. So the output for this string would be cookies, fruit.
I need to assign values to these numbers in an sql query. Something like:
select answer_data
from answers
where if the first number in the string = 1, the value is pie; if the 2nd number in the string = 1, the value is cookies;if the third number in the string=1, the value is fruit;

Caveat: Storing the data in this implied mapping to an array definition is a very brittle design. A better approach would be to just store the actual text values as JSON.
However, if you must use that approach...
If you defined a stored procedure
https://dev.mysql.com/doc/refman/8.0/en/create-procedure.html
You could use the Substring function to tease out the values - and assemble desired output
https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_substring

Related

tying to add boolean values as timeseries gives error Time series is configured as numeric but used as a string or the other way around

I have IOT data that is coming from devices into foundry. I created the timeseries dataset and metadata as explained in the documentation https://www.palantir.com/docs/foundry/time-series/time-series-setup/#time-series-dataset
I have two datasets, one with double values that show up fine, but the dataset with value as string doesnt work.
As per the domumentation it says the Dataset itself should have cloumn type String for enum series .
Then it says that the metadataa should have is_enum which should be of type boolean
is_enum: This is an optional field for time series values that are expected in string format (ie Open/Close). Must be of type Boolean and is required to be true for series that are Enum types (otherwise is optional). This value should be consistent for a measure.
So in my metadata, for the series Id that have boolean value, I have is_enum set to true and datatype is boolean. Im able to create the time series sync. But I get the error that the time series is configured as numeric.

Substring from-to

I am working on a SSIS(2017) solution to read and load data from these 3 excel file names:
message_EDF_100420202.csv
message_UltaBIO_10042020.csv
message_SEIDV_10042020.csv
What I need to do is get only EDF or UltraBIO or SEIDV as a new column (derived column task)
so I need some help to set up correctly the substring function inside the derived column task.
any suggestion?
It appears your pattern is message_ Stuff-I-Want _junk (spaces not present in actual pattern). It's delimited by underscores and since the starting text is constant, that makes life easier.
Create a new column called MessageLessName
Remove the message_ portion with an expression
REPLACE([SourceFile], "message_", "")
Now, we want to take the left N most characters where N corresponds to the location of the underscore in our new column MessageLessName. For ease of debugging, I propose you add a second Derived Column Task to the output of the first one (where we defined MessageLessName). Here, we're going to create FirstUnderscore column
findstring([MessageLessName], "_", 1)
Finally, we'll add a third Derived Column Task and here-in is where we'll get to the final file name.
LEFT([MessageLessName], [FirstUnderscore])
Now that may be off by one due to my being lazy but because you can check each step along the way, you can verify MessageLessName is exactly what you think it should be and that FirstUnderscore is N characters in from our MessageLessName column.
script component using Split.
Row.ColumnName.ToString().Split('_')[1];
You are taking the column value and casting to string. (current value is the whole string)
Next is splitting based on '_' (current value is an array of three strings)
Finally you are taking the second value (0 based) (current value is the string you want)
Here's a little bonus. Getting the date as well:
string[] breakdown = Row.fileNames.Split('_');
Row.Type = breakdown[1];
string dateToFix = breakdown[2].Replace(".csv", "");
Row.Date = DateTime.Parse(dateToFix.Substring(0,2) +"/"
+ dateToFix.Substring(2,2) + "/" + dateToFix.Substring(4,4));

Pad user input with zeros in a multiple value parameter

Is there a way to pad numbers in a multiple value parameter to make them have 8 characters in length? Right now I am using:
=Right("00000000" & Parameters!Accounts.Value,8)
in an invisible parameter then that parameter is passed to the in part of my query. When I have, multiple values unchecked it works properly, but as soon as I turn on multiple values I get an error
The DefaultValue expression for the report parameter ‘ActualAccounts’ contains an error: Operator ‘&’ is not defined for string “00000000” and type ‘Object()’.”
I want the user to be able to paste in a list of accounts like:
93874
93932128
3838
And then it queries the database as
00093874
93932128
00003838
Here is an approach that could work for you.
First, grab the udf_Split user-defined function (UDF) from the this question's answer. With this UDF you to pass the delimited string as a parameter, and it will return a table with a row for each value. This is how SSRS sends the data to SQL Server when it comes to multi value parameters.
Once you have that in place, then all you need to do is use that UDF in the following manner.
DECLARE #ActualAccounts varchar(100) = '93874,93932128,3838'
SELECT RIGHT('00000000' + RTRIM(LTRIM(Value)), 8) AS Accounts
FROM dbo.udf_Split(#ActualAccounts, ',');
Results:
Accounts
--------
00093874
93932128
00003838
Then you could use this in the SQL for the report
SELECT *
FROM Accounts
WHERE AccountNumber IN (SELECT RIGHT('00000000' + RTRIM(LTRIM(Value)), 8)
FROM dbo.udf_Split(#ActualAccounts, ','));
This answer assumes the name of the SSRS parameter is ActualAccounts. You should be able to fit this into a stored procedure, if that is what you are using. It should work in straight SQL if you have that embedded in the RDL, too.
Hope this helps out.

Remove query string pairs until a result is returned

Lets say I have a search form on my site which generates a query string to filter results, eg. mysite/search?field1=value1&field2=value2&field3=value3
The user enters the following into the search fields:
Field 1 = Cat
Field 2 = Black
Field 3 = Stray
Given the nature of a query string, all three field values would have to be present in an item being searched, in order for it to be recognised as a match.. right?
Is there a way to either make the '&' an 'OR', so that any matching field will return a result.
Or
Is there a way to match the nearest result? Ie. Remove string pairs until a match is found, or in some way, find the next closest result.
For example. If the user enters 'Cat', 'Black' and 'Stray' and there is an item that includes all three values, it returns that result (standard response). If there isn't an item that includes all three values, let's say there's only an item that has 'Cat' and 'Stray', it recognizes there are no items containing all three values, so it looks for two field value matches instead?
Happy to consider any ideas to prevent "no items found" and at least render something rather than nothing.
It depends on what you're using for filtering but general idea is that after you filter the data you check if it is empty and if it is you remove one filter do the filtering again. Something like this for example:
result = filter(data,filters);
while(!result || filters.length > 0){
filters.pop();
result = filter(data, filters);
}
Not working code, just general idea.

SSRS 2008 Conditional data driven sum

The matrix's odd rows contain strings, representing integer numbers. The even rows contain strings, representing dates in mm/dd/yyyy format. The order is not guaranteed, it could be the other way around if someone changed the category names.
The grand total row has to be added to this matrix, but the following expressions throw an #Error:
=Sum(Iif(IsDate(Fields!Data.Value), 0, CInt(Fields!Data.Value)))
=Sum(Iif(InStr(Fields!Data.Value, "/"), 0, CInt(Fields!Data.Value)))
Converting 0 and field value to some other numeric data types did not work too.
Interesting enough, the expressions partially work for the grand total column, i.e. they calculate the numbers' row total, but #Error on the dates rows.
Protecting the row grand total as follows did not help either:
=Iif(Fields!Results.Value = "# of items", CStr(Sum(CInt(Fields!Data.Value))), "")
What is the correct way to implement data driven conditional totals?
I could do this in plain SQL and dump into a tablix in a heartbeat but this has to be wrapped in SSRS and used with an existing matrix which must not be changed otherwise.
The #ERROR you get on the date rows is due to the CInt conversion failing.
This is because IIF is a function, not a language construct so both the true and false parameters get evaluated before being passed to the function regardless of the value of the boolean condition parameter. This means that:
=Sum(Iif(IsDate(Fields!Data.Value), 0, CInt(Fields!Data.Value)))
will always attempt the conversion to integer regardless of the result of the IsDate function.
Try using Val instead of CInt. The problem with CInt is it errors when the string to be converted is an inappropriate form; Val doesn't have that problem - it simply grabs whatever numbers it can. So you can use the expression:
=Sum(Iif(IsDate(Fields!Data.Value), 0, Val(Fields!Data.Value)))
and then simply format it like an integer.
Note that the Val function is still being run even when the field is not numeric but this expression succeeds because the Val function doesn't raise errors like Cint. We simply make the calculation and discard the result when the field is a date.
This expression, combining the tips from both answers with additional protection, works:
=Iif(
IsNumeric(Fields!Data.Value),
Sum(Val(Iif(InStr(Fields!Data.Value, "/"), "", Fields!Data.Value))),
0
)