Looking to find a number embedded in a string at some random place - MS SQL Server 2008 R2 - sql

I'm converting an old access database to a SQL solution. There is a comment field that the users used for a six digit number (sometimes). It is a free-form field and contains things like "ID# 123456", "123456", "ID #123456". In many cases, however, it just contains random notes that contain some numbers such as dates or other numbers.
What I'd like to do (since this isn't super-critical and is just to get the database started in the direction of properly populating this field), in my select into statement is if there are six consecutive numbers in this comment field insert them into my new field that I'm using specifically for this number.
I've tried various trims and other things but haven't gotten an adequate result.
Thanks!

Try the following
to select only fields with 6 consecutive digits
select * from LegacyTable
where Comment like '%[0-9][0-9][0-9][0-9][0-9][0-9]%'
To extract those six digits from the string
select Comment
substring(Comment,PatIndex('%[0-9][0-9][0-9][0-9][0-9][0-9]%',Comment),6) as nbr
from legacytable
where Comment like '%[0-9][0-9][0-9][0-9][0-9][0-9]%'

Assuming Sql Server is your new database, you could try something like this:
Select *
From LegacyTable
-- Where Comment LIKE '%\d\d\d\d\d\d%' This doesn't work
Where Comment Like '%[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]%'
This should return rows that have 6 numbers consecutively in the Comment column.

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.

SQL: Extract specific part of string

I'm trying to figure out how to extract a specific part of a string (made out of multiple terms) with a select statement in Oracle SQL.
The values in the column look somewhat like '2E WK 12-345-678 TM 13-06-2017', which has a slight variation in format in each row.
Now I want to create a new column that displays only the '123-456-789' part from each row. The question now is: How can you identify this exact format of 3 numbers, hyphen, 3 numbers, hyphen, 3 numbers from each row?
SUBSTR(...) didn't do it for me since the part in question is not always in the same position. Then I tried to apply REGEXP_LIKE(...)
but this doesn't return the right values either.
How should I write the SQL statement to do this? Help is very much appreciated.
Example of strings:
2E XX **18-580-0111**
**18-990-0020**: 11.2.11-11.14.19
**65-660-0838** 2015 xxxx core sysxx
**78-140-401** t/m 0019
** = specific part of the string that's needed
Kind regards!
Your question can directly be translated into a regular expression, for regexp_substr():
select col, regexp_substr(col, '[0-9]{2}-[0-9]{3}-[0-9]{3}')

Remove unnecessary Characters by using SQL query

Do you know how to remove below kind of Characters at once on a query ?
Note : .I'm retrieving this data from the Access app and put only the valid data into the SQL.
select DISTINCT ltrim(rtrim(a.Company)) from [Legacy].[dbo].[Attorney] as a
This column is company name column.I need to keep string characters only.But I need to remove numbers only rows,numbers and characters rows,NULL,Empty and all other +,-.
Based on your extremely vague "rules" I am going to make a guess.
Maybe something like this will be somewhere close.
select DISTINCT ltrim(rtrim(a.Company))
from [Legacy].[dbo].[Attorney] as a
where LEN(ltrim(rtrim(a.Company))) > 1
and IsNumeric(a.Company) = 0
This will exclude entries that are not at least 2 characters and can't be converted to a number.
This should select the rows you want to delete:
where company not like '%[a-zA-Z]%' and -- has at least one vowel
company like '%[^ a-zA-Z0-9.&]%' -- has a not-allowed character
The list of allowed characters in the second expression may not be complete.
If this works, then you can easily adapt it for a delete statement.

Query to return all results where a specific character is in the 4th position of a string

I have a table that contains 6 digit ID numbers ('AB1E11' for example) and I need to build a query in Teradata SQL that returns all results where 'E' is in the fourth position of the string. I haven't had a reason to do anything like this in several years, so I am extremely rusty. I know how to filter the results so that 'E' is contained anywhere in the string ( using SELECT * WHERE PLANID LIKE '%E%'), but I'm not sure how to filter the results so that only the ones where 'E' is in the 4th position show up. Can anyone help me out with this? I tried searching several times but couldn't find an answer.
Thank you.
Just use LIKE with the _ wildcard:
where planid like '____E%'
Note: that is 4 underscores, which represent any single character.
SELECT planid WHERE CHARINDEX('E', planid) = 4;
The starting position returned is 1-based, not 0-based.

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.