SQL: How to replace an entire field if it matches a string? - mysql

I am trying to replace an entire field from a database when it matches a certain string.
For example:
TABLE_FRUITS contains
ID NAME
---------------
1 APPLE
2 ORANGE
3 PASSIONFRUIT
4 BANANA
5 DRAGONFRUIT
6 KIWI
7 STRAWBERRY FRUIT
Now If I try to select column NAME, and if a field contains the string 'FRUIT', it should replace that whole field to another string, like 'SAMPLE'.
Expected result:
select NAME from TABLE_FRUITS;
would return:
APPLE
ORANGE
SAMPLE
BANANA
SAMPLE
KIWI
SAMPLE
I am not sure if I should use replace or substr/instr.

If you're using MySQL, you could use something like
SELECT IF(NAME LIKE '%FRUIT%', 'SAMPLE', NAME) AS NAME FROM TABLE_FRUIT
In other varieties of SQL you will probably need to use a CASE expression:
SELECT CASE WHEN NAME LIKE '%FRUIT%' THEN 'SAMPLE' ELSE NAME END AS NAME FROM TABLE_FRUIT

Use case when:
select case when name like '%fruit%' then 'Sample' else name end from tablename

You can use CASE
select case when name like '%FRUIT%' then 'sample' else name end
from table_fruit

Related

Showing all column names where value is null

is there anyway i can show all column names where values are null?
For example i have table like this:
id
name
surname
1
Jack
NULL
2
NULL
Grain
3
NULL
NULL
And i want my result to look like that:
id
nullFields
1
name
2
surname
3
name, surname
Perfect solution would be some sql which takes all the columns and check them (if there wouldnt be need to manually input column name) but if there is no such possibility "normal solution" will do fine.
We can use the base string functions here:
SELECT id, CONCAT_WS(', ',
CASE WHEN name IS NULL THEN 'name' END,
CASE WHEN surname IS NULL THEN 'surname' END) AS nullFields
FROM yourTable
ORDER BY id;

Define a custom ORDER BY order in mySQL using the LIKE word

How can I make a custom order where I sort the rows by NAME where the first rows' name has Bob in it followed by rows with name of Alex in it?
To explain what exactly I mean: I have made the following query to sort result if NAME = 'Bob' and if NAME = 'Alex':
SELECT * FROM table
ORDER BY CASE `NAME`
WHEN 'Bob' THEN 1
WHEN 'Alex' THEN 2
ELSE 3
END
But this only works when the NAME is exactly equal to Bob or Alex. I want to modify it to sort if the NAME has Bob or Alex in it, essentially if NAME LIKE '%Bob%' and NAME LIKE '%Alex%'. I tried something like the following but it does not work.
ORDER BY CASE `NAME`
WHEN LIKE '%Bob%' THEN 1
WHEN LIKE '%Alex%' THEN 2
ELSE 3
END
What is the correct syntax for this?
Use the other form of CASE where you specify a condition in WHEN rather than a value.
ORDER BY CASE
WHEN NAME LIKE '%Bob%' THEN 1
WHEN NAME LIKE '%Alex%' THEN 2
ELSE 3
END

How to show table row data as a column?

I have faced problem in selection data from database.
Main table:
TYPE DATE
APPLE 2013-10-02
BANANA 2013-2-4
KIWI 2014-10-2
I want to show
APPLE BANANA KIWI
2013-10-02 2013-2-4 2014-10-2
How can i do this ?
In Excel this function is called Transpose.
You have one good example and working code here:
https://dba.stackexchange.com/questions/47902/how-to-transpose-convert-rows-as-columns-in-mysql
Try this
SELECT CASE WHEN Type = 'APPLE' Then Date End APPLE,
CASE WHEN Type = 'BANANA' Then Date End BANANA,
CASE WHEN Type = 'KIWI' Then Date End KIWI,
From Table1

Sorting special name to end in database

I want to sort a mysql-table
select id,name from tbl order by name asc;
returns
1 name1
2 name2
4 name3
5 name4
8 name5
How to order that e.g name 3 goes to end of the table like
select id,name from tbl order by ["name is name3????"],name asc;
returns
1 name1
2 name2
5 name4
8 name5
4 name3
Thank you
With case you can return a value based on a condition. So you can return 1 for name3 and 0 for other names. Primarily sort on this value to put name3 in the back. The secondary sort value is the name, so that the other names are still sorted alphabetically.
select
id, name
from
tbl
order by
case when name = 'name3' then 1 else 0 end,
name
Strawberry just taught me in the comment that you could also use the function field to accomplish this. This is especially handy and more compact, if you want to specify a specific sort for a number of names. Using case that would quickly become bulky. Field returns the index of the first string in the list of other strings, so field(name, 'name3', 'name4') would return 1 for 'name3' and 2 for 'name4' and 0 for all other names.
Your query would then look like this:
select
id, name
from
tbl
order by
field(name, 'name3'),
name

Mysql: Order by like?

assume that we are performing search using keywords: keyword1, keyword2, keyword3
there are records in database with column "name":
1: John Doe
2: Samuel Doe
3: John Smith
4: Anna Smith
now Query:
SELECT * FROM users WHERE (name LIKE "%John%" OR name LIKE "%Doe%")
it will select records: 1,2,3 (in this order)
but i want to order it by keyword
in example keyword1=John, keyword2=Doe
so it should be listed by keywords: 1,3,2 (because i want to perform search for "Doe" after searching for "John")
I was thinking about SELECT DISTINCT FROM (...... UNION .....)
but it will be much easier to order it somehow in another way (real query is really long)
are there any tricks to create such order?
order by case
when name LIKE "%John%" then 1
when name LIKE "%Doe%" then 2
else 3
end
To build on RedFilter's answer, you could make the rows that have both keywords to be at the top:
order by case
when (name LIKE "%John%" and name LIKE "%Doe%") then 1
when name LIKE "%John%" then 2
when name LIKE "%Doe%" then 3
end
Read up on Boolean Fulltext Searches, with which you can do ordering.
SELECT *
from
(
SELECT u.*, 1 OrderNum
FROM users
WHERE (name LIKE "%John%")
UNION
SELECT u.*, 2 OrderNum
FROM users
WHERE (name LIKE "%Doe%")
)
Order by OrderNum
My example will Order all of the John's Alphabetically followed by the Doe's.
ORDER BY CASE
WHEN name LIKE "John%Doe" THEN CONCAT('a',name)
WHEN name LIKE "John%" THEN CONCAT('b',name)
WHEN name LIKE "%Doe" THEN CONCAT('c',name)
ELSE name
END