How to break a string apart by a character the occurs multiple times in SQL - sql

I am looking to break a column into two columns by a character. Some of the rows have the character occuring multiple times and I need to key on the last occuring one.
Example:
400000007_MOD-HUD_1-1.jpg
I want to break into two columns
Column 1: 400000007_MOD-HUD_1
Column 2: -1.jpg
The data looks like this,
200000297_R-1_1-1.jpg
400000007_MOD-HUD_1-1.jpg
500000334_R-1_1-1.jpg
500000334_R-2_MOD_HUD_1-1.jpg
500000342_MOD-HUD_1-1.jpg
1200000177_MOD-HUD_1-1.jpg
1300000433_C-1-EQSHED_1-1.jpg
1300000433_C-3-UB_1-1.jpg
2100000375_C_1-5_Barn_1-1.jpg
The character I want to split them by is "-". This character occurs multiple times in some of these file names and I want to key on the last occuring one.

Here's a possible solution you can try. Possible as I'm guessing you want to break on the last - character. Assuming ssms tag implies SQL Server, try the following:
select Left(col,Len(col)-p) Col1, Right(col,p) Col2
from t
cross apply (values(CharIndex('-',Reverse(col))))x(p)
Demo DB<>Fiddle

Related

Select column ignore beginning numbers

I have a column that I need to select but it has an inconsistent amount of numbers/formatting in the beginning
The column values are ideally supposed to be structured like:
# Question_-_Answer
But here are some examples which make it hard to remove the numbers in the beginning
0 Question1_-_50-60
1.Question_-_apple
12Question_-_40/50
13 Question_-_orange
14.Question_-_apple
15. Question_-_orange2
Is there a way I can query this column so that it ignores everything until the first alphabetical character while also not removing any characters/alphanumerical values in the question and answer portion?
You can use PATINDEX and STUFF to achieve this:
SELECT STUFF(V.YourString,1,PATINDEX('%[A-z]%',V.YourString)-1,'')
FROM (VALUES('0 Question1_-_50-60'),
('1.Question_-_apple'),
('12Question_-_40/50'),
('13 Question_-_orange'),
('14.Question_-_apple'),
('15. Question_-_orange2'))V(YourString);
This removes all characters up to the first alpha character.

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.

SQLite TRIM same character, multiple columns

I have a table in an SQLite db which has multiple columns with leading '='. I understand that I can use...
SELECT TRIM(`column1`, '=') FROM table;
to clean one column however I get a syntax error if I try for example, this...
SELECT TRIM(`column1`, `column2`, `column3`, '=') FROM table;
Due to incorrect number of arguments.
Is there a more efficient way of writing this code than applying the trim to each column separately like this?
SELECT TRIM(`column1`,'=')as `col1`, TRIM(`column2`,'=')as `col2`, TRIM(`column3`,'=')as `col3` FROM table;
How SQLite guide tells:
trim(X,Y)
The trim(X,Y) function returns a string formed by removing any and all
characters that appear in Y from both ends of X. If the Y argument is
omitted, trim(X) removes spaces from both ends of X.
You have only two parameters, so it's impossible apply it one shot on 3 columns table.
The first parameter is a column, or variable on you can apply trim. The second parameter is a character to change.

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.

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.