im getting error when i tried to use subtring and charindex in my query
Concat('https://api.giscloud.com/1/layers/2985288/features/',f.fid,'/mosque_status_photos /',substring(f.mosque_status_photos,1,CharIndex(',',f.mosque_status_photos,0)-1),'?api_key=a2cdb99935fec159f9557b24fae90f00')
and also i tried case funcation bt still getting eror
case when isnull(f.mosque_status_photos,'0') is null
then '0' else
Concat('https://api.giscloud.com/1/layers/2985288/features/',f.fid,'/mosque_status_photos /',substring(f.mosque_status_photos,1,CharIndex(',',f.mosque_status_photos,0)- 1),'?api_key=a2cdb99935fec159f9557b24fae90f00') end
i used isnull Funcation as well bt still getting error
Concat('https://api.giscloud.com/1/layers/2985288/features/',f.fid,'/mosque_status_photos/',substring(isnull(f.mosque_status_photos,'0'),1,CharIndex(',',isnull(f.mosque_status_photos,'0'),0)-1),'?api_key=a2cdb99935fec159f9557b24fae90f00')
Your case statement is wrong for a start, it it converting all null values to '0' character and then checking if the result is null which will never be the case.
Probably what you want to something like (assuming you are looking for commas in the mosque_status_photos column..
CASE WHEN f.mosque_status_photos not like ','
OR CharIndex(',',f.mosque_status_photos,0) <=1
OR LEN(LTRIM(RTRIM(mosque_status_photos))) = 0
THEN '0'
ELSE [original code for valid entries here]
END
Related
I have the following MYSQL query which creates table as select statement but having a case statment to change field formats to numbered fields to compare:
CREATE TABLE wm.bochg
AS
SELECT
wca.bochg.BochgID,
wca.bochg.SecID,
wca.bochg.OldOutValue,
wca.bochg.NewOutValue,
case when NewOutValue+0.0 >= OldOutValue+0.0 then NewOutValue ELSE '' END AS test2
from wca.bochg
WHERE
wca.bochg.SecID in (select secid from client.pfisin where accid = 416)
AND (wca.bochg.BochgID is null or wca.bochg.BochgID =
(select subbochg.BochgID from wca.bochg as subbochg
where subbochg.secid=bochg.secid
AND subbochg.Actflag <> 'D'
AND subbochg.NewOutValue+0.0 >= subbochg.OldOutValue+0.0
order by subbochg.BochgID desc limit 1)
or wca.bochg.BochgID is NULL);
However I am getting warnings when this is created saying:
'Truncated incorrect DOUBLE value: '' '
This is due to my case statment and the and clause within my subquery. Is there a way to cater for this issue and get rid of the warning message above going forward? Thanks
You are mixing datatypes in the case statement. Use something else than a string '' for the ELSE part (NULL, 0 or anything that suits your purpose).
select IFNULL(idtag,'Not Available') parentidtag from authorisation;
Mysql query where the idtag default value is null, so i had write a condition ifnull is to show as 'not available' ,but it is showing as ' 'empty spaces in table(im using phpmyadmin 4.0 )
My guess is that the data in question is not really NULL, but rather is empty string ''. Instead, try the following query:
SELECT CASE WHEN IFNULL(idtag, '') = '' THEN 'Not Available' ELSE idtag END
FROM authorisation
Is there a way to update any return values in MySQL that come back as either NULL or blank to "Unknown" or any value? My NULLs and blanks exist because I'm joining quite a few tables together and sometimes records exist but are blank and sometimes records don't exist in the other tables at all.
I would prefer not to update the original table because I don't save my result each time I run the query -- I just copy and paste the return into Excel before I send out the report.
Basically, it's just getting annoying sending this out multiple times a day and after pasting into Excel hitting Ctrl+F and replacing anywhere that says "NULL" with "Unknown" and then doing it again to replace any blank cell with "Unknown."
After looking around, I found IFNULL which obviously works if it's NULL but not blank -- but (1) it would be great to not have to wrap every part of my SELECT statement with IFNULLs if possible and (2) use something to encompass the blanks as well.
Just hoping there's something that I could put at the end of the query or something that I can't find. But it might just not exist within the way I'm doing it. I don't think this question needs code or schema because of the general-ness of it, but I'm certainly happy to get more specific if it helps. Thanks!
You could wrap all of the columns that could be null in a case statement that checks if any of them are null or blank before returning them.
For example:
SELECT
CASE WHEN firsttable.column1 IS NULL or firsttable.column1 = '' THEN 'Unknown' ELSE firsttable.column1 END AS firsttable.column1
CASE WHEN firsttable.column2 IS NULL or firsttable.column2 = '' THEN 'Unknown' ELSE firsttable.column2 END AS firsttable.column2
CASE WHEN secondtable.column1 IS NULL or secondtable.column1 = '' THEN 'Unknown' ELSE secondtable.column1 END AS firsttable.column1
CASE WHEN secondtable.column2 IS NULL or secondtable.column1 = '' THEN 'Unknown' ELSE secondtable.column2 END AS firsttable.column2
FROM
firsttable,
secondtable
WHERE
firsttable.id = secondtable.firsttableid
You can use IF in combination with COALESCE:
SET #val := '';
select IF(COALESCE(#val,'')='','Unknown',#val);
Returns 'Unknown', the same will returns for NULL. Any other value will be returned:
SET #val := 'test';
select IF(COALESCE(#val,'')='','Unknown',#val);
Returns 'test'.
Trick is in COALESCE - it returns first non-NULL value, so COALESCE(NULL,'') will return '' and COALESCE('','') will return '' too. You can replace variable #val with any column from table or view etc.
I have my Mysql Query as :
select RecordID,ID,Name,Description,RecordDateTimeUTC,Location from
eemployee where RecordID like ? and(isNull(RecordDateTimeUTC) OR RecordDateTimeUTC= CASE WHEN COALESCE(?,'') = '' THEN RecordDateTimeUTC ELSE ? END)........
Now my query There are lott of fields(? --> this is where i am putting my data for filter perpose) upon which user can query so i am using coalesce but the disadvantage of using coalesce is it doesn't give me null values when i want to get all data present in employee table , so to get null values i have used isNull function.
Now i am testing the above query for two use cases
1-->User can pass percentage in RecordID:-In this case he should get all data either null as well as not null values :
select RecordID,ID,Name,Description,RecordDateTimeUTC,Location from
eemployee where RecordID like '%'
and(isNull(RecordDateTimeUTC) OR RecordDateTimeUTC= CASE WHEN COALESCE('','') = '' THEN RecordDateTimeUTC ELSE '' END)
2-->User can filter data based on RecordDateTimeUTC:- If user passes this parameter then he should get all not null data satisfying the above filteration.:-
select RecordID,ID,Name,Description,RecordDateTimeUTC,Location from
eemployee where RecordID like '%' and(isNull(RecordDateTimeUTC) OR RecordDateTimeUTC= CASE WHEN COALESCE('2012-07-09 11:11:00','') = '' THEN RecordDateTimeUTC ELSE '2012-07-09 11:11:00' END)
For the first use case it is working fine but for the second use case it is giving me filtered data as well as null Data
So what should be my query so that both my use cases are supported by the above single query.
I am using MYSQl Query Browser to test my query
Thanks in advance
It looks like you're trying to make parameters optional while using a single static SQL statement. In that case I would imagine you mean something like:
SELECT RecordID,ID,Name,Description,RecordDateTimeUTC,Location
FROM eemployee
WHERE (#RecordId IS NULL OR RecordID LIKE #RecordId)
AND (#RecordDateTimeUTC IS NULL OR RecordDateTimeUTC = #RecordDateTimeUTC)
If the parameter value is NULL, it will be omitted completely from the filter, otherwise the passed value will be used.
Note you can't use ? here because of the need to re-use the parameter - unless you want to specify each parameter twice, that is. Specifying a name makes the query a lot more readable IMHO.
How do you replace a NULL value in the select with an empty string?
It doesn't look very professional to output "NULL" values.
This is very unusual and based on my syntax I would expect it to work.
I'm hoping for an explanation why it doesn't.
select CASE prereq WHEN (prereq IS NULL) THEN " " ELSE prereq end from test;
Example of what the original table looks like, what I want, and what actually prints:
original wanted what actually prints
-------- ------ ---------------------
value1 value1
NULL NULL
value2 value2
NULL NULL
As you can see it does the opposite of what I want, hence I tried flipping the IS NULL to IS NOT NULL and of course that didn't fix it. I also tried swapping the position of when case, which did not work.
It seems the 3 solutions given below all do the task.
select if(prereq IS NULL ," ",prereq ) from test
select IFNULL(prereq,"") from test
select coalesce(prereq, '') from test
If you really must output every values including the NULL ones:
select IFNULL(prereq,"") from test
SELECT COALESCE(prereq, '') FROM test
Coalesce will return the first non-null argument passed to it from left to right. If all arguemnts are null, it'll return null, but we're forcing an empty string there, so no null values will be returned.
Also note that the COALESCE operator is supported in standard SQL. This is not the case of IFNULL. So it is a good practice to get use the former. Additionally, bear in mind that COALESCE supports more than 2 parameters and it will iterate over them until a non-null coincidence is found.
Try below ;
select if(prereq IS NULL ," ",prereq ) from test
Some of these built-in functions should work:
COALESCE(value,...)
Returns the first non-NULL value in the list, or NULL if there are no non-NULL values.
IS NULL
Tests whether a value is NULL.
IFNULL(expr1,expr2)
If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2.
select IFNULL(`prereq`,'') as ColumnName FROM test
this query is selecting "prereq" values and if any one of the values are null it show an empty string as you like
So, it shows all values but the NULL ones are showns in blank
The original form is nearly perfect, you just have to omit prereq after CASE:
SELECT
CASE
WHEN prereq IS NULL THEN ' '
ELSE prereq
END AS prereq
FROM test;
Try COALESCE. It returns the first non-NULL value.
SELECT COALESCE(`prereq`, ' ') FROM `test`
Try this, this should also get rid of those empty lines also:
SELECT prereq FROM test WHERE prereq IS NOT NULL;
I have nulls (NULL as default value) in a non-required field in a database. They do not cause the value "null" to show up in a web page. However, the value "null" is put in place when creating data via a web page input form. This was due to the JavaScript taking null and transcribing it to the string "null" when submitting the data via AJAX and jQuery. Make sure that this is not the base issue as simply doing the above is only a band-aid to the actual issue. I also implemented the above solution IFNULL(...) as a double measure. Thanks.
UPDATE your_table set your_field="" where your_field is null