Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I have a table which contains column Flag with values(Y,N) ,when I write select Query it should return me result as (Yes,No) .how to achieve that????Please Help
Try this:
SELECT CASE Flag
WHEN 'Y' THEN 'Yes'
ELSE 'No'
END AS Flag
FROM TableName
Syntax:
CASE column_name
WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
ELSE result
END
Read more about CASE here.
Try this
SELECT CASE Flag
WHEN 'Y' THEN 'Yes'
ELSE 'No'
END AS Flag
FROM TABLENAME
You can also try if condition apart from casing as suggested above:
SELECT IF(flag='Y','Yes','No') FROM mytable;
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
my requirement is to generate a mysql query as
select ss.column1 P1,ss.column2 P2,ss.column3 P3 from table1 ss;
i want to select column 2 field values only if it has a value in table1 else i want to insert a constant value into the P2 column. Can someone help me with framing a query for this.
COALESCE is the SQL function that let's you replace null values by something else. E.g.:
select
ss.column1 as p1,
coalesce(ss.column2, 'no value') as p2,
ss.column3 as p3
from table1 ss;
The data types must match however, so you can use the above when column2 is a text column. If it is numeric, you can replace null with a numeric value (e.g. with a zero) or you'd cast the columns' datatype.
Some examples:
coalesce(mytext, 'unknown')
coalesce(mynumber, 0)
coalesce(cast(mynumber as varchar), 'unknown')
coalesce(mydate, date(now()))
coalesce(date_format(mydate, '%Y-%m-%d'), 'unknown')
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How do I do the following pseudocode in MySQL:
if (a IS NOT NULL)
{
GROUP BY (id)
num = COUNT(*)
} else {
num = 0
}
Note sure what you are trying to do .....
if you want to count non-null value, check those examples, it would look like this
COUNT(NULLIF(a, ''))
Also consider use Case if you need more complete counting
CASE
WHEN a = 'something' COUNT(1)
ELSE NULL
EDIT
If you want to invoke some query, depend on some value, consider use store procedure/ function
This may be the code you are looking for:
select num := count(distinct id)
from t;
This will set num to 0 if there are no rows.
If you really want the comparison to NULL:
select num := (case when a is not null then count(distinct id) else 0 end)
from t;
Note this assumes that a and num are not columns in the database.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hey guys I got a problem I don't remember the command to select all values in a column which are greater than X for replacing them with 0... Would be nice if someone could help me
You're looking for UPDATE statement
UPDATE table_name
SET column_name = 0
WHERE column_name > X
Here is SQLFiddle demo
update myTable set myField = 0 where myField > x
try this
update tableX set colY = 0 where colY > 23;
Of course 23 can be parameterized as X if you want.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
SELECT ques_id,ans_desc
FROM answer
ORDER BY ans_desc
HAVING ans_desc=0
Use the ans_desc IS NULL predicate in the WHERE clause:
SELECT ques_id, ans_desc
FROM answer
WHERE ans_desc = 0 OR ans_desc IS NULL
ORDER BY ans_desc ;
This will give you only those that have either 0 or NULL values in the ans_desc column.
The WHERE ans_desc = 0 alone won't give the NULL values.
I don't think you even need 'order by ans_desc' when you need to select only the rows with NULL values.
You should do something like:
SELECT ques_id as `question`, ans_desc as `answer`
FROM answer
WHERE ans_desc is NULL;
Advantage of this query is that it doesn't use filesort.
See output of explain for the query you are using to know more.
Hope that helps.
0 is different from NULL and different from empty
SELECT ques_id,ans_desc
FROM answer
WHERE ISNULL(ans_desc) OR ans_desc = ''
ORDER BY ans_desc
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have realized that these two queries do not work if 'fk_building' is null. I don't understand why, and cant seem to find a good explanation as it is kind of hard to google. Can someone explain why the hell !=3 doesnt return everything that is not 3??? including null rows? Why do I need to use <=> ?
update floor set fk_building = 3 where fk_building != 3 and floor_id = 1;
or
select * from floor where fk_building != 3
do not work where fk_building is null.
I would recommend looking at the "Common Mistakes" section on NULL values on Wikipedia.
From the entry:
For example, a WHERE clause or conditional statement might compare a column's value with a constant. It is often incorrectly assumed that a missing value would be "less than" or "not equal to" a constant if that field contains Null, but, in fact, such expressions return Unknown
As users have suggested, you can use a null safe operator if your RDBMS allows it, or check for IS NULL.
use the null safe operator
update floor set fk_building = 3 where (not fk_building <=> 3) and floor_id = 1;
http://dev.mysql.com/doc/refman/5.5/en/comparison-operators.html
Rather than get pissed off, why not just do:
update floor set fk_building = 3 where (fk_building != 3 OR fk_building IS NULL) and floor_id = 1;
You can also use x <=> 3 to test for both.
EDIT: in your case it be 'not x <=> 3'