MYSQL - Select based on a dynamic value - mysql

I'm looking for a way to select something in one column based on another column's data. I have 2 tables:
input_table - has a column called "year_of_birth"
result_table - has a column called "notes"
you see, that "notes" column might contain a year in there, something like "1980". I need to be able to select rows where the notes field contains a match to the year_of_birth field. I have my statement written like this so far:
select inp.year_of_birth, res.notes, res.result
from order_input inp join order_result res on inp.order_id = res.order_id
where res.result !='no match'
and res.notes like '%' + inp.year_of_birth + '%'
right now i'm getting an error thta says "conversion failed when converting the varchar value '%' to data type int. I'm not sure what i'm doing wrong because I have a similar statement i'm using that has this type of string in it that works...

You need to use CONCAT instead of +
res.notes like CONCAT('%', inp.year_of_birth, '%')

Try this:
select inp.year_of_birth, res.notes, res.result from order_input as inp join order_resultas res on inp.order_id = res.order_id where res.result <> 'no match' and res.notes like CONCAT('%', inp.year_of_birth, '%')

Related

SQL Select if substring occurs then copy until substring else keep original

I have a database with TV Guide data, and in my description field (VARCHAR) sometimes i have a '|' where behind it is the rating. I used to check this in php, before converting it all to XML, but i would like to do this in SQL.
So if i have this string:
This is the description | rating pg-13
Then i want to keep the
This is the description
but if there is no '|' i want the whole string.
I tried using substring, but can't get it to work.
My query now is:
SELECT *, SUBSTRING(`long_description`, 1, POSITION('|' IN `long_description`)) FROM `programs` WHERE station_id = 1
this works only one way - this gives me the string before the '|' but if there is no '|' it gives an empty column.
Based on the use of backticks, you might be using MySQL. If so, substring_index() does exactly what you want:
select substring_index(long_description, '|', 1)
How about this:
SELECT
*,
IF(long_description LIKE '%|%',
SUBSTRING(`long_description`,
1,
POSITION('|' IN `long_description`)),
long_description)
FROM
`programs`
WHERE
station_id = 1
The IF clause basically just checks if you have a | in the field and applies your routine when this is true. Else it will simply return the complete long_description value.

How to use conditions to select a where clause

I am trying to set a search filter to my view. Normally the view returns the full data table. Now i have 3 filter fields for this first name, last name, and country. I want if the user presses search while all are empty it will return all the data in the db. but if even on field has a string it will return all the values matching that string of that field. similarly if other fields are also full the returned data will be the rows matching the corresponding strings in the fields.
Since i have used pagination using stored procedure i would prefer to use stored procedures for this as well. But i am not as well versed in mysql so i don't really know how to structure my query so that i can check all the conditions in the query and return the result from there instead of getting the data first and then filtering it.
I need the db to return nothing if even one input doesn't matches its corresponding value. but so far i am getting all the row if all the fields have a value and even one field is wrong, and i get the rows even if they do not match with the previous field for example:
if i search a john in first name and enter kelly in the last naem i will get all the john's and kelly's whether the first name doesnt match.
My stored Procedure:
CREATE PROCEDURE [dbo].[GetFilterResult]
#PageIndex INT,
#pageSize INT,
#attr1 nvarchar(300),
#attr2 nvarchar(300),
#attr3 nvarchar(300)
AS
Begin
SELECT Accounts.firstName, Accounts.lastName, Accounts.Email,
Accounts.dateOfBirth, Accounts.phoneNo, Countries.CountryName
FROM Accounts INNER JOIN Countries On Accounts.CountryID =
Countries.CountryID
Where (Accounts.firstName LIKE CONCAT('%', #attr1, '%') and
Accounts.lastName LIKE CONCAT('%', #attr2, '%') and Accounts.CountryID
Like CONCAT('%', #attr3, '%'))
or (Accounts.firstName LIKE CONCAT('%', #attr1, '%') and
Accounts.lastName LIKE CONCAT('%', #attr2, '%') and #attr3 like CONCAT
('null'))
or (Accounts.firstName LIKE CONCAT('%', #attr1, '%') and
Accounts.CountryID LIKE CONCAT('%', #attr3, '%') and #attr2 like CONCAT
('null'))
or (Accounts.CountryID LIKE CONCAT('%', #attr3, '%') and
Accounts.lastName LIKE CONCAT('%', #attr2, '%')and #attr1 like CONCAT
('null'))
or (Accounts.firstName LIKE CONCAT('%', #attr1, '%')and #attr3 like
CONCAT ('null') and #attr2 like CONCAT ('null'))
or (Accounts.lastName LIKE CONCAT('%', #attr2, '%')and #attr3 like
CONCAT ('null') and #attr1 like CONCAT ('null'))
or (Accounts.CountryID LIKE CONCAT('%', #attr3, '%')and #attr1 like
CONCAT ('null') and #attr2 like CONCAT ('null'))
ORDER BY UserId OFFSET #PageSize*(#PageIndex-1) ROWS FETCH NEXT
#PageSize ROWS ONLY;
END

Parameter query on nvarchar(max) shows as ntext error

I recently converted all ntext column types in my database to nvarchar(max).
I then ran EXECUTE sp_refreshview for the related views.
Yet when I run the following Parameter query (from classic ASP) on a view, I get an error:
Query:
SELECT CARID
FROM vwCAR
WHERE (1=1)
AND (Description LIKE '%'+ ? + '%')
ORDER BY CARID;
Error: The data types nvarchar and ntext are incompatible in the add operator (yet there are no longer any ntext columns!)
Yet I do NOT get this error if I run the same query directly SQL Server without the ? parameter as:
Query:
SELECT CARID
FROM vwCAR
WHERE (1=1)
AND (Description LIKE '%test%')
ORDER BY CARID;
I tried using Convert in the query, but had same result:
Query:
SELECT CARID
FROM vwCAR
WHERE (1=1)
AND (CONVERT(NVARCHAR(MAX), Description) LIKE N'%'+ ? + '%')
ORDER BY CARID;
What am I doing wrong?
Additional information:
I changed the type to nVarChar(4000) instead of (MAX) and everything works fine. This is a work around, but it solved the problem.
Just so I will know for the future, is it possible to run a parameter query using LIKE criteria on a nVarChar(Max) type column?
(Thank you #McNets for the post clean up .. I am new to this)
try to set before :
Set #value = '%' + #value + '%' ;
then use:
(description like #value )
I was using the wrong field type adLongVarWChar (203) in the parameter. Should have been using adVarWChar (202) for the nvarchar(max) type.
Confusion arose when I retrieved the field type directly from the database as noted below, it returned 203 for the nvarchar(max) type, so I assumed setting the parameter based on that type would work.
For each ofield in objRS.Fields
Redim Preserve FieldTypes(1,x)
FieldTypes(0,x) = ofield.type
FieldTypes(1,x) = ofield.definedsize
x = x + 1
Next

How to get data from table by like query

I have some problem in query I have two tables
appcp_sound_attributes
appcp_vocalize
appcp_sound_attributes contain field name "name" and appcp_vocalize contain field "attributes"
I want to get data from "appcp_vocalize" using like query Eg appcp_vocalize.attributes like '%' + appcp_sound_attributes.name + '%'
My query is :
SELECT *
FROM appcp_vocalize
JOIN appcp_sound_attributes
ON appcp_vocalize.attributes LIKE '%appcp_sound_attributes.name%'
Please give the best solution of this query
Try this. It will give you any matching records where the appcp_vocalize.attributes field contains the appcp_sound_attributes.name field.
SELECT *
FROM appcp_vocalize
JOIN appcp_sound_attributes
ON INSTR(appcp_vocalize.attributes, appcp_sound_attributes.name) > 0
you are looking for this CONCAT('%',appcp_vocalize.attributes,'%')

"Merge" IN and LIKE operator in Mysql

I've a problem and I'm guessing if there's a better way to achieve my goal. I've this query in Mysql to retrieve some rows:
SELECT *
FROM table
WHERE field IN ('V','S','G','B')
What I would like to do is to run a query that retrieve the rows where the field has value LIKE those in the IN list. I know that the trivial way is to use this query:
SELECT *
FROM table
WHERE field LIKE '%V%' OR field LIKE '%S%' OR
field LIKE '%G%' OR field LIKE '%B%'
What I want to know is there's an operator that do this or at least, if that operator does not exist, a better way to write the query.
Thanks to everyone that will help me.
Put the values in a table (Params) then use
SELECT *
FROM table
WHERE EXISTS (
SELECT *
FROM Params
WHERE table.field LIKE '%' + Params.col + '%'
);
Consider also putting the wildcard characters into the values in the Params table.