I have strings that are 6 characters in length and I need to test if the first character and third are the same, or the 1st and 5th or 3rd and 5th. The strings contain letters and numbers
So
aabbcc --> false
abbcad --> true
aaabcd --> true
bacada --> false
1a1b33 --> true
I need this to be part of a mysql query. Help is greatly appreciated!
The simplest (and probably fastest) way is to compare the individual substrings:
SELECT str,
SUBSTR(str, 1, 1) = SUBSTR(str, 3, 1) OR
SUBSTR(str, 1, 1) = SUBSTR(str, 5, 1) OR
SUBSTR(str, 3, 1) = SUBSTR(str, 5, 1) AS matching
FROM data
Output
str matching
aabbcc 0
abbcad 1
aaabcd 1
bacada 0
1a1b33 1
If you are running MySQL 8+ you can take advantage of the enhanced regex capability to use back-references in the pattern:
SELECT str,
REGEXP_LIKE(str, '^(.).\\1') OR
REGEXP_LIKE(str, '^(.)...\\1') OR
REGEXP_LIKE(str, '^..(.).\\1') AS matching
FROM data
Output is the same as the previous query:
str matching
aabbcc 0
abbcad 1
aaabcd 1
bacada 0
1a1b33 1
Demo on dbfiddle
You can use substring().
...
WHERE substring(nmuloc, 1, 1) = substring(nmuloc, 3, 1)
OR substring(nmuloc, 1, 1) = substring(nmuloc, 5, 1)
OR substring(nmuloc, 3, 1) = substring(nmuloc, 5, 1)
...
Related
Why do these queries give different results? Reference is a single character column and I would expect to have a result giving counts for upper and lower case letter 'r'.
Select SUM(IIF(StrComp([REFERENCE],'R',0) = 0, 1, 0)) AS BIG_R,
SUM(IIF(StrComp([REFERENCE],'r',0) = 0, 1, 0)) AS LITTLE_R
From [SYMREF]
Where [PROGRAM] = 'SOMEPROGRAM'
The result is that both BIG_R and LITTLE_R are the same and equal the count of BIG_R's
However,
Select SUM(IIF(StrComp([REFERENCE],'r',0) = 0, 1, 0)) AS LITTE_R,
SUM(IIF(StrComp([REFERENCE],'R',0) = 0, 1, 0)) AS BIG_R
From [SYMREF]
Where [PROGRAM] = 'SOMEPROGRAM'
Again LITTLE_R and BIG_R are the same, but this time they equal the count of LITTLE_R's
This looks like a bug in the way MS Access processes this type of query, or have I missed something here?
Access (or probably rather JetEngine) thinks that StrComp is called twice with the same argument and optimizes away one of the two calls.
A workaround is to compare the ASCII character values (Asc("r") = 114, Asc("R") = 82):
Select
SUM(IIF(Asc([REFERENCE]) = Asc('R'), 1, 0)) AS BIG_R,
SUM(IIF(Asc([REFERENCE]) = Asc('r'), 1, 0)) AS LITTLE_R
From [SYMREF]
Where [PROGRAM] = 'SOMEPROGRAM'
Yet another workaround:
Select SUM(IIF(StrComp([REFERENCE],Chr$(82),0) = 0, 1, 0)) AS BIG_R,
SUM(IIF(StrComp([REFERENCE],Chr$(114),0) = 0, 1, 0)) AS LITTLE_R
From [SYMREF]
Where [PROGRAM] = 'SOMEPROGRAM'
Here the two inputs to StrComp are clearly different. So, the second call not optimized away.
I have a column which I am trying to convert in MySQL into another column with a pattern where ever there are consecutive 1s in the data. Please see the example dataset below
Dataset Sample: https://1drv.ms/x/s!ApGNZAoiMmX3gi9OR7SUxt3ou84v?e=tuSV7f
Following is the code I have written but not able to make it work and any suggestions would be helpful.
select rownum,result,movingsum,new_result
(select rownum,result,movingsum,
if(result_norm_max=0,0,if(movingsum=1,1,0)) as new_result
from
(select rownum,result,
sum(result) over (order by rownum rows between 2 preceding and current row) as movingsum
from mytable) a;
The issue is, the above code doesn't return the output needed for all required logic of:
when result column is 0 new_result should be 0
when result is 1, new_result = 1 but only when previous 2 new_results are 0
Any suggestion on how I should approach this will be useful.
Thanks!
With some tries I was able to find the solution which is close to what I need as mentioned below. I used 2 variables to carry out the trick,
select rownum,result,
if (result= 0, 0, if(#n = 1, if(#m >= 7, 1 , 0), 1)) as new_max,
if (result= 0, 0, if(#n = 1,
case when #m >= 7 then #m:=0 else 0 end
, 1)) as new_max1,
if (result= 0, if(#m>0,#m:=#m-1,#m:=0), if(#n = 1, #m:=#m+1,#m:=#m-1)) as new_m,
#n := result
from mytable a, (select #n:= 0, #m:= 0) b
I'm looking to bring in my expression 2 values that I what to add together.
=Sum(iif(Fields!Leadsource.Value = "set1", 1, 0) and (Fields!Leadsource.Value = "set", 1, 0))
but is just coming back as 0 when the value should be 400 or so.
Can any one point me in the right direction?
I'm not sure how SSRS evaluates your expression
=Sum(iif(Fields!Leadsource.Value = "set1", 1, 0) and (Fields!Leadsource.Value = "set", 1, 0))
I think SUM(1 AND 0) and SUM(1 AND 1) both equal 1.
Your expression needs to be changed a little - though I'm not sure which you need.
=Sum(IIF(Fields!Leadsource.Value = "set1" OR Fields!Leadsource.Value = "set", 1, 0))
Otherwise if you want to count the two different criteria separately, use:
=Sum(IIF(Fields!Leadsource.Value = "set1", 1, 0) + (Fields!Leadsource.Value = "set", 1, 0))
Consider following query
SELECT
('foo' + 1 - 1) = 'foo',
('foo' + 1 - 1),
'foo'
to my surprise it returns 1, 0, 'foo'
so how 0 equals foo?
and how to make statement that ('foo' + 1 - 1) = 'foo' will return false (0) ?
The answer is described in Type Conversion in Expression Evaluation section of the mysql documentation.
('foo' + 1 - 1) expression is evaluated as number because of the numbers and type of operators in the expression. In this context the string 'foo' is interpreted as 0. So, the above expression translates as 0+1-1 => 0. Then, this number is compared with the string 'foo'. Since one of the operands is an integer, the other is a string, the comparison is done as floating point numbers. In this context the string 'foo' is converted to 0. 0 = 0 is true, so you get 1 as a result.
I just found one solution
SELECT
binary ('foo' + 1 - 1) = binary 'foo',
('foo' + 1 - 1),
'foo'
I have a mysql query:
SELECT my_table.* WHERE SOUNDEX(my_col)='whatever' OR SUBSTR(SOUNDEX(my_col),4)='whatever' ORDER BY SUBSTR(SOUNDEX(my_col),4)='whatever',SOUNDEX(my_col)='whatever'
How many times will the substring function and soundex functions will actually be called? I mean for exactly same inputs will mysql cache the results over the span of one query?
If not, how can I make the change in the query so that each function is called minimum times possible.
MySQL would call this function four times for every returned row, to avoid this you can use a subquery, so instead of
SELECT *
FROM song
ORDER BY Substr(pre_calculated_soundex, 1, 1) =
Substr(Soundex("aaaa"), 1, 1)
+ Substr(pre_calculated_soundex
, 2, 1) =
Substr
(Soundex("aaaa"), 2, 1)
+ Substr(pre_calculated_soundex, 3, 1)
= Substr(Soundex("aaaa"), 3, 1)
+ Substr(pre_calculated_soundex, 4, 1
)
= Substr(Soundex("aaaa"), 4, 1)
You can do
SELECT * from (select *, Soundex("aaaa") as current_soundex from song)
ORDER BY
Substr(pre_calculated_soundex, 1, 1) = Substr(current_soundex , 1, 1)
+ Substr(pre_calculated_soundex, 2, 1) = Substr(current_soundex , 2, 1)
+ Substr(pre_calculated_soundex, 3, 1) = Substr(current_soundex , 3, 1)
+ Substr(pre_calculated_soundex, 4, 1) = Substr(current_soundex , 4, 1)