Add some text on result of MySQL Case Operator - mysql

How I can add some text on result of MySQL Case Operator?
I would like to get some result like this:
I try this but get a error syntax:
SELECT (CASE
WHEN job_url_outside IS NULL
THEN ''
ELSE ''
END) AS job_url
FROM job

Probably you want to concatenate some strings? Then use the following query, where CONCAT is added to do the concatenation:
SELECT (CASE
WHEN job_url_outside IS NULL
THEN CONCAT('')
ELSE CONCAT('')
END) AS job_url
FROM job

You should string concatenate the href string together.
SELECT (CASE WHEN job_url_outside IS NULL THEN '' ELSE '' END) AS job_url FROM job

Try use:
CONCAT(column,'some text',column)
More information here
In your case it will be like this:
SELECT (CASE
WHEN job_url_outside IS NULL
THEN CONCAT('')
ELSE CONCAT('')
END) AS job_url
FROM job;
DEMO here

Related

IF condition doesn't work properly

I would like to write a query using IF, but its not working , what's wrong with this query?
SELECT
IF(Emp.Name is not null)
((Emp.Name) + '_' + (Emp.LastName)) as ID
else
Emp.ID
I get this error:
Incorrect syntax near the keyword 'IF'.
Why is that?
Thank you.
You can use CASE EXPRESSION :
SELECT CASE WHEN emp.name is not null THEN emp.name + '_' + emp.lastName
ELSE Emp.id
END as [ID]
FROM ...
The IF() is performed differently in SQL-Server (I assume by the concat syntax),
IF ( Condition )
SQL STATEMENT
ELSE
SQL STATEMENT
Which means you need to perform the entire select in each block. Your kind of IF() is used in MySQL , IF(Condition , THEN , ELSE )
Try using Case in your query check this MySQL: CASE Function
You need to do this in a CASE clause:
SELECT
CASE WHEN (Emp.Name IS NOT NULL)
THEN ((Emp.Name) + '_' + (Emp.LastName))
ELSE
Emp.ID
END as ID
The IF..ELSE syntax is somewhat different:
IF(Emp.Name IS NOT NULL)
SELECT ((Emp.Name) + '_' + (Emp.LastName)) AS ID
ELSE
SELECT Emp.ID AS ID
Assuming it's SQL Server based on your syntax use this:
SELECT CASE <variable> WHEN <value> THEN <returnvalue>
WHEN <othervalue> THEN <returnthis>
ELSE <returndefaultcase>
END AS <newcolumnname>
FROM <table>
Instead of usinf IF, you can use CASE in SELECT statement. Something like this
SELECT
CASE Emp.Name
WHEN NULL THEN Emp.ID
ELSE CONCAT(CONCAT(Emp.Name,'_'),Emp.LastName)
END AS "ID"
FROM Emp;
Hope it helps.

Can Sybase CASE expressions have a default column name for their result?

I have a sybase query that is structured like this:
SELECT
case
when isnull(a,'') <> '' then a
else convert(varchar(20), b)
end
FROM table_name
WHERE b=123
It used to return the results of the 'case' in a column named 'converted'. It now returns the results of the 'case' in a column with an empty string name ''.
How could this be? Could there be some database configuration that defaults the results of a 'case' with no name?
(I've fixed the broken query by adding " as computed" after 'end' but now I'd like to know how it used to return as 'computed' before I added the fix?)
Is this what you want?
SELECT (case when isnull(a, '') <> '' then a
else convert(varchar(20), b)
end) as converted
-------------^
FROM table_name
WHERE b = 123;
By the way, you could write the select more succinctly as:
SELECT coalesce(nullif(a, ''), b) as converted

Using a field of view in the view's sub query

I have a view which has a column which is calculated from other columns. For example, in my original table I have the columns A and B from which I calculate the value of column X in my view. I need the value of X in another column I have in the view - Z. But when I put it in the sub-query of my view I get an Unknown column 'X' in 'field list' SQL statement.
The sub-query of the view looks like this:
(CASE
WHEN (`users`.`A` REGEXP '^(regexone)') THEN 'ValueA'
WHEN (`users`.`B` REGEXP '^(regextwo)') THEN 'ValueB'
ELSE ''
END) AS `X`,
(CASE
WHEN
(`X` = 'ValueA')
THEN
(`users`.`C`*0.95-0.3)
ELSE ''
END) AS 'Z'
What's the correct syntax of using a computed view field in the computation of another field?
if you can you user-defined variables use like
SELECT
#x := (CASE
WHEN (`users`.`A` REGEXP '^(regexone)') THEN 'ValueA'
WHEN (`users`.`B` REGEXP '^(regextwo)') THEN 'ValueB'
ELSE ''
END) AS `X`,
(CASE
WHEN
(#x = 'ValueA')
THEN
(`users`.`C`*0.95-0.3)
ELSE ''
END) AS 'Z'

How to check if field is null or empty in MySQL?

I am trying to figure out how to check if a field is NULL or empty. I have this:
SELECT IFNULL(field1, 'empty') as field1 from tablename
I need to add an additional check field1 != "" something like:
SELECT IFNULL(field1, 'empty') OR field1 != "" as field1 from tablename
Any idea how to accomplish this?
Either use
SELECT IF(field1 IS NULL or field1 = '', 'empty', field1) as field1
from tablename
or
SELECT case when field1 IS NULL or field1 = ''
then 'empty'
else field1
end as field1
from tablename
If you only want to check for null and not for empty strings then you can also use ifnull() or coalesce(field1, 'empty'). But that is not suitable for empty strings.
Using nullif does the trick:
SELECT ifnull(nullif(field1,''),'empty') AS field1
FROM tablename;
How it works: nullif is returning NULL if field is an empty string, otherwise returns the field itself. This has both the cases covered (the case when field is NULL and the case when it's an empty string).
Alternatively you can also use CASE for the same:
SELECT CASE WHEN field1 IS NULL OR field1 = ''
THEN 'empty'
ELSE field1 END AS field1
FROM tablename.
You can use the IFNULL function inside the IF. This will be a little shorter, and there will be fewer repetitions of the field name.
SELECT IF(IFNULL(field1, '') = '', 'empty', field1) AS field1
FROM tablename
You can create a function to make this easy.
create function IFEMPTY(s text, defaultValue text)
returns text deterministic
return if(s is null or s = '', defaultValue, s);
Using:
SELECT IFEMPTY(field1, 'empty') as field1
from tablename
By trimming and comparing, we ensure we also take care of empty or tab or space character content in the field.
SELECT
CASE
WHEN LTRIM(RTRIM(col_1))='' or col_1 IS NULL THEN 'Not available'
ELSE col_1
END AS col_alias
FROM
my_table
SELECT * FROM (
SELECT 2 AS RTYPE,V.ID AS VTYPE, DATE_FORMAT(ENTDT, ''%d-%m-%Y'') AS ENTDT,V.NAME AS VOUCHERTYPE,VOUCHERNO,ROUND(IF((DR_CR)>0,(DR_CR),0),0) AS DR ,ROUND(IF((DR_CR)<0,(DR_CR)*-1,0),2) AS CR ,ROUND((dr_cr),2) AS BALAMT, IF(d.narr IS NULL OR d.narr='''',t.narration,d.narr) AS NARRATION
FROM trans_m AS t JOIN trans_dtl AS d ON(t.ID=d.TRANSID)
JOIN acc_head L ON(D.ACC_ID=L.ID)
JOIN VOUCHERTYPE_M AS V ON(T.VOUCHERTYPE=V.ID)
WHERE T.CMPID=',COMPANYID,' AND d.ACC_ID=',LEDGERID ,' AND t.entdt>=''',FROMDATE ,''' AND t.entdt<=''',TODATE ,''' ',VTYPE,'
ORDER BY CAST(ENTDT AS DATE)) AS ta
If you would like to check in PHP , then you should do something like :
$query_s =mysql_query("SELECT YOURROWNAME from `YOURTABLENAME` where name = $name");
$ertom=mysql_fetch_array($query_s);
if ('' !== $ertom['YOURROWNAME']) {
//do your action
echo "It was filled";
} else {
echo "it was empty!";
}

Update Database field value by Alphabetical order

Reorder rows
A row in my database it in a random order with the following characters
HFMNLBX#&I
It was input weirdly and the rows are like HF and FH, which are both equivalent to the system. Is there a way to update all of the rows to go in alphabetical order, then the characters on the end?
Thanks
Here is a way to alphabetize the characters in a column:
select concat((case when col like '%A%' then 'A' else '' end),
(case when col like '%B%' then 'B' else '' end),
. . .
(case when col like '%Z%' then 'Z' else '' end)
) as newcol
from t
Note that this does not handle duplicate letters.
I'm not sure exactly what you mean by "characters on the end". You can use a subquery, for instance, to handle just a subset of them.
Or, if you want to keep everything after the #, something like:
select concat((case when col like '%A%#%' then 'A' else '' end),
(case when col like '%B%#%' then 'B' else '' end),
. . .
(case when col like '%Z%#%' then 'Z' else '' end),
substring(col, locate('#', col) - 1)
) as newcol
from t