Creating a query that can be numerically evaluated in Access - ms-access

I'm trying to get some of the data provided to me clean enough to be evaluated. I've been provided with testing data that has special characters in it. For example, a large proportion has the value <0.5** or <0.5* indicating that the level of contaminant is below 0.5 PPM. However, this doesn't help much when I'm trying to evaluate that column.
I'm trying to figure out if the best way is to create a query and use that, or to create a new field and use an update query to remove the special characters. However, I can't figure out how to create a query from these values that can be evaluated numerically, and whenever I try and run an update query with Replace(Replace([SampleResult],"*",""),"<","") it doesn't output anything.

Related

How to return substring positions in LIKE query

I retrieve data from a MySQL database using a simple SELECT FROM WHERE LIKE case-insensitive query where I escape any % or _ in the like clause, so really the user can only perform basic text research and cannot mess up with regex because I then surround it myself with % in the LIKE clause.
For every row returned by this query, I have to search again using a JS script in order to find all the indexes of the substring in the original string. I dislike this method because I it's a different pattern matching than the one used by the LIKE query, I can't guarantee that the algorithm is the same.
I found MySQL functions POSITION or LOCATE that can achieve it, but they return only the first index if it was found or 0 if it was not found. Yes you can set the first index to search from, and by searching by passing the previously returned index as the first index until the new returned index is 0, you can find all indexes of the substring, but it means a lot of additional queries and it might end up slowing down my application a lot.
So I'm now wondering: Is there a way to have the LIKE query to return substring positions directly, but I didn't find any because I lack MySQL vocabulary yet (I'm a noob).
Simple answer: No.
Longer answer: MySQL has no syntax or mechanism ot return an array of anything -- from either a SELECT or even a Stored Procedure.
Maybe answer: You could write a Stored procedure that loops through one result, finding the results and packing them into a commalist. But I cringe at how messy that code would be. I would quickly decide to write JS code, as you have already done.
Moral of the story: SQL is is not a full language. It's great at storing and efficiently retrieving large sets of rows, but lousy at string manipulation or arrays (other than "rows").
Commalist
If you are actually searching a simple list of things separated by commas, then FIND_IN_SET() and SUBSTRING_INDEX() in MySQL closely match what JS can be done with its split (on comma) method on strings.

Insert A String That Contains An Incremental Value

I need to setup 1000 test users in our DB. The users are named like this:
TestUser0001
TestUser0002
TestUser0003
And so on up to 1000. The thing is, I don't want to write 1000 insert statements to get this done. My approach was to use an IF/ELSE and use several variables so that I can check when the incremental value is within a range where I can put the appropriate zeros in place.
There has to be an easier way to do this, but I can't think of of it. The leading zeros are the biggest problem.
Anyone know of an easy way to accomplish this task? How would you do this?
The answer to this is to use FORMAT(). You could pass your variable that holds the incremented value into the FORMAT function and the format would be '0000'. Like so:
FORMAT(#IncVar, '0000')

Is there a way to get only the numeric elements of a string in mysql?

I'm looking to make it easier for the clients to search for stuff like phone/mobile/fax numbers. For that to happen I want to strip both the search value and the relevant columns in my database of any non-numeric characters before comparing them. I'm using these functions to get only the numeric elements of the strings in mysql but they slow my queries down to a crawl when I use them.
Is there any way to do it without blowing my run times sky high?
The reason why your query times are exploding is because any use of such functions disables you from using any index. Since you are not searching directly on a field, but on the output of a function, there is no way mySQL can use an index to execute the query.
This is in addition to the fact that you have to compute the function output for each record.
The best way around these runtimes, if you have access and permission to do so, is to add a new column with the content you're filtering. Add a WRITE trigger to fill the column with the stripped values, run a script that updates the field once for all records. Add an index and include the new column. Then, in your application, use the new column for searches for a number value of a telephone. Downsides are table schema alterations and added code for the business logic and/or data abstraction layer.

Mysql: use value as alias in query

given a table
create table mymy(A int(2),B int(2))
is it possible to use a field value as an alias? Something like (not really):
select A as valueOf(B) from mymy.
No. You can't. The values are not known until the query is run. And even if you could, you'd have a lot of possibly different values in one column. Which one should be used?
The only valid reason I can imagine for such a request is that you have some kind of EAV design and you want to have a Pivot result.
If that's the case, you could use Dymanic SQL (run a query, get the results, build another query based on those results and run that one.) But this kind of operations is better done at the application side (get the results and format them there, as you prefer).

Calculated columns in Access 2003 have null value when inserting into new table

I have a make table query where some of the columns are calculated. An example of how one of those columns looks is as follows:
SQFTCost: (([SUPPLY_MASTER]![LAST_COST]+[SUPPLY_MASTER]![FREIGHT_COST])/[SUPPLY_MASTER]![SQFT_PER_CTN])
In this case, LAST_COST is a decimal with a precision of 9 and a scale of 3. FREIGHT_COST is is a decimal with a precision of 8 and a scale of 3, and SQFT_PER_CTN is a decimal with a precision of 7 and a scale of 3.
Whenever I run the make table query, that column and all the others like it are filled with nulls. I know that they are actually null, because I tested that in a routine that I wrote.
However, if I change the query to a SELECT query, all is well. The values are correct.
Does anyone have any idea what can be done to fix this? I am using Access 2003.
Just a few suggestions:
Try adding a cLng() or something equivalent in front of your expressions, to force a well defined data type
I avoid Make Table queries, preferring Append queries. Just make an "template" table properly set up, and use a copy with Append queries. It's the only way to have a clean design.
I have come across the correct answer to this issue. It is the result of a sometimes flaky ODBC driver that we are using here. Our data rests in COBOL files that are in the Vision file format. The trouble is coming from using fields that are defined in the xfds as REDEFINES of other fields in the file. Using the original field fixes the issue.