alter value in isnull - sql

I have a stored procedure where I'm serching for part of a column if a parameter isn't null.
The problem is that the value I'm searching for is mixed up in a long string of other values. Here's an example of what a row might look like:
'...value=153139,example=xyz,param=15,morestuff=otherthings...' //ignore the actual names, it's just examples for SO
So this wont work because it would potentially select the wrong thing (value=153139would cause a match if #param = '15'):
WHERE
[column] LIKE ISNULL(#param, [column])
This wouldn't work either:
WHERE
[column] LIKE '%param=' + ISNULL(#param, [column]) + '%'
The perfect thing for me would be something like ISNULL(check_expression, replacement_if_null, replecement_if_not_null), but to my knowledge, that doesn't exist..
P.S. I know that the way the information is stored isn't optimal, but it's what the system uses and I have to work with it =/ So no comments like "You should split the column up so that it only contains one value" please

Have you try a case statement:
case when check_expression is null then replacement_if_null else replecement_if_not_null end
or
[column] LIKE '%param=' + ISNULL(#param, [column]) + ',%' -- comma added before the last %
Cast #param if it is necessary and make sure you don't have hidden space around the values (rtrim or ltrim can be used to remove them)

Related

Full Text Search Using Multiple Partial Words

I have a sql server database that has medical descriptions in it. I've created a full text index on it, but I'm still figuring out how this works.
The easiest example to give is if there is a description of Hypertensive heart disease
Now they would like to be able to type hyp hea as a search term and have it return that.
So from what I've read it seems like my query needs to be something like
DECLARE #Term VARCHAR(100)
SET #Term = 'NEAR(''Hyper*'',''hea*'')'
SELECT * FROM Icd10Codes WHERE CONTAINS(Description, #Term)
If I take the wild card out for Hypertensive and heart, and type out the full words it works, but adding the wild card in returns nothing.
If it makes any difference I'm using Sql Server 2017
So it was a weird syntax issue that didn't cause an error, but stopped the search from working.
I changed it to
SELECT * FROM Icd10Codes where CONTAINS(description, '"hyper*" NEAR "hea*"')
The key here being I needed double quotes " and not to single quotes. I assumed it was two single quotes, the first to escape the second, but it was actually double quotes. The above query returns the results exactly as expected.
this will work:
SELECT * FROM Icd10Codes where SOUNDEX(description)=soundex('Hyp');
SELECT * FROM Icd10Codes where DIFFERENCE(description,'hyp hea')>=2;
You could try a like statement. You can find a thorough explanation here.
Like so:
SELECT * FROM Icd10Codes WHERE Icd10Codes LIKE '%hyp hea%';
And then instead of putting the String in there just use a variable.
If you need to search for separated partial words, as in an array of search terms, it gets a bit tricky, since you need to dynamically build the SQL statement.
MSSQL provides a few features for full text search. You can find those here. One of them is the CONTAINS keyword:
SELECT column FROM table WHERE CONTAINS (column , 'string1 string2 string3');
For me - this had more mileage.
create a calculated row with fields as full text search.
fullname / company / lastname all searchable.
ALTER TABLE profiles ADD COLUMN fts tsvector generated always as (to_tsvector('english', coalesce(profiles.company, '') || ' ' || coalesce(profiles.broker, '') || ' ' || coalesce(profiles.firstname, '') || ' ' || coalesce(profiles.lastname, '') || ' ' )) stored;
let { data, error } = await supabase.from('profiles')
.select()
.textSearch('fts',str)

Match Character Whether or Not It Exists in Like Statement

I need a like expression that will match a character whether or not it exists. It needs to match the following values:
..."value": "123456"...
..."value": "123456"...
"...value":"123456"...
This like statement will almost work: LIKE '%value":%"123456"%'
But there are values like this one that would also match, but I don't want returned:
..."value":"99999", "other":"123456"...
A regex expression to do what I'm looking to do is 'value": *?"123456"'. I need to do this in SQL Server 2008 and I don't believe there is good regex support in that version. How can I match using a like statement?
Remove the whitespace in your compare with REPLACE():
WHERE REPLACE(column,' ','') LIKE '%"value":"123456"%'
May need a double replace for tabs:
REPLACE(REPLACE(column,' ',''),' ','')
I don't think you can with the like operator. You could exclude ones you could match, like if you want to make sure it just doesn't contain other:
[field] LIKE '%value":%"123456"%` AND [field] NOT LIKE '%"other"%'
Otherwise I think you'd have to do some processing on the string. You could write a UDF to take the string and parse it to find the value for 'value' and compare based on that:
dbo.fn_GetValue([field], 'value') = '123456'
The function could find the index of '"' + #name + '"', find the next index of a quote, and the one after that, then get the string between those two quotes and return it.

Check if character column contains numeric value in DB/2 with sql

We have an AS400 that the db/2 is stored on and I am trying to join a table where character column contains the value of a numeric column.
I am running this in visual studio and I tried cast(val as char) and receive a 'as' not recognized, I tried CONVERT, and that isn't recognized either. Here is my original query:
SELECT *
FROM tab e, tab n
WHERE (e.LN = 0001) AND (n.RMK LIKE '%' + e.ORDNO)
n.RMK contains a character value which is consistent but something like, "ordr 1401" and then e.ORDNO contains a numeric value which would be 1401
I want to see if e.RMK contains the 1401 value in e.ORDNO.
When I run that query I get a Errot type 6 = Numeric data that is not valid error. So I figure I would try and convert the numeric value to char and try again.
Like so,
SELECT *
FROM tab e, tab n
WHERE (e.LN = 0001) AND n.RMK LIKE '%' + cast(e.ORDNO as varchar(10))
This did not work,
I also tried
n.RMK LIKE '%' + CONVERT(VARCHAR(10), e.ORDNO) and I get CONVERT not recognized.
First off, best practice is to use explicit joins.
Secondly, CAST (fld AS CHAR) should work, though it defaults to CHAR(1). You probably need more than that.
Third, assuming the prefix is always "ordr " I'd use a SUBSTR()
SELECT *
FROM tab e
JOIN tab n ON n.rmk = INT(SUBSTR(e.ordno,6))
WHERE e.ln = 1
Finally got a working solution although it may not be the best:
SELECT *
FROM tab e, tab n
WHERE (e.LN = 0001) AND (n.RMK LIKE CONCAT(RTRIM(CONCAT('%', CHAR(e.ORDNO))), '%'))
There was whitespace being added to the end of the e.ORDNO string for some reason using CHAR and there was whitespace at the end of the RMK string too; and the wildcard search wouldn't work unless I prepended and appended the '%' to e.ORDNO or I did a right trim on both n.RMK and e.ordno. Not sure if one way is better than the other.

MS SQL What does = '%' and '%%%' mean when in a where clause

In a vendor's database that the company I work for, they have some expressions that I don't think I have ever seen:
FROM CO_ITEM_MASTER WHERE smartpart_num = '%'
I have seen = '%Text%'
and I know what that means, but if there is no text along with the '%' what does that mean?
I also have the following:
AND (lower(CO_ITEM_MASTER.ITEM_NUM) like lower('%%%')
What does the '%%%' mean when there is no text between the '' characters?
% means
Match Any string of zero or more characters.
Because a zero length string matches this can be repeated as many times as desired without affecting the semantics and will return any row where ITEM_NUM is not NULL.
It is of course pointless to use more than one, perhaps this is code generated by code rather than a human.

SQL Search Query, Search for String Omit Spaces and Convert to Lower Case

My wording may not be correct so the solution could be on this site. In which case, pointing me to the solution would be great. Anyway, I am doing a search and need to do compares against the string and value that is in a column. It has to go beyond LIKE. For instance, I want to take the column put it in lower case and take out all spaces before comparing it to the string (which is too put in lower case and all spaces are gone). I want to do this without modifying the contents of the column. So just for the compare. Then if the compare evaluates to true take the original contents out of the column (not in the lower case and no spaces form). This may be to specific. Is it possible to do this, if so, any code sample would help. So I have something like this now:
SELECT *
FROM [MY_TABLE]
WHERE (
[MY_COLUMN] LIKE My_Search_String
)
so the My_Search_String is already formatted. I just need to format the [MY_COLUMN] without permanently modifying its contents. Any help is appreciated. Thank you!
Upper and lower case only make a difference if your default collations or column collations are case sensitive. In any case, you seem to want:
SELECT *
FROM [MY_TABLE]
WHERE lower(replace([MY_COLUMN], ' ', '')) LIKE lower(replace(My_Search_String, ' ', ''))
I think below query will solve your problem.!
IF EXISTS(select * from [MY_TABLE] where LOWER(REPLACE([MY_COLUMN],' ','')) like 'My_Search_String')
INSERT INTO Another_Table
select * from [MY_TABLE] where LOWER(REPLACE([MY_COLUMN],' ','')) like 'My_Search_String'