SQL select where like using query string - mysql

if i have these two complicated query strings
ALM_frmTopAlarmHistoryReport.aspx?strAlarmConnection=AlarmSystem
AND
http://1.1.4.1/xyz/ALM_frmAlarmHistoryReport.aspx?ViewPDF=
1&dtmStartDate={0}&dtmEndDate={1}& + '&lngAlarmGroup=' +
$('#ddlAlarmGroup').val() + '&lngProcessor=' + $('#ddlProcessor').val() +
'&intCategory=' + $('#ddlCategory').val()
how can i excuete a select WHERE LIKE condition
i tried
SELECT * FROM [tablename] WHERE string1 LIKE '%string2%'
and i got the following error
Msg 103, Level 15, State 4, Line 1
The identifier that starts with'%string2 ' is too long. Maximum length is 128.
any help will be very welcome thanks in advance

Use locate or instr instead:
select *
from tablename
where instr(string2, string1) > 0

Related

masking the id of an employee in a select query

I have an ID which I want to mask the last 4 digits of that id.
example: my_id = 123456789
SELECT
concat(left(my_id,length(my_id) -4)) + ' ' + 'xxxx' AS masked_data
FROM
dual ;
Expected output : 12345XXXX
But I am getting as 12345
The strings that you're concatenating should all be arguments to the CONCAT() function. + is for addition, not concatenation.
SELECT
concat(left(my_id, length(my_id) - 4), 'xxxx') AS masked_data
You also don't want a space before xxxx.

MySql - Query Error: Error: ER_OPERAND_COLUMNS: Operand should contain 1 column(s)

I have tbl which include 2 columns: title and params, the values are like the following:
- title: {"Teaching"}
- params:
{ "ufield926":"34",
"ufield927":"Sud",
"ufield928":"Ara",
"ufield929":"Mecca",
"ufield930":"1\/1\/1983",
"ufield933":"011",
"ufield934":"Mub",
"ufield943":"SU\/HI\/14",
"ufield944":"Average",
"ufield946":"Female"
}
I want to extract the code after "ufield943": which is SU/HI/14 only and concatenate it with the value in title column to be like the following:
--> Teaching (SU/HI/14)
Here is the query I have tried:
SELECT title, CONCAT(title, "(", (select *,
substring(
params,
locate('ufield943', params) + 12,
locate('ufield944', params) - locate('ufield943', params) - 15
) FROM tbl), ")") AS title
FROM tbl;
I get the following error everytime I run the query
"Query Error: Error: ER_OPERAND_COLUMNS: Operand should contain 1 column(s)"
.
Mysql expects the exact field or value as SELECT statement fields. So, When you use an inner SELECT result as a field or value on outer SELECT, you have to return a specific value as a result.
So, You have to remove *, from your inner SELECT as the first step.
Another thing that you must do is removing title from outer SELECT. As your query CONCAT your title with the expected result, so there is no need of selecting title again.
So your query should like:
SELECT CONCAT(title, "(", (select
substring(
params,
locate('ufield943', params) + 12,
locate('ufield944', params) - locate('ufield943', params) - 21
) FROM master_code), ")") AS title
FROM master_code;
As a final note, This is not a good approach to saving data as JSON in MySQL and accessing data like this.
Thanks to MySQL (5.7.8 or upper), you can use MySQL JSON type features or handle this using a programing language.
Demo

Convert a string with operation to int using sql query

persons is a tinytext field and can contain "4 + 2", "4+2", "4 +2", "5" or "" and so on.
I would like to select and int like 6, 6, 6, 5 and 0 from that MySQL 5.6 Table.
Tried this without success:
SELECT CAST(persons AS INT) FROM Table
SELECT CONVERT(INT, persons ) FROM Table
If + is the only operator and it appears once, then:
select (case when col like '%+%'
then substring_index(col, '+', 1) + substring_index(replace(col, ' ', ''), '+', -1)
else col + 0
end) as added_value
use SUBSTRING_INDEX
select SUBSTRING_INDEX(col , "+", 1)+ SUBSTRING_INDEX(col , "+", -1) as col1
from cte where col like '%+%'
union all
select SUBSTRING_INDEX(col , "+", 1) from cte where col not like '%+%'
output
col1
6
6
6
5
the upper solution will work only for your sample data
demo link
What database are you using? You may need to use something db specific. e.g. in oracle you can do:
select dbms_aw.eval_number ('4+2') from dual
It will return 6.
Generally speaking - using dynamic SQL you can easily achieve this.

I don't get how this SQL Query work and the := operator

I found this sql query online , i did work but am not quite able to parse it .
i haven't used any queries with '#' or " := "
if some one could explain me what it means and which topic it comes under , it would help me a lot ..
select (select (#) from (select(#:=0x00),(select (#) from (information_schema.columns) where (table_schema>=#) and (#)in (#:=concat(#,0x3C,0x62,0x72,0x3E,' [ ',table_schema,' ] > ',table_name,' > ',column_name))))a)#
First of all i would make the query a litte bit more readable by reformatting it:
1) SELECT (SELECT (#)
2) FROM (SELECT (#:=0x00),
3) (SELECT (#)
4) FROM (information_schema.columns)
5) WHERE (table_schema >= #)
6) AND (#) IN (#:=CONCAT(#,0x3C,0x62,0x72,0x3E,' [ ',table_schema,' ] > ',table_name,' > ',column_name))
7) )
8) )
9) a);
The assignment of # is as follows:
In Line 3 it gets the value 0x00 (Decimal: 0)
In line 5 this value is used for the greater than (table_schema >= 0)
Line 6 is a way to concat each schema, table and column name into #
# is returned in line 1 and contains a concatenated list of your structures
In line 6 an additional <br> (0x3C,0x62,0x72,0x3E) is added to the variable to make the output more readable

MySQL Error: Operand Should Contain at least 1 column (Comma vs Point)

SELECT (0,14285714285714285714285714285714*(5 - 2) + 2)
What does this mean?
The actual line giving me trouble is:
SET #a13 = (0,14285714285714285714285714285714*(#a - #tmpv) + #tmpv)
Both #a and #tmpv have been calculated previously in the trigger
Any insight would be helpful
There is a syntax error in your query: You use the comma as a decimal separator. You have to use a dot instead. This works just fine:
SELECT ( 0.14285714285714285714285714285714 * ( 5 -2 ) +2 )