SQL query search within a string - sql

I have a column of data in my database that has a project number the numbers are formatted like this, YYYYnnnn.ee (for example 20140124.00). Since there are three distinct parts of the number, the user could search by either the YYYY or the nnnn. I have written a query that searches by the YYYY. I need to write a query for the nnnn.
How would you write a query to search for a specific string in a specific location of another string?

You can use SUBSTRING for that.
The first paramter to SUBSTRING should be the columname, the next parameter is the index of the start character and the last parameter is the number of characters.
The example below will return the nnnn part from your column. In the example below we are searching for records where the nnnn equals 1234.
SELECT *
FROM tablename
WHERE
(SUBSTRING(columnName, 5, 4) = '1234')
Please note, if your column is a number (decimal). You will need to first cast it as a varchar (string). For example:
SELECT *
FROM tablename
WHERE
(SUBSTRING(CAST(columnName AS VARCHAR(20)), 5, 4) = '1234')

Related

Consider a query to find details of research fields where the first two parts of the ID are D and 2 and the last part is one character (digit)

The ID of research fields have three parts, each part separated by a period.
Consider a query to find the details of research fields where the first two parts of the ID are D and 2, and the last part is a single character (digit).
IDs like D.2.1 and D.2.3 are in the query result whereas IDs like D.2.12 or D.2.15 are not.
The SQL query given below does not return the correct result. Explain the reason why it does not return the correct result and give the correct SQL query.
select *
from field
where ID like 'B.1._';
I have no idea why it doesnt work.
Anyone can help on this? Many thanks
D.2.1 and D.2.3 are in the query result whereas IDs like D.2.12 or D.2.15 are not.
An underscore matches any single character in a LIKE filter so B.1._ is looking for the start of the string followed by a B character followed by a . character then a 1 character then a . character then any single character then the end of the string.
You could use:
SELECT *
FROM field
WHERE ID like 'B.1._%';
The % will match any number of characters (including zero) until the end of the string and the preceding underscore will enforce that there is at least one character after the period.

Find substring in string

Is it possible to check if a specific substring which is in SQL Server column, is contained in a user provided string?
Example :
SELECT * FROM Table WHERE 'random words to check, which are in a string' CONTAINS Column
From my understanding, CONTAINS can't do such kind of search.
EDIT :
I have a fully indexed text and would like to search (by the fastest method) if a string provided by me contains words that are present in a column.
You can use LIKE:
SELECT * FROM YourTable t
WHERE 'random words ....' LIKE '%' + t.column + '%'
Or
SELECT * FROM YourTable t
WHERE t.column LIKE '%random words ....%'
Depends what did you mean, first one select the records that the column has a part of the provided string. The second one is the opposite.
Just use the LIKE syntax together with % around the string you are looking for:
SELECT
*
FROM
table
WHERE
Column LIKE '%some random string%'
This will return all rows in the table table in which the column Column contains the text "some random string".
1) If you want to get data starting with some letter you can use % this operator like this in your where clause
WHERE
Column LIKE "%some random string"
2) If you want to get data contains any letter you can use
WHERE
Column LIKE "%some random string%"
3)if you want to get data ending with some letter you can use
WHERE
Column LIKE "some random string%"

Using SQL to make specific changes in a database.

I am trying to figure out some commands/code in SQL.
I have database with names, addresses IDs etc, but I have to convert firstname values ending in “jnr” to “(Jnr)” and those ending in “snr” to “(Snr)”.
How do I do this?
update table TABLE_NAME set NAMES = '*xyz*Jnr' where NAMES like '%jnr'
Update or select:
PASTE(column, CHAR_LENGTH(column)-3, 1, UPPER(SUBSTRING(column FROM CHAR_LENGTH(column)-3 FOR 1)
WHERE column LIKE '%jnr' OR column LIKE '%snr'
PASTE is used to put in one character at position 3 from end,
CHAR_LENGTH to get length of column value,
UPPER converts character to upper case,
SUBSTRING is used to pick one character here (j or s),
LIKE is used to find values ending with jnr, or snr.
All ANSI SQL (no dbms specified!)

Reading a part of a alpha numeric string in SQL

I have a table with one column " otname "
table1.otname contains multiple rows of alpha-numeric string resembling the following data sample:
11.10.32.12.U.A.F.3.2.21.249.1
2001.1.1003.8281.A.LE.P.P
2010.1.1003.8261.A.LE.B.B
I want to read the fourth number in every string ( part of the string in bold ) and write a query in Oracle 10g
to read its description stored in another table. My dilemma is writing the first part of the query.i.e. choosing the fourth number of every string in a table
My second query will be something like this:
select description_text from table2 where sncode = 8281 -- fourth part of the data sample in every string
Many thanks.
novice
Works with 9i+:
WITH portion AS (
SELECT SUBSTR(t.otname, INSTR(t.otname, ".", 1, 3)+1, INSTR(t.otname, ".", 1, 4)) 'sncode'
FROM TABLE t)
SELECT t.description_text
FROM TABLE2 t
JOIN portion p ON p.sncode = t.sncode
The use of SUBSTR should be obvious; INSTR is being used to find location the period (.), starting at the first character in the string (parameter value 1), on the 3rd and 4th appearance in the string. You might have to subtract one from the position returned for the 4th instance of the period - test this first to be sure you're getting the right values:
SELECT SUBSTR(t.otname, INSTR(t.otname, ".", 1, 3)+1, INSTR(t.otname, ".", 1, 4)) 'sncode'
FROM TABLE t
I used subquery factoring so the substring happens before you join to the second table. It can be done as a subquery, but subquery factoring is faster.
Newer versions of oracle (including 10g) have various regular expression functions. So you can do something like this:
where sncode = to_number(regexp_replace(otname, '^(\d+\.\d+\.\d+\.(\d+))?.+$', '\2'))
This matches 3 sets of digits-followed-by-a-dot, and a fourth grouped set of digits, followed by the rest of the string, and returns a string consisting of all that entirely replaced by the first group (the fourth set of digits).
Here's a complete query (if I understood your description of the two tables correctly):
select t2.description_text
from table1 t1, table2 t2
where t2.sncode = to_number(regexp_replace(t1.otname, '^(\d+\.\d+\.\d+\.(\d+))?.+$', '\2'))
Another slightly shorter alternative regex:
where t2.sncode = to_number(regexp_replace(t1.otname, '^((\d+\.){3}(\d+))?.+$', '\3'))

Is it possible to get the matching string from an SQL query?

If I have a query to return all matching entries in a DB that have "news" in the searchable column (i.e. SELECT * FROM table WHERE column LIKE %news%), and one particular row has an entry starting with "In recent World news, Somalia was invaded by ...", can I return a specific "chunk" of an SQL entry? Kind of like a teaser, if you will.
select substring(column,
CHARINDEX ('news',lower(column))-10,
20)
FROM table
WHERE column LIKE %news%
basically substring the column starting 10 characters before where the word 'news' is and continuing for 20.
Edit: You'll need to make sure that 'news' isn't in the first 10 characters and adjust the start position accordingly.
You can use substring function in a SELECT part. Something like:
SELECT SUBSTRING(column, 1,20) FROM table WHERE column LIKE %news%
This will return the first 20 characters from column column
I had the same problem, I ended up loading the whole field into C#, then re-searched the text for the search string, then selected x characters either side.
This will work fine for LIKE, but not full text queries which use FORMS OF INFLECTION because that may match "women" when you search for "woman".
If you are using MSSQL you can perform all kinds VB-like of substring functions as part of your query.