Access - Error 3615 when joining two decimals - ms-access

I've got two tables.
The first one looks like this. The type of the numbers is decimal.
TABLE1
T1NUMBER T1INFORMATION
123456 Stuff1
123457 Stuff2
123458 Stuff3
My other table looks similar. Basically the id is the same as in the other table, but with two extra characters at the end.
TABLE2
T2NUMBER T2INFORMATION
123456L1 Important Info1
123457L1 Important Info2
123458L1 Important Info3
To join the tables, I made a query to cut the extra characters. I know, that I can only match decimal colums with decimal ones. So I added a function, that converts the string I got to a decimal. The result of the query looks like this.
QUERY1
Q1NUMBER Q1INFORMATION
123456 Important Info1
123457 Important Info2
123458 Important Info3
The function I used to get a decimal value is this one. I added it as a module.
Function NewCDec(MyVal)
NewCDec = CDec(MyVal)
End Function
Let's finally get to my problem. I want to join TABLE1 with QUERY1 on the numerical columns. Both should now be decimal values and should be joinable. But I always get error 3615 (Type mismatch in JOIN expression). Why do I still get that error? Can anyone help me fix it?
My query should look like this in the end:
RESULT
NUMBER T1INFORMATION Q1INFORMATION
123456 Stuff1 Important Info1
123457 Stuff2 Important Info2
123458 Stuff3 Important Info3

I found my error. Some values at T2NUMBER where invalid/null and I forget to filter that. So Access didn't let me join the tables. It worked after I filtered the null values.

Logically, a simple change in your function should solve the problem.
Function NewCDec(ByVal MyVal As String) As Double
NewCDec = CDec(MyVal)
End Function
From the information provided in your question, this should solve the problem.
But when it comes to problems, logics sometime doesn't work!!
So, if the above changes doesn't work. Try to put your CDec() in SQL by creating a temporary table using (SELECT ... FROM Table) As TempTable.
SIMPLE EXAMPLE:
SELECT CDec(T2NUMBER) As Number FROM Table2
TEMP TABLE EXAMPLE:
SELECT TempTable.* FROM (SELECT CDec(T2NUMBER) As Number FROM Table2) As TempTable

Related

How to get the exact value from sql like query

I have a Mysql database that contains some category ids on it which stores comma-separated values on a table.
sql table view
By using select * from style where categories like '%8,%'; it returns all the values end with 8. For example, if the table rows have two values like 8 and 148 it returns both rows. But I want to get only the rows that contain 8. How to do it
Storing multiple values in a single column is a denormalised design that will almost always cause you problems. However you need to add commas to both sides and compare:
select *
from Style
where concat(',',Categories,',') like '%,8,%';
Like everyone else: normalize your data. But if you can't mySQL supports find_in_set() for set datatypes which this appears to be.
DEMO dbfiddle.uk
DOC LINK: Find_in_set()
DOC LINK: SET data type
SQL
With CTE as (SELECT 'T-Shrits' as baseCategory, '8,21,75,87,148' categories UNION ALL
SELECT 'T-Shrits' as baseCategory, '8,21,75,87,148' categories UNION ALL
SELECT 'T-Shrits - Long Sleeve' as baseCategory, '8,21,75,87,148,92' categories UNION ALL
SELECT 'T-Shrits' as baseCategory, '21,75,87,100,148' categories)
SELECT * FROM CTE where find_in_set(8,categories) >0
OR we can use a boolean evaluation and eliminate the > 0
SELECT * FROM CTE where find_in_set(8,categories)
Giving us:
+------------------------+-------------------+
| baseCategory | categories |
+------------------------+-------------------+
| T-Shrits | 8,21,75,87,148 |
| T-Shrits | 8,21,75,87,148 |
| T-Shrits - Long Sleeve | 8,21,75,87,148,92 |
+------------------------+-------------------+
Notes
Find_in_set() returns the Returns a value in the range of 1 to N in the pseudo array of the value being searched. We need to ensure the result is greater than 0 (or treat it as a Boolean) in order for the searched value to "exist" within a record column.
The engine didn't return my 4th union value in CTE because it doesn't have an "alone" 8 value
If we searched for just 100 it would return that last record.
This function comes at a cost of performance on large datasets; which if data was normalized and indexed, you wouldn't have.
So why does this exit? For small enumerated lists or properties. It's still not ideal but if you have just a few using it "can" make sense. but in a very limited use case and often is missused.
This design violates 3rd normal form. Which is why most RDBMS designs cringe when it's brought up as it's not scalable.
as to why people are up in arms about multi value columns: Read this or This
You can also use rlike and in fact it is much better than like as it has much more options.
* = repetition of what is in front of it zero or more times
. = Equivalent to any character including none
^ = Anchor start (Forces that begins with ...)
$ = final anchor (forces it to end with ....)
[ ] = [ RST ] Contain an R or S or T but only one
[^] = DENY IT
And many more options
select * from style where concat(',',categories,',') rlike '*,8,*';

Count returns 0 for a column that exists

So i have a MySQL table that contains 2 fields - deviceID and jobID. Below is a sample of what the table looks like with Data in it:
+----------------------+----------------------+
| deviceID | jobID |
+----------------------+----------------------+
| f18204efba03a874bb9f | be83dec5d120c42a6b94 |
| 49ed54279fb983317051 | be83dec5d120c42a6b94 |
+----------------------+----------------------+
Usually i run a query that looks a little like this:
SELECT Count(deviceID)
FROM pendingCollect
WHERE jobID=%s AND deviceID=%s
Now this runs fine and usually returns a 0 if the device doesnt exist with the specified job, and 1 if it does - which is perfectly fine. HOWEVER, for some reason - im having problems with thew second row. The query:
SELECT Count(deviceID)
FROM pendingCollect
WHERE jobID='be83dec5d120c42a6b94' AND deviceID='49ed54279fb983317051'
is returning 0 for some reason - Even though the data exists in the table3 and the count should be returned as 1, it is returning as 0... Any ideas why this is?
thanks in Advance
EDIT:
Sorry for the type guys! The example SQL query shouldnt have had the same devID and jobID.. My Mistake
EDIT 2:
Some people are suggesting i use the SQL LIKE operator.... Is there a need for this? Again, when i run the following query, everything runs fine and returns 1. It only seems to be on the deviceID "49ed54279fb983317051" that is returning the error...
SELECT Count(deviceID)
FROM pendingCollect
WHERE jobID='be83dec5d120c42a6b94' AND deviceID='f18204efba03a874bb9f'
The above query works as expected returning 1
You need to provide the correct value for jobID. Presently you are providing the value of deviceID in jobID which is not matching and hence returing 0 rows.
SELECT Count(deviceID) FROM pendingCollect
WHERE jobID='49ed54279fb983317051' AND deviceID='49ed54279fb983317051'
^^^^^^^^^^^^^^^^^^^^^^^
The reason why
jobID=%s and deviceID=%s
which I think you mean
jobID like '%s' and deviceID like '%s'
was working because both were matching. But now since you are using the AND condition and providing jobID value same for both so it would not match any row. And will return 0 rows.
EDIT:
You query seems to be correct and is giving giving the correct result.
SQL FIDDLE DEMO
You need to check if there is any space which is getting added to the values for the jobID and deviceID column.
This is because of the AND operator. AND means both conditions must be true. Instead of AND, use OR operator.
SELECT Count(deviceID)
FROM pendingCollect
WHERE jobID = '49ed54279fb983317051' OR deviceID = '49ed54279fb983317051'
Check the real value the column contains, there might be some unprintable character in it:
SELECT deviceID, hex(deviceID), hex('49ed54279fb983317051')
FROM pendingCollect
WHERE jobID='be83dec5d120c42a6b94';
If there is anything weird in that deviceID, it should show as a difference in the hex output - for the value you want, I did not add it to the where condition as you have problems "targeting" it, so this query will return rows even for the other devices with the same jobID.

MySql select on fields containing null values

In our company we moved our web application (LAMP) from one server (Ubuntu 10.04) to a new server (Ubuntu 12.04.2). Now we encountered a strange behavior I haven't seen before and I really do not know where to begin. Maybe someone can give me hint.
We have got the following simple table:
id data1 data2 data3
(int) (varchar) (int) (int)
-------------------------------------
1 (empty) 123 456
2 (null) 321 654
3 abc 555 666
(empty) means the field contains a empty string. (null) means that the field is null. Now we use the following very very simple query:
SELECT * FROM `table` WHERE `data1` != 'abc';
On our old server the query returned the lines with the ids 1 and 2 which, I guess, is absolutely correct since !='abc' matches those two recordsets.
On our new server the query only returns the recordset with the id 1. Recordsets containing null in the select fields are suddenly ignored by the query somehow.
Just to make it more clear: I know that IS NULL could be used, but that would result in checking all queries and tables in the application matching this situation.
Now the questions are:
Did we had luck on our old server that the query behaved as expected by returning lines 1 and 2 or does the new server behave correct by returning only line 1?
Generally: Should !='abc' match the recordsets 1 and 2 or should it only match id 1?
Is it possible that there is a setting in the mysql configuration that controlls that behaviour? I am a little stuck with that. Every help is appreciated!
Thanks in advance...
Because null is a special case, if you want null values to be included, you should explicitly specify that you want them.
SELECT * FROM table WHERE (data1 <> 'abc' or data1 is null)
The expected server behavior, is ignore nulls unless you ask for them...
SELECT * FROM `table` WHERE `data1` = '';

mysql string split

I am using the following queries and getting diff results.
SELECT SUM(amt) FROM table1 WHERE id in (114,116) and another_id = 10;
result is 10+20 = 30
SELECT SUM(amt) FROM table1 WHERE id in (REPLACE("116,114",'"',"")) and another_id = 10;
result is 10
i was suggested to use a string split function and put the splits in a table,can someone point me to an example?
SELECT SUM(amt)
FROM table1
WHERE id in (REPLACE("116,114",'"',"")) and another_id = 10;
It would help if we knew what you were trying to do.
At a guess, I suspect that you want to provide a facility where someone can specify a set of ids and pass that as an argument to the query.
A simple solution would be:
WHERE CONCAT('%,', id, ',%') LIKE ('${submitted_string}')
But this will not be able to use any index on table1.id
AFAIK there's no way to cast an array/string to a table type in MySQL, so you'd need to parse the string and put the values temporaril;y into a table along with a key referencing the session (or into a temporary table). Doing this in SQL is rather hard work - it's a lot simpler to use a logic tier on top - which, based on my interpretation of what your trying to achieve you must already have.
Based on your code, it appears ID is a numeric field...
Try this for the second example...
SELECT SUM(amt) FROM table1 WHERE ","+CAST(id AS CHAR)+"," in (","+"116,114"+",")
and another_id = 10;

MySQL select with subquery having replace

So I have a data with format like ;1;;2; and then I need to use this number in a query so I thought I'd convert it to 1,2 and use that in a IN condition. In my table, the result should return 2 rows but instead it is returning only 1 row.
My query is like this. The subquery return 1,2 with no problem but only 1 row is retrieve.
select *
from wt_lists
where id IN ((select replace (replace(sendto, ';;',','),';','')
from wt_stats where statsid IN (1)))
But when I try it with this. It returns the correct result, which in my case is 2 rows.
select *
from wt_lists
where id IN (1,2)
What am I missing here?
Comma delimited strings need to be explicitly defined in the query in order to be used in the IN clause - there's countless examples on SO where people need to use dynamic SQL to incorporate user submitted comma delimited strings.
That said, I have a solution using the FIND_IN_SET function:
SELECT DISTINCT wl.*
FROM WT_LISTS wl
JOIN (SELECT REPLACE(REPLACE(ws.sendto, ';;',','),';','') AS ids
FROM WT_STATS ws
WHERE ws.statsid = 1) x ON FIND_IN_SET(wl.id, x.ids) > 0
You are replacing the string:
';1;;2;'
To:
'1,2'
So, you SQL query looks like:
select * from wt_lists where id IN ('1,2') from wt_stats where statsid IN (1)
To use IN clause you need select different values in different rows.
I found this store procedure that does exactly what you need.
http://kedar.nitty-witty.com/blog/mysql-stored-procedure-split-delimited-string-into-rows/
I have not tested, but it is the way.
Obs: Like David said in the comments above, parsing the data in your application is a better way to do this.