Good day, I have a problem wherein the value of days are formatted with MTWThF, ThF, TTh and so on. I am trying to convert into columns. this is my code:
SELECT
dys,
CASE WHEN dys LIKE 'M%' THEN 'M' ELSE '' END AS M,
CASE WHEN (dys LIKE '%T%' AND NOT '%Th%') THEN 'T' ELSE '' END AS T,
CASE WHEN dys LIKE '%W%' THEN 'W' ELSE '' END AS W,
CASE WHEN dys LIKE '%Th%' AND NOT 'T' THEN 'Th' ELSE '' END AS th,
CASE WHEN dys LIKE '%F%' THEN 'F' ELSE '' END AS F,
CASE WHEN (dys LIKE '%S%' AND NOT 'Su') THEN 'S' ELSE '' END AS S,
CASE WHEN (dys LIKE '%Su%' AND NOT 'S') THEN 'Su' ELSE '' END AS Su
FROM sched
GROUP BY dys
and the results are
Tb result
T is till detected on ThF same S from SSu. Do i missing something? Thank you
Related
I'm having trouble writing the regular expression to do what I need in MySQL syntax.
I have the following value for column http_referer in a database table:
https://www.example.com?id=123&true
And I need to return the value of the query string parameter uid (in this case, 123), to plug in to another query.
`SELECT * FROM sites WHERE id = (http_referer REGEXP 'uid=([0-9]+)&?')
This is my query, that doesn't work, probably because I'm trying to pass in a PHP-style regular expression instead of one MySQL can use (however, I understand that MySQL doesn't even support capture groups, so I'm kind of stumped).
If you use MySQL prior to version 8 which doesn't support REGEXP_REPLACE(), you can use SUBSTRING_INDEX() to extract parts of a string:
mysql> set #a = 'some.domain?id=123&true';
mysql> select substring_index( substring_index(#a, '?id=', -1), '&', 1) as result;
+--------+
| result |
+--------+
| 123 |
+--------+
If the position of the parameter whose value you are interested in is not fixed, i.e. not always the first, or if parameters following it are optional and may not be present, it's a bit more tricky and you have to add more logic.
Old topic but this solution might help for people who don't have mysql version 8.
2 notes:
You need a case set for each character. So this example is for
maximum of 3 characters (0 -999).
You can't use this solution when
your string has multiple occurrences. For example
id=123&secondid=456&anotherid=789.
SELECT
CONCAT(
CASE WHEN content LIKE '%id=?1%' THEN '1' ELSE '' END,
CASE WHEN content LIKE '%id=?2%' THEN '2' ELSE '' END,
CASE WHEN content LIKE '%id=?3%' THEN '3' ELSE '' END,
CASE WHEN content LIKE '%id=?4%' THEN '4' ELSE '' END,
CASE WHEN content LIKE '%id=?5%' THEN '5' ELSE '' END,
CASE WHEN content LIKE '%id=?6%' THEN '6' ELSE '' END,
CASE WHEN content LIKE '%id=?7%' THEN '7' ELSE '' END,
CASE WHEN content LIKE '%id=?8%' THEN '8' ELSE '' END,
CASE WHEN content LIKE '%id=?9%' THEN '9' ELSE '' END,
CASE WHEN content LIKE '%id=?0%' THEN '0' ELSE '' END,
CASE WHEN content LIKE '%id=?_1%' THEN '1' ELSE '' END,
CASE WHEN content LIKE '%id=?_2%' THEN '2' ELSE '' END,
CASE WHEN content LIKE '%id=?_3%' THEN '3' ELSE '' END,
CASE WHEN content LIKE '%id=?_4%' THEN '4' ELSE '' END,
CASE WHEN content LIKE '%id=?_5%' THEN '5' ELSE '' END,
CASE WHEN content LIKE '%id=?_6%' THEN '6' ELSE '' END,
CASE WHEN content LIKE '%id=?_7%' THEN '7' ELSE '' END,
CASE WHEN content LIKE '%id=?_8%' THEN '8' ELSE '' END,
CASE WHEN content LIKE '%id=?_9%' THEN '9' ELSE '' END,
CASE WHEN content LIKE '%id=?_0%' THEN '0' ELSE '' END,
CASE WHEN content LIKE '%id=?__1%' THEN '1' ELSE '' END,
CASE WHEN content LIKE '%id=?__2%' THEN '2' ELSE '' END,
CASE WHEN content LIKE '%id=?__3%' THEN '3' ELSE '' END,
CASE WHEN content LIKE '%id=?__4%' THEN '4' ELSE '' END,
CASE WHEN content LIKE '%id=?__5%' THEN '5' ELSE '' END,
CASE WHEN content LIKE '%id=?__6%' THEN '6' ELSE '' END,
CASE WHEN content LIKE '%id=?__7%' THEN '7' ELSE '' END,
CASE WHEN content LIKE '%id=?__8%' THEN '8' ELSE '' END,
CASE WHEN content LIKE '%id=?__9%' THEN '9' ELSE '' END,
CASE WHEN content LIKE '%id=?__0%' THEN '0' ELSE '' END
) id
FROM 'page?id=123' test WHERE test LIKE '%?id=%' and p.domain_id=18
I have written a query with CASE but I am getting () problem.
select SM.subscriber_name as name ,
SM.accountType as accountType,
SM.middlename as middleName,
SM.lastname as lastName,
SM.title as title,
SM.email as email,
SM.company as company,
SM.email1 as aEmail,
,
SM.zipcode as zipcode,
SM.phone_no as phoneNumber,
SM.landlinenumber as landlineNumber,
SM.login_id as loginId,
SD.subscriberType as subscriptionType,
SD.product_id as productType,
case SM.state when 'null' then '' as state else STDD.state_name as state end,
case SM.city when 'null' then '' as city else CDD.city_name as city end,
case SM.country when 'null' then '' as country else CD.country_name as country end,
SD.fulldownloadaccess as fullDownloadAccess,
SD.emailid_limit as emailLimit,
SD.acessTime as planTime
from subscriber_master SM ,
subsciber_details SD,
city_details CDD,
state_details STDD,
country_details CD
where SM.subscriber_id=16704 and
SM.subscriber_id=SD.subscriber_id and
SM.country = CD.country_id and
SM.state = STDD.state_id and
SM.city = CDD.city_id;
Please help me where I can put the brackets
There are a couple of problems with your query. Firstly your CASE expressions are written incorrectly, the expression must be complete before any AS part (see the manual) so for example,
case SM.state when 'null' then '' as state else STDD.state_name as state end,
should be written as
case SM.state when 'null' then '' else STDD.state_name end as state,
Additionally if you are checking for a NULL value, rather than a string value of 'null', you need to write the CASE expression as:
case when SM.state IS NULL then '' else STDD.state_name end as state
Also part way down you have an extra , (between SM.email1 as aEmail, and SM.zipcode as zipcode,).
However none of these problems will give you the message in your title, is there some code you haven't shown us?
I would say you are interested to show the NULL's as empty values, so, you need to rebuild your query like:
CASE sm.state
WHEN 'null' THEN '' AS state
ELSE stdd.state_name AS state
end,
To be rewritten as:
CASE
WHEN sm.state IS NULL THEN ''
ELSE stdd.state_name END AS state,
And the same for all 'NULL'. Also, the AS should be written after the END of the CASE
REVIEWED: Just realised that the sentence below should return the same but is much more compact and easier to read:
ISNULL(sm.state, '') AS state,
You can change your case statement as shown below it will check both null and blank values
case ISNULL(SM.[state],'') when '' then '' else STDD.state_name end as [state],
case ISNULL(SM.city,'') when '' then '' else CDD.city_name end as city,
case ISNULL(SM.country,'') when '' then '' else CD.country_name end as country
You should use join for providing where conditions between different tables in select statement. Alternatively you can use the below modified query
select SM.subscriber_name as name ,
SM.accountType as accountType,
SM.middlename as middleName,
SM.lastname as lastName,
SM.title as title,
SM.email as email,
SM.company as company,
SM.email1 as aEmail,
SM.zipcode as zipcode,
SM.phone_no as phoneNumber,
SM.landlinenumber as landlineNumber,
SM.login_id as loginId,
SD.subscriberType as subscriptionType,
SD.product_id as productType,
case ISNULL(SM.[state],'') when '' then '' else STDD.state_name end as [state],
case ISNULL(SM.city,'') when '' then '' else CDD.city_name end as city,
case ISNULL(SM.country,'') when '' then '' else CD.country_name end as country,
SD.fulldownloadaccess as fullDownloadAccess,
SD.emailid_limit as emailLimit,
SD.acessTime as planTime
from subscriber_master SM,
subsciber_details SD,
city_details CDD,
state_details STDD,
country_details CD
where SM.subscriber_id=16704
and SM.subscriber_id=SD.subscriber_id and
SM.country = CD.country_id and
SM.[state] = STDD.state_id and
SM.city = CDD.city_id;
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
I am writing a query to display an alias column with respect to a column value.
below is my code
CASE TRIM(channel_id)
WHEN '' THEN 'General'
ELSE 'Specific'
END AS templateType
When the column channel id is empty/null the templateType column should show 'General'
else should show 'Specific'
I am getting wrong output
Can anyone help me please..?
CASE TRIM(IFNULL(channel_id,''))
WHEN '' THEN 'General'
ELSE 'Specific'
END AS templateType
Try this..
CASE WHEN channel_id > '' THEN
'Specific'
ELSE
'General'
END As templateType
Aside:
SELECT CASE WHEN '' = ' ' THEN 'same' ELSE 'different' END
Results:
same
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