May I know how to make a null value show in an SQL result as 0?
String SQL = " SELECT A.CNCODE,C.TOTPREM,B.ORDER_AMOUNT,DECIMAL(D.AMOUNT,12,2) AS AMOUNT FROM TB_ORDER A,"+
" TB_ORDER2 B, TB_ORDER3 C LEFT JOIN TB_ORDER4 D ON C.UKEY2=D.UKEY"+
" WHERE C.UKEY2='0012254' AND C.UKEY2=B.UKEY2 AND C.UKEY2=A.UKEY ";
SQL += " WITH UR";
In the current SQL result, amount is shown as "-" indicating NULL; how to make it "0.00"?
===========================================
CNCODE | TOTPREM | ORDER_AMOUNT | AMOUNT
===========================================
I0012254 |136.54 | 5 | -
In mysql you can use coalesce function to convert null as 0 while selecting the data
mysql> select coalesce(null,0);
+------------------+
| coalesce(null,0) |
+------------------+
| 0 |
+------------------+
1 row in set (0.00 sec)
mysql> select coalesce(123,0);
+-----------------+
| coalesce(123,0) |
+-----------------+
| 123 |
+-----------------+
1 row in set (0.00 sec)
Try this Query..
select CNCODE, TOTPREM, ORDERAMOUNT, IFNULL(AMOUNT,0)(select A.CNCODE as CNCODE, C.TOTPREM as TOTPREM,
B.ORDER_AMOUNT as ORDERAMOUNT, DECIMAL(D.AMOUNT,12,2) AS AMOUNT FROM TB_ORDER A,
TB_ORDER2 B, TB_ORDER3 C LEFT JOIN TB_ORDER4 D ON C.UKEY2=D.UKEY
WHERE C.UKEY2='0012254' AND C.UKEY2=B.UKEY2 AND C.UKEY2=A.UKEY ) temp
Related
In my query I am using REPLACE( b.DESCRIPTION,'SP. Z O.O.','') AS DESCRIPTION to remove SP. Z O.O. these characters from columns. And hopefully it's working for me. But in my database SP. Z O.O. this characters are stored in different ways. Like sp. Z.o.o, SP. z.o.o etc.
Somewhere it's stored in capital letters and somewhere it's stored in small letters.
REPLACE( b.DESCRIPTION,'SP. Z O.O.','') AS DESCRIPTION by this method I am only able to remove capital letters. I want all conditions to remove similar words like this.
How to apply regex or case in this situation?
This is my query:
SELECT b.TRANS_DETAILS_ID, b.CREDIT_AMOUNT, b.ENTITY_NAME, REPLACE( b.DESCRIPTION,'SP. Z O.O.','') AS DESCRIPTION, DATE_FORMAT(a.TRANSACTION_DATE_TIME,'%d-%m-%Y') AS TRANS_DATE FROM bank_book_transaction_master a, bank_book_transaction_details b WHERE a.TRANSACTION_DATE_TIME BETWEEN '2017-12-01' AND '2017-12-26' AND DEBIT_CREDIT_FLAG = 1 AND a.ORG_ID = '53' AND a.BANK_ID = '14' AND a.TRANSACTION_ID = b.TRANS_MASTER_ID
You can do it with a query like this:
UPDATE strtest
SET mystring = CONCAT(
LEFT (mystring, POSITION('SP. Z O.O.' IN mystring)-1),
RIGHT(mystring, LENGTH(mystring)-POSITION('SP. Z O.O.' IN mystring)-9)
)
WHERE mystring LIKE '%SP. Z O.O.%';
Sample:
Test Table
MariaDB [bernd]> SELECT * from strtest;
+----+------------------+
| id | mystring |
+----+------------------+
| 1 | SP. Z O.O. |
| 2 | ABCSP. Z O.O. |
| 3 | SP. Z O.O.XYZ |
| 4 | QWESP. Z O.O.IOP |
| 5 | AAASp. Z o.O.LLL |
+----+------------------+
5 rows in set (0.00 sec)
Remove the String in a SELECT ( The >>><<< are only for test)
MariaDB [bernd]> SELECT CONCAT( '>>>',
-> LEFT (mystring, POSITION('SP. Z O.O.' IN mystring)-1),
-> RIGHT(mystring, LENGTH(mystring)-POSITION('SP. Z O.O.' IN mystring)-9),
-> '<<<') AS resultstring
-> FROM strtest;
+--------------+
| resultstring |
+--------------+
| >>><<< |
| >>>ABC<<< |
| >>>XYZ<<< |
| >>>QWEIOP<<< |
| >>>AAALLL<<< |
+--------------+
5 rows in set (0.00 sec)
To UPDATE the Table
MariaDB [bernd]> UPDATE strtest
-> SET mystring = CONCAT(
-> LEFT (mystring, POSITION('SP. Z O.O.' IN mystring)-1),
-> RIGHT(mystring, LENGTH(mystring)-POSITION('SP. Z O.O.' IN mystring)-9)
-> )
-> WHERE mystring LIKE '%SP. Z O.O.%';
Query OK, 5 rows affected (0.00 sec)
Rows matched: 5 Changed: 5 Warnings: 0
MariaDB [bernd]> SELECT * from strtest;
+----+----------+
| id | mystring |
+----+----------+
| 1 | |
| 2 | ABC |
| 3 | XYZ |
| 4 | QWEIOP |
| 5 | AAALLL |
+----+----------+
5 rows in set (0.00 sec)
MariaDB [bernd]>
If you are using MariaDB, you could use REGEXP_REPLACE() like the next line:
REGEXP_REPLACE(col, regexp, replace)
Here you will find examples about the usage.
I realize we can use substring() and locate() function for tokonization
I was tried query
insert into sum_of_counts
select substring(pair,1,locate('|',pair)),
sum(count)
from em
group by substring(pair,1,locate('|',pair))
for example : we use 'resumption|||resumption' as pair to query after substring('resumption|||resumption',1,locate('|','resumption|||resumption'))
it should be resumption|
but after the query it appeared
+------------+------+
| wild_pair | sum |
+------------+------+
| resumption | 8 |
+------------+------+
the problem is we could find 'resumption|||resumption' in em table
after I check the table sum_of_counts some of wild_pair are word| some of wild_pair are just word how can I solve this problem?
SELECT
LEFT(`pair`, LOCATE('|||', `pair`)) `wild_pair`,
SUM(count) `sum`
FROM `em`
GROUP BY `wild_pair`;
Should do the same thing easier.
If the error occurs when inserting the result into another table, check if the existing columns are wide enough to take the calculated data in full length.
Your result should not be possible:
mysql> select locate('|', 'resumption|||resumption');
+----------------------------------------+
| locate('|', 'resumption|||resumption') |
+----------------------------------------+
| 11 |
+----------------------------------------+
1 row in set (0.00 sec)
mysql> select substring('resumption|||resumption', 1, 11);
+---------------------------------------------+
| substring('resumption|||resumption', 1, 11) |
+---------------------------------------------+
| resumption| |
+---------------------------------------------+
1 row in set (0.00 sec)
Are you sure that pair is always foo|||bar with 3 |||?
I m stuck in a situation where I needed (column1 and column2) or (column1 or column3) from a table. So i implemented it as
select * from mytable
where column1=x and (column2=y or column3=z)
But it fetches me some unneccesory rows and by implementing as
select * from mytable
where (column1=x and column2=y) or (column1=x and column3=z)
It gives the result but i couldn't understand the diff between the two...please suggest
EDIT (added details)
Below I have explained my situation, Please check this,
Let me elaborate my situation :::
I have a table, say clientdetails(int id, var firstname, var mobileno, var landlineno) and I need to fetch those entries fetching values having unique (firstname and mobileno), or (firstname and landlineno). Either of the two mobileno or landlineno is mandatory.
so i wrote a query...
select id
from clientdetails
where firstname = 'pooja'
and (mobileno = mn or landlineno= ln )
and mobileno REGEXP '^[0-9]+$'
and landlineno REGEXP '^[0-9]+$'"
Now ln or mn can be anything and say ''. Since there are many instances where the firstname is "pooja" without a landlineno. So it fetches that entries too which has no landlineno but different mobileno..
When I use the following query
select id
from clientdetails
where (firstname = 'pooja' and mobileno = mn)
or (firstname = 'pooja' and landlineno= '' )
and mobileno REGEXP '^[0-9]+$'
and landlineno REGEXP '^[0-9]+$'"
It fetches me the required rows.
Please explain me the execution format of these queries
So as Alnitak pointed out in comments, There shouldn't be any difference - given three boolean variables
A, B, C, then A & (B | C) == (A & B) | (A & C)
Well, I have tested both queries with simple example below both queries gives same result.
create table emp_temp(id smallint(5),fname varchar(10),lname varchar(10));
insert into emp_temp values (1,'jon','kam'),(2,'ish','dalviv'),(3,'ctn','gado'),
(4,'jin','jain'),(5,'niraj','yadav');
select * from emp_temp;
mysql> select * from emp_temp;
+------+-------+--------+
| id | fname | lname |
+------+-------+--------+
| 1 | jon | kam |
| 2 | ish | dalviv |
| 3 | ctn | gado |
| 4 | jin | jain |
| 5 | niraj | yadav |
+------+-------+--------+
5 rows in set (0.00 sec)
Now comparing your two queries.
select * from mytable
where column1=x and (column2=y or column3=z)
select * from mytable
where (column1=x and column2=y) or (column1=x and column3=z)
Consider
column1 is id
column2 is fname
column3 is lname
mysql> select * from emp_temp
-> where id=1 and (fname='jon' or lname='yadav');
+------+-------+-------+
| id | fname | lname |
+------+-------+-------+
| 1 | jon | kam |
+------+-------+-------+
1 row in set (0.01 sec)
mysql> select * from emp_temp
-> where (id=1 and fname='jon') or ( id=1 and lname='yadav');
+------+-------+-------+
| id | fname | lname |
+------+-------+-------+
| 1 | jon | kam |
+------+-------+-------+
1 row in set (0.01 sec)
Both queries produces the same result.
Are any of the columns NULL?
true AND unknown => false
true OR unknown => true
etc.
I did not see if all combinations of NULL would cause the two expressions to differ, but it seems a possible culprit.
I'm trying to get a query to get the batchuuid from the batchTBL
that aren't in the JobBatchStatusTBL..
I've tried a couple of different queries trying to use something like:
select *
from BatchTBL as ba
left join JobBatchStatusTBL as j
on ba.BatchUUID=j.BatchUUID
join JobTBL as j2
on j.JobUUID=j2.JobUUID
where j.batchuuid IS NULL
and j.JobUUID = 'ecd0fab8-8bf1-83cc-b1d7-495034a55618';
but i'm screwing something up...
any thoughts?
thanks
mysql> select BatchName,BatchUUID from BatchTBL;
+-----------+--------------------------------------+
| BatchName | BatchUUID |
+-----------+--------------------------------------+
| aa | d288ff51-d045-d218-52fd-93e3523db85e |
| aa1 | a288ff51-d045-d218-52fd-93e3523db85e |
| aa3 | d188ff51-d045-d218-52fd-93e3523db85e |
| aaa3 | da88ff51-d045-d218-52fd-93e3523db85e |
| baa3 | db88ff51-d045-d218-52fd-93e3523db85e |
| z1 | 7eedfea4-c498-ed6e-f0dd-1397fe7dbd67 |
| d1 | 34781dba-d99c-82e3-f499-b55ded863f81 |
| nb | 1dd56d9c-daed-7f9f-c13b-246d2ec96513 |
| ds | cca9a771-b5ef-5926-4c26-21151215a800 |
| a1 | 1bb51584-e68a-21d1-a2df-e07707591b43 |
+-----------+--------------------------------------+
10 rows in set (0.00 sec)
mysql> select JobName,JobUUID from JobTBL;
+---------+--------------------------------------+
| JobName | JobUUID |
+---------+--------------------------------------+
| aa | 8afa9cf4-bf63-a4cd-3cd9-cbc6d17f84be |
| aa1 | ecd0fab8-8bf1-83cc-b1d7-495034a55618 |
+---------+--------------------------------------+
2 rows in set (0.00 sec)
mysql> select JobUUID,BatchUUID from JobBatchStatusTBL;
+--------------------------------------+--------------------------------------+
| JobUUID | BatchUUID |
+--------------------------------------+--------------------------------------+
| ecd0fab8-8bf1-83cc-b1d7-495034a55618 | d288ff51-d045-d218-52fd-93e3523db85e |
+--------------------------------------+--------------------------------------+
1 row in set (0.00 sec)
thanks
Your query puts an inner join on JobBatchStatusTbl to JobTbl, so rows that are in one but not the other will never be returned at all. You only need two of the tables for this:
SELECT *
FROM BatchTbl
WHERE JobUUID
NOT
IN (SELECT JobUUID
FROM JobBatchStatusTBL)
I should note that it's impossible to use the JobUUID in the WHERE clause as you're attempting in your initial query, because this is returning only batches for which there is no corresponding job, according to your post - which makes me wonder if your post was misworded, since you had a specific job uuid in your query?
On another note, you should never name tables, columns or anything else in a way that describes their schema type.
Try
select * from BatchTBL where JobUUID not in (select JobUUID from JobBatchStatusTBL)
If I understand what you are trying to do correctly. To "get a query to get the batchuuid from the batchTBL that aren't in the JobBatchStatusTBL"
use the not in SQL syntax. It is easier to write and read, and the optimizer will take care of the rest.
Select BatchName,BatchUUID from
BatchTBL where BatchUUID not in
(Select BatchUUID from JobBatchStatusTBL)
I have a query that retrieves some data, among those data I have some that are returned with a value like 0. I would like the query to NOT return the columns when that's the case.
How can we do such a thing?
Regards,
MEM
select <column_name> from <table_name> where <column_name> <> 0.0
Here is all the data in a sample database. Notice how there are 3 rows with one having a zero value for the num column.
mysql> select * from test_tbl;
+------+----------+
| num | some_str |
+------+----------+
| 0 | matt |
| 2 | todd |
| 3 | Paul |
+------+----------+
3 rows in set (0.00 sec)
Now lets use the where clause to specify the rows we want to ignore (it's a little bit of reverse logic because we are actually specifying what rows we want).
mysql> select * from test_tbl where num <> 0.0;
+------+----------+
| num | some_str |
+------+----------+
| 2 | todd |
| 3 | Paul |
+------+----------+
2 rows in set (0.00 sec)
Note: This will only work without getting messy if 0 is the only value you are worried about. A better way would be to allow nulls in your column and then you can check to see if they are non-null in the where clause.