I'm using the Stimulsoft Design to make reports and I'm using two variables to filter.
So I want to make the datasource to build one SQL statement if filter one is filled and the other is empty and another SQL if filter two is filled and the other is empty, and maybe an else statement...
So it would be something like this:
If filter one is filled and filter two is empty, then make SELECT 1, which is:
SELECT * FROM tableExample WHERE column1 LIKE '%{filter1}%'
If filter two is filled and filter one is empty, then make SELECT 2, which is:
SELECT * FROM tableExample WHERE id = '%{filter2}%'
and ID is a primary key.
Well, can I do that?
Maybe I could check it only using SQL, but I can't figure out how to do this, could someone help me please?
Thank you!
If you are looking for a single query which contains your logic you could try the following:
SELECT *
FROM tableExample
WHERE
(COALESCE(filter2, '') = '' AND COALESCE(filter1, '') <> '' AND
filter1 LIKE '%{filter1}%') OR
(COALESCE(filter1, '') = '' AND COALESCE(filter2, '') <> '' AND
id = '%{filter2}%')
Note, the somewhat ugly COALESCE calls are there because I don't know what you mean by empty. Does this mean NULL, empty string, or both?
Related
In my sql table I have a number of values as follows;
<![CDATA[9435547092]]>
<![CDATA[Company Name]]>
Most of the rows have a CDATA wrap, I wanted to remove this from all the files so I was thinking a subquery would be good something like;
SELECT value FROM attributes WHERE value LIKE "%<![CDATA[%";
Would give me each value and then I was thinking to do
SELECT REPLACE(SELECT value FROM attributes WHERE value LIKE "%<![CDATA[%";, "<![CDATA[", '') FROM attributes
But this isn’t valid, anyone know how this is possible?
just use replace in select for obatin the right string
SELECT replace(value , "<![CDATA[", '')
FROM attributes
WHERE value LIKE "%<![CDATA[%";
if you need store in databae then you need updated
UPDATE attributes
set value = replace(value , "<![CDATA[", '')
WHERE value LIKE "%<![CDATA[%";
but seems you are looking for the left string after LIKE "%
select right(value, length(value) - locate(value,']]')+2)
from attributes
WHERE value LIKE "%<![CDATA[%";
UPDATE attributes
set value = right(value, length(value) - locate(value,']]')+2)
WHERE value LIKE "%<![CDATA[%";
Does this do what you want?
select replace(replace(value, '<![CDATA[', ''), ']]>', '')
Note that if your actual value has "wraps", then this might affect the value. I think a more generic method would be:
select (case when value like '<![CDATA[%]]>'
then substr(value, 9, length(value) - 3)
else value
end)
Please try this,
SELECT select SUBSTRING(value,10,LEN(value)-12)
FROM attributes
WHERE value like '<![CDATA[%'
If needed where clause could be removed and as suggested above could be used with CASE statement.
My table contains some columns with ;-separated numbers like this :
1;2;43;22;20;12
and so on. It's also possible there's only 1 number in this column like 110, or 2 numbers like this 110;143
I want to select the rows that contain a certain number in this column. The number is in a variable $search_var.
Let's say I need to search for the number 1 in my column. If I use a select with like statement like so :
"SELECT * FROM Table WHERE ids LIKE '%".$search_var."%'"
I get all results containing '1' and not only '1', so I also get 11, 14, 110, 1999 etc.
I think I need to use some sort of regex-statement but I'm lost here... Who can help ?
You might not need regex for this
Set #YourNUmber := 110;
SELECT *
FROM Table
WHERE ';' + ids + ';' LIKE '%;'+ #yourNumber + ';%'
This guarantees there are always ; surrounding all the numbers.
This is formatted for SQL Server. The variable syntax and wildcards might be different if you are using something else.
EDIT:
Thanks #FélixGagnon-Grenier for the suggestions. I think either of these two will work. See here for a SQL Fiddle example
SELECT *
FROM T
WHERE concat(';',ids,';') LIKE concat('%;', #YourNumber , ';%');
SELECT *
FROM T
WHERE LOCATE(concat(';', #YourNumber , ';'),concat(';',ids,';'))>0
Try this solution if you're using SQL Server. This searches for the number where adjcent characters are not numbers:
SELECT * FROM Table WHERE ids LIKE '%[^0-9]".$search_var."[^0-9]%'
in my table i have a feild user_ids. the user_ids feilds containeing the values like 12,45,78,95,21,78,98
what i need is i need an mysql query that search for a specific id(for ex:45) in the feild.
i used like operator but its not working as i expected. for ex: when i search for 5 its return tru, but the id 5 not in the list.
i would like to know is there any default function is available in mysql.
could you pls help me...
my query.
SELECT * FROM `FRIENDSLIST` WHERE `USERS_ID` LIKE '%'.$ID.'%';
NB: i dont know whether this question meets standard,pls dont do down vote. pls help me....
This also works:
select * from FRIENDSLIST where find_in_set( $ID, USERS_ID ) <> 0
Try
Where ',' + MyField + ',' Like '%,' + MySearchString + ',%'
The problem is that you're thinking of it as IDs, but it's just a string. So when you search for '5' in '12,45,78,95,21,78,98' it finds it in the 5 of the 45. If you surround with commas then it searches for ',45,' in ',12,45,78,95,21,78,98,' and finds it, but is you look for ',5,' it won't match, as desired.
you also need to add commas at the beginning and end to be able to math the first and last IDs.
As per your data simpler way is to search with comma as like ',45,'.
But better way is it split them based on comma and matching to it.
I have a table where I extract some values, one column values can contain "value1|value2|value3", but I only want to get the characters before the | - "value1".
This is what I tried, but it doesn't work.. What am I doing wrong?
Thanks!
$sql = "SELECT * LEFT('Like', LOCATE('|', 'Like')-1) FROM $tablename
WHERE Parent = '0' AND Type LIKE 'top' ORDER BY Order ASC";
I want to use this for ALL values, not just one field..
you need the following statement to get that portion of [ColName]:
LEFT([ColName],INSTR([ColName],"|")-1)
If you want to select multiple columns into the same recordset column you can union all with something like the following:
SELECT LEFT(ColName,INSTR(ColName,"|")-1) AS FirstValue From $TableName;
UNION ALL
SELECT LEFT(ColName2,INSTR(ColName2,"|")-1) AS FirstValue From $TableName;
If you want to use this on multiple columns, script the creation of the sql.
Two things: (1) you don't have a comma between your * and the expression you're trying to do with LEFT and (2) you're putting like in quotes, so the functions are working on the constant value like instead of your column named like. Try putting like in backticks.
SELECT *, LEFT(`Like`, LOCATE('|', `Like`)-1)
...
You can also use the MySQL SUBSTRING_INDEX function for this:
SELECT *, SUBSTRING_INDEX(`Like`, '|', 1)
...
I wonder if is it possible to run mysql command which can select an alternative field if the first given field is empty, on the same table.
Example : I have table called "posts" which have "intro" and "content". In the same statement I would like to select as a result "content" if "intro" is empty, but not having both in the result array.
Thanks in advance
You can use IF function:
SELECT IF(LENGTH(intro)>0, intro, content)
FROM posts
Or you can test for NULL if you mean that empty is NULL
Coalesce is what you are looking for.
SELECT COALESCE(intro, content) AS column1
FROM table
Assuming intro is null and not a zero length string.
There two potential ways. When way is with a case statement.
Select case when field1 <> '' then field2 else field1 end
or you you can use coalesce.
select coalesce(field1, field2)