LIKE clause in stored procedure - sql

IN my stored procedure I have
SELECT properName
FROM nameTable
WHERE properName like '%IN_newName%'
But it isn't working on a value that has a space in the first character spot " Name"
I've tried like IN_newName concat'%' and also LOCATE(), but I'm not getting any results. I just need to be able to match the values of a string even if there is a space in the front or back and concat doesn't seem to get it. For reference, properName and IN_newName are both CHARACTER(10)
What else can I try here? This is db2 for iSeries jVersion 7

Have you tried:
like trim(IN_newName) concat'%'

Perhaps you could try:
SELECT properName
FROM nameTable
WHERE properName like CONCAT(LTRIM(IN_newName),'%')

Related

Replace Value Of Fields Before Searching

To Increase Speed of search in the database, i want to do something like this:
If field TheFieldName (without any space in it) was equal with test then show the record(s)
how can i do it?
This did'nt work for me:
"SELECT * FROM TheTableName WHERE REPLACE(TheFieldName, ' ', '')=test"
Error: Undefined function 'REPLACE' in expression
It seems unlikely to me that replace() is not known in SQL Server (or almost any other database). But, check to be sure you are using the database you think you are.
Your query, as written, does have an error -- because you seem to want test as a string. Does the query really look like this:
SELECT *
FROM TheTableName
WHERE REPLACE(TheFieldName, ' ', '') = 'test';
Note the quotes around 'test'.
This should work.
"SELECT * FROM TheTableName WHERE rtrim(ltrim(TheFieldName))=test"

Using a GUID In The Where Clause

For some reason I'm unable to use comparisons on GUID columns, it does not return any results.
See below, with the WHERE clause set to the exact value of the 'secguid' column, it does not return any results. What's going on?
SELECT * FROM dbMobileFile
SELECT * FROM dbMobileFile WHERE secguid = '3137459D-EFDE-449E-94A3-89345A8580FA'
SELECT * FROM dbMobileFile WHERE secguid LIKE '3137459D-EFDE-449E-94A3-89345A8580FA'
Using LIKE does not work either.
Try this
SELECT [fileID],
[fileCOde],
[filePassword],
[fileDescription],
[rowguid],
[secguid]
FROM [dbo].[dbMobileFile]
WHERE CAST(secguid as uniqueidentifier) = CAST('3137459D-EFDE-449E-94A3-89345A8580FA' as uniqueidentifier)
Since you mention that the column is stored as NVARCHAR, its possible that the string has leading or trailing whitespaces, which is why it might not be popping up in the query with the WHERE clause.
You can try this :
SELECT [fileID],
[fileCOde],
[filePassword],
[fileDescription],
[rowguid],
[secguid]
FROM [dbo].[dbMobileFile]
WHERE LTRIM(RTRIM(secguid)) = '3137459D-EFDE-449E-94A3-89345A8580FA'
which should show you the result as leading and trailing whitespaces are eliminated in the WHERE clause.
Also, in case you want to make use of the LIKE operator, you can write your query as :
SELECT [fileID],
[fileCOde],
[filePassword],
[fileDescription],
[rowguid],
[secguid]
FROM [dbo].[dbMobileFile]
WHERE secguid LIKE '%3137459D-EFDE-449E-94A3-89345A8580FA%'
Hope this helps!!!
I've had this problem with a corrupt database. Some GUIDs be contained in the WHERE clause, with other GUIDS on the same table would not return results.
Turns out that database had Index issues. Run DBCC to make sure your database isn't corrupt.
The accepted answer works, but it is a bit verbose and probably not the intended way to do this. UniqueIdentifier is qualified by {}, so the following is the easiest;
SELECT * FROM dbMobileFile WHERE secguid = '{3137459D-EFDE-449E-94A3-89345A8580FA}'
See inside the database that guid value stored as 32 hex digits:00000000000000000000000000000000 so if we search by 32 hex digits separated by hyphens: 00000000-0000-0000-0000-000000000000, it's not get any output
Try this:
SELECT * FROM dbMobileFile WHERE secguid = ('3137459D-EFDE-449E-94A3-89345A8580FA')
Use parentheses to enclose GUID string LIKE ('GUID')

Sql LIKE End Of String

I need to query all records of street addresses that end in a number.
For example, addresses that have the unit number at the end of the string:
"902 MILLBOURNE AVENUE -113"
Can someone please help me?
I found Sql LIKE statement. End Of String but am confused.
This is my attempt but there is an error with my expression:
SELECT * FROM AddressTable WHERE:
RIGHT(StreetAddress,1) IN ('0','1','2','3','4','5','6','7','8','9')
Edit:
I am using ArcGIS
Also, I was able to make a new field in my DB and populate it with the last character of the StreetAddress field and SQL select based on numeracy of that field (I used NewField IN ('0','1','2','3','4','5','6','7','8','9')) to do that.
I would prefer to not make any schema changes, however.
Also, why are you down voting my question? Do you have an answer that will help me? Please Share it. If it doesn't work I dont know what else to tell you.
There's no : after a WHERE. Fix your syntax like this:
SELECT * FROM AddressTable
WHERE RIGHT(StreetAddress,1) IN ('0','1','2','3','4','5','6','7','8','9')
or you can use LIKE for a slightly more succinct query:
SELECT * FROM AddressTable WHERE StreetAddress LIKE '%[0123456789]'

SQL select does not return any entries

I am using a Sql query to load a subset of data
SELECT [sfb_id]
,[prs_id_201304]
,[prs_id_201204]
,[vorname]
,[sex]
FROM [IAB\KruegerJ049].[test]
WHERE sex='Weiblich'
the column sex contains the characters Männlich or Weiblich (male or female). I only want to read out rows which contain sex='Weiblich'. But this code does not return any entries!
Thanks in advance
It may be your collation is not right. change the collation and repeat the query . there is not error on your query.
Try WHERE sex LIKE '%Weiblich%' in case there are some spaces before or after.
Try to debug it with babysteps. First check should be if
SELECT * FROM [IAB\KruegerJ049].[test]
returns entrys, then try
SELECT * FROM [IAB\KruegerJ049].[test] WHERE sex='Weiblich'
i think you misstyped something :) you can also try
WHERE sex LIKE '*Weiblich*'
Try this:
WHERE LTRIM(RTRIM(LOWER(sex))) = 'weiblich'

unable to make out use of BETWEEN in oracle

I am not able to make this out: Between eliminates use of >= and <=
...but when i tried this query:
SELECT *
FROM names
WHERE name >= 'Ankit'
AND name <= 'P'
...it gives output:
name
------
Ankit
Mac
Bob
When I tried:
SELECT *
FROM names
WHERE name BETWEEN 'Ankit' AND 'P'
...it gives output:
name
------
Ankit
Can you explain this why?
First, Oracle VARCHAR2 type is case sensitive.
Second, check that you do not have spaces in the beginning of name like this:
" Bob"
" Mac"
Use trim function to check if this causes the problem:
SELECT *
FROM names
WHERE trim(name) BETWEEN 'Ankit' AND 'P'
If this does not help, check that language and sort order are correct for your database.
Edit:
Since above advice did not solve your problem, you could try following:
Maybe you have some other non-printable characters in field. Use Oracle DUMP function to check:
SELECT DUMP(name), name FROM names
You should get something like this:
Typ=1 Len=3: 66,111,98 Bob
...
Verify that Len is correct length.
Check NLS parameters so that they are not inadvertently changed to something that does not work for your database:
SELECT * FROM NLS_SESSION_PARAMETERS
SELECT * FROM NLS_DATABASE_PARAMETERS
SELECT * FROM NLS_INSTANCE_PARAMETERS
Check results of these three queries and verify that parameters on sort, language and character set are correct.
I'm quite certain this has nothing to do with your syntax and everything to do with your DB setup. I've recreated your test scenario and, like others, have no problem with either query returning the results you expect. Did you check your NLS_SESSION_PARAMETERS as mentioned earlier?
SQL code is case insensitive.
String values and string comparisons are case sensitive.
See for yourself:
SELECT CASE WHEN 'a' = 'A' THEN 'string comparison is case insensitive'
WHEN 'a' <> 'A' THEN 'string comparison is case sensitive'
END
FROM dual;
You seem to have used a lowercase 'p' in the top query and an uppercase 'P' in the second. Was this intentional?
SELECT *
FROM names
WHERE name BETWEEN 'Ankit' AND 'P'
also should return all three rows - I just verified that it does so using your example. Are you sure that you made the test correctly? Maybe you inserted data in other session and didn't commit additional rows?
Nut 100% sure about it, but as it's only 3 rows, you could try it:
SELECT *
FROM names
WHERE (name BETWEEN "Ankit" AND "P")
OR (name BETWEEN "ankit" AND "p")