Sql LIKE End Of String - sql

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]'

Related

How to search a string in list in sql?

I have been using sql for quite a time but unable to figure out below query logic.
I'm extracting two values
First_name i.e abc
FIRST_NAMES_LIST (list containing first names) i.e ['abc','abc','cba','dba'] (this may contain junk values also in between strings)
I trying to search first_name in first_name_list and return 1 or 0, using below logic
CASE FIRST_NAME in FIRST_NAMES_LIST then 1
else 0
but this isn't giving correct result
Can somebody please help.
Thanks,
Naseer
Look this information:
https://www.techonthenet.com/oracle/functions/instr.php
INSTR is a function which return <> 0 if your parameter match. If not it return 0.
I no have any clear example in your ennunciate to give you the correct answer. See the functionalities.
Regards!
Ideally you would parse out your json into a table object ( I don't know how to do that in Oracle) and then search your table object where the object contains the value, but that is pretty expensive. It would be more robust and would be able to handle special characters/corner cases better.
On the other hand, if the names are going to stay simple (ie, no quote marks ' or commas), you could use a LIKE expression and search the string.
CASE WHEN FIRST_NAMES_LIST LIKE '%''' + FIRST_NAME + '''%' THEN 1 ELSE 0 END
Yes im currently using INSTR but query is taking bit of time. hope resolves this issue. thanks.

LIKE clause in stored procedure

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),'%')

SQL Like filtering

Welcome!
I am currently working on some C# and I encountered a rather silly issue. I need to fill ListBox with some data from database obviously. Problem is varchar filtering. I need to filter codes and display only the right ones.
Example of codes are: RRTT, RRTR, RT12, RQ00, R100, R200, R355, TY44, GG77 etc. Four digit codes.
I managed to filter only R-codes with simple select * from table1 where code_field like 'R%' but I get both R000 and RQ10 and I need to get R ones followed by numbers only.
So from example:
RRTT, RRTR, RT12, RQ00, R100, R200, R355
Only:
R100, R200, R355
Any ideas what should I add to the like 'R%'?
In SQL Server, you can make use of wildcards. Here is one approach:
where rcode like 'R___' and -- make sure code has four characters
rcode not like 'R%[^0-9]%' -- and nothing after the first one is not a number
Or:
where rcode like 'R[0-9][0-9][0-9]'
In other databases, you would normally do this using regular expressions rather than extensions to like.
here solution using like
SELECT *
FROM (
VALUES ( 'R123'),
( 'R12T' ),
( 'RT12' )
) AS t(test)
where test like 'R[0-9][0-9][0-9]'
like 'S[0-9%]'
Friend came up with the solution, thanks anyway

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'

SQL select statement

in a string array i have a variable amount of values.
how will an sql statement look if i want to select all the records that are equal with the array variables.
it will look something this:
SELECT * FROM users WHERE username='"+ variable amount of elements in the String array like: Steven, Mike, John ..... +"'"
You might be looking for the IN( ) operator.
SELECT * FROM users WHERE username IN ('chris', 'bob', 'bill');
This? (obviously the "OR username='xxx' is repeated for as many items as you require)
SELECT * FROM users WHERE username='item1' OR username='item2' OR username='item3'
Every one above me is corrrect! There are multiple ways of doing this! A simple google search for 'mysql statement variables' yield tons of help full results including:
http://www.daniweb.com/forums/thread126895.html
Just treat your array like a standard varible with [a number] on the end!
Two tips come from this:
Google / Search your problem first 9 times out of 10 someone else has had the same problem and found a working solution!
And be prepared to look at the answers on here with in 5 mins. This forums probably the quickest you'll ever see. The only thing that slows the community down is typing ! :P
Hope that Helps,
Andy
Another approach, although less efficient, can be used if parsing the string apart is problematic...
DECLARE #listOfNames VARCHAR(2000)
SET #ListOfNames = 'JOE,KELLIE,PETE'
SELECT * FROM users WHERE charindex(","+userName+",",","+#listOfNames+",") > 0
This approach is slower than either of the other answers, but can save you from using dynamic SQL to build a SQL query based on the list of names passed in...