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
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
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 wish to extract only a number if it's between 8 and 12 digits long, otherwise I wish it to be a blank. However in the column of data there can be text which I want as a blank.
Have tried may alterations of the code below with different brackets, though I get an error
SELECT CASE WHEN
isnumeric(dbo.worksheet_pvt.MPRNExpected) = 0 THEN '' ELSE(
CASE WHEN(
len(dbo.worksheet_pvt.MPRNExpected) >= 8
AND len(dbo.worksheet_pvt.MPRNExpected) < 13
) THEN dbo.worksheet_pvt.MPRNExpected ELSE ''
END
) AS [ MPRN Expected ]
Assuming you are using SQL Server, I would suggest:
select (case when p.MPRNExpected not like '%[^0-9]%' and
len(p.MPRNExpected) between 8 and 12
then p.MPRNExpected
end) as MPRN_Expected
. . .
from dbo.worksheet_pvt p
Presumably, you don't want isnumeric(), because it allows characters such as '.', '-', and 'e' in the "number".
The problem with your code is that you have two case expressions and they are not terminated correctly.
As a note, in MySQL, you would use regular expressions:
select (case when p.MPRNExpected regexp '^[0-9]{8-12}$'
then p.MPRNExpected
end) as MPRN_Expected
. . .
from dbo.worksheet_pvt p
Try this query
SELECT CASE WHEN ISNUMERIC(COLUMN_NAME)=0 THEN '' ELSE
CASE WHEN (LEN(COLUMN_NAME)>=8 AND LEN(COLUMN_NAME)<13)
THEN COLUMN_NAME ELSE ''END END AS data FROM TABLE_NAME
Thanks.
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'
I wanna display multiple statements when a single condition is true in case..when statement in SQL.
Eg:
case when (condition is true) then
print "A"
print "B"
.
.
.
.
print "Z"
when (condition2 is true) then
print "Z"
print "Y"
.
.
.
.
print "A
end
Could anyone provide me the exact syntax for it please?
Thanks in advance.
If your condition is complex, you can move it to a subquery. That way you don't have to repeat it for each column:
select case when Condition = 1 then 'A' else 'B' end
, case when Condition = 1 then 'C' else 'D' end
, case when Condition = 1 then 'E' else 'F' end
, ...
from (
select *
, case
when ... complex condition ... then 1
else 0
end as Condition
from YourTable
) as SubQueryAlias
Another option is a union with a CTE (not available in all databases.) That allows you to write the expressions for both without case, and thanks to the CTE the condition is not repeated.
; with CteAlias as
(
select *
, case
when ... complex condition ... then 1
else 0
end as Condition
from YourTable
)
select 'A', 'C', 'E'
from CteAlias
where Condition = 1
union all
select 'B', 'D', 'F'
from CteAlias
where Condition = 0