SQL: Extract a word from a string - sql

I have a following SQL statement that looks for a certain word after the keyword "dispatch level:".
SELECT SUBSTRING(CadRemarks, CHARINDEX('dispatch level:', CadRemarks) + LEN('dispatch level:'), 6) as
data
from myTable
I would like to upgrade this statement in a way that will return whatever comes between the keyword "dispatch level:" and anything that comes after the word that I'm looking for. So it could be another word, character, or space. For example:
Dispatch level: XXXXX YYY
or
Dispatch level: XXXXX
Only return XXXXX
EDIT
I'm trying out this SQL, but it selects the word after the word I need, as well.
select left(tt.CadRemarks, charindex(' ', tt.CadRemarks+ ' ')) as data
from myTablet cross apply
( values (substring(CadRemarks, charindex('dispatch level:', CadRemarks) +
16, len(CadRemarks)))
) tt(CadRemarks)
where t.CadRemarks like '%dispatch level:%'
the where clause is for me to limit the records, but I will have to move it and make the whole thing as CASE statement. But that's later....
Also tried this, but getting an error (invalid length parameter passed to the left...)
SELECT SUBSTRING(CadRemarks, CHARINDEX('dispatch level: ', CadRemarks) +
LEN('dispatch level: ') + 1,
CHARINDEX('', CadRemarks) - (CHARINDEX('dispatch level: ', CadRemarks) + 2 +
LEN('dispatch level: ')))
FROM myTable

Use left() to further find the string :
select left(tt.CadRemarks, charindex(' ', tt.CadRemarks+ ' ')) as data
from table t cross apply
( values (substring(CadtRemarks, charindex('dispatch level:', CadRemarks) + 16, len(CadRemarks)))
) tt(CadRemarks);

Related

SQL: What are phone number formatting options?

SQL: What are phone number formatting options?
I query my SQL Database for a phone number:
816-123-4567
I want to output it is as:
816 123-4567.
What steps do I take to achieve this result in an SQL environment?
A standard SQL solution would be:
select substring(phone, 1, 3) || ' ' || substring(phone, 5, 8)
There may be simpler solutions in other databases. For instance, in SQL Server:
select stuff(phone, 4, 1, ' ')
And in MySQL:
select insert(phone, 4, 1, ' ')
Note: These are specific the the format you have provided in your question. If you have other formats, then you might need more complicated logic.
Use SUBSTRING and CHARINDEX functions.
SUBSTRING gets a part of the string according to given indexes.
CHARINDEX gets the index of the character that triggers your String separation.
Below is an MSSQL Server query.
According to your DBMS, the function names can vary, but the logic will is the same.
Query :
SELECT
SUBSTRING('816-123-4567', 0, CHARINDEX('-', '816-123-4567') ) +
' ' +
SUBSTRING('816-123-4567', CHARINDEX('-', '816-123-4567') + 1 , LEN('816-123-4567') -
CHARINDEX('-', '816-123-4567') )
And the result :
816 123-4567
When we put your field and table name instead of static values :
SELECT
SUBSTRING(YourPhoneField, 0, CHARINDEX('-', YourPhoneField) ) +
' ' +
SUBSTRING(YourPhoneField, CHARINDEX('-', YourPhoneField) + 1 , LEN(YourPhoneField) -
CHARINDEX('-', YourPhoneField) )
FROM YourTableName

SQL I need to extract a stored procedure name from a string

I am a bit new to this site but I have looked an many possible answers to my question but none of them has answered my need. I have a feeling it's a good challenge. Here it goes.
In one of our tables we list what is used to run a report this can mean that we can have a short EXEC [svr1].[dbo].[stored_procedure] or "...From svr1.dbo.stored_procedure...".
My goal is to get the stored procedure name out of this string (column). I have tried to get the string between '[' and ']' but that breaks when there are no brackets. I have been at this for a few days and just can't seem to find a solution.
Any assistance you can provide is greatly appreciated.
Thank you in advance for entertaining this question.
almostanexpert
Considering the ending character of your sample sentences is space, or your sentences end without trailing ( whether space or any other character other than given samples ), and assuming you have no other dots before samples, the following would be a clean way which uses substring(), len(), charindex() and replace() together :
with t(str) as
(
select '[svr1].[dbo].[stored_procedure]' union all
select 'before svr1.dbo.stored_procedure someting more' union all
select 'abc before svr1.dbo.stored_procedure'
), t2(str) as
(
select replace(replace(str,'[',''),']','') from t
), t3(str) as
(
select substring(str,charindex('.',str)+1,len(str)) from t2
)
select
substring(
str,
charindex('.',str)+1,
case
when charindex(' ',str) > 0 then
charindex(' ',str)
else
len(str)
end - charindex('.',str)
) as "Result String"
from t3;
Result String
----------------
stored_procedure
stored_procedure
stored_procedure
Demo
With the variability of inputs you seem to have we will need to plan for a few scenarios. The below code assumes that there will be exactly two '.' characters before the stored_procedure, and that [stored_procedure] will either end the string or be followed by a space if the string continues.
SELECT TRIM('[' FROM TRIM(']' FROM --Trim brackets from final result if they exist
SUBSTR(column || ' ', --substr(string, start_pos, length), Space added in case proc name is end of str
INSTR(column || ' ', '.', 1, 2)+1, --start_pos: find second '.' and start 1 char after
INSTR(column || ' ', ' ', INSTR(column || ' ', '.', 1, 2), 1)-(INSTR(column || ' ', '.', 1, 2)+1))
-- Len: start after 2nd '.' and go until first space (subtract 2nd '.' index to get "Length")
))FROM TABLE;
Working from the middle out we'll start with using the SUBSTR function and concatenating a space to the end of the original string. This allows us to use a space to find the end of the stored_procedure even if it is the last piece of the string.
Next to find our starting position, we use INSTR to search for the second instance of the '.' and start 1 position after.
For the length argument, we find the index of the first space after that second '.' and then subtract that '.' index.
From here we have either [stored_procedure] or stored_procedure. Running the TRIM functions for each bracket will remove them if they exist, and if not will just return the name of the procedure.
Sample inputs based on above description:
'EXEC [svr1].[dbo].[stored_procedure]'
'EXEC [svr1].[dbo].[stored_procedure] FROM TEST'
'svr1.dbo.stored_procedure'
Note: This code is written for Oracle SQL but can be translated to mySQL using similar functions.

Last Name First Name Switch with hyphenated names

I know this has been asked several times and I have been able to find a partial answer using the results I have found on StackOverflow. I have a column that contains the name combination of "LastName FirstName" I need the row to display "FirstName LastName. I can make that happen using this SQL code:
SELECT
SUBSTRING('First Last', CHARINDEX(' ', 'First Last') + 1, 8000) +' '+
SUBSTRING('First Last', 1, CHARINDEX(' ', 'First Last') - 1) AS Name
Now when I try to use this code using my data with this code:
SUBSTRING(Rtrim(Ltrim(name)), CHARINDEX(' ', Rtrim(Ltrim(name))) + 1,
8000) +' '+ SUBSTRING(Rtrim(Ltrim(name)), 1, CHARINDEX(' ',
Rtrim(Ltrim(name))) - 1) AS Name
FROM
VW_MyView
I get my expected results but I also get an error message that states
Invalid length parameter passed to the LEFT or SUBSTRING function.
I have tracked the error down to names that have a hyphen. How can I deal with those hyphen during this process
Hmmm . . .
If you have only one or two names separated by a space:
select (stuff(name, 1, charindex(' ', name + ' ') - 1, '') + ' ' +
left(name, charindex(' ', name + ' ') - 1)
)
Note that this appends a space so the charindex() returns a valid position.

splitting a column into individual words then filter results

I have a column where I would like to break it into individual words for an "auto suggest" on text box. I was able to accomplish this thanks to this article:
http://discourse.bigdinosaur.org/t/sql-and-database-splitting-a-column-into-individual-words/709/45
However, the results have characters that I don't want like "/' , etc.
I have a database function that I use when I want to filter out characters, but I cannot figure out how to merge the 2 and get it to work.
Here's the splitting code:
;WITH for_tally (spc_loc) AS
(SELECT 1 AS spc_loc
UNION ALL
SELECT spc_loc + 1 from for_tally WHERE spc_loc < 65
)
SELECT spc_loc into #tally from for_tally OPTION(MAXRECURSION 65)
select substring(name, spc_loc, charindex(' ', name + ' ', spc_loc) - spc_loc) as word
into #temptable
from #tally, products
where spc_loc <= convert(int, len(name))
and substring(' ' + name, spc_loc, 1) = ' '
Then, here's how I view the table:
select distinct word from #temptable order by word
Then here's how I call the database function in other queries:
SELECT * INTO #Keywords FROM dbo.SplitStringPattern(#Keywords, '[abcdefghijklmnopqrstuvwxyz0123456789-]')
Where #Keywords is the string to filter.
I've tried all I can think of, to filter the first query by the dbo.SplitStringPattern function
ie:
select substring(dbo.SplitStringPattern(name, '[abcdefghijklmnopqrstuvwxyz0123456789-]'), spc_loc, charindex(' ', name + ' ', spc_loc) - spc_loc) as word
into #temptable
from #tally, products
where spc_loc <= convert(int, len(name))
and substring(' ' + name, spc_loc, 1) = ' '
But I get "Cannot find either column "dbo" or the user-defined function or aggregate "dbo.SplitStringPattern", or the name is ambiguous."
I need this to be optimized as this query needs to return results very quick.
Or, if there's a better way of doing this, I'm open to suggestions.
Create a T-SQL Function to filter out the unwanted characters before your "database function" is called. The following web page links should help.
MSDN Create Function (Transact-SQL)
How to remove special characters from a string in MS SQL Server (T-SQL)

Running SQL query to remove all trailing and beginning double quotes only affects first record in result set

I'm having a problem when running the below query - it seems to ONLY affect the very first record. The query removes all trailing and beginning double quotes. The first query is the one that does this; the second query is just to demonstrate that there are multiple records that have beginning double quotes that I need removed.
QUESTION: As you can see the first record resulting from the top query is fine - it has its double quotes removed from the beginning. But all subsequent queries appear to be untouched. Why?
If quotes are always assumed to exist at both the beginning and the end, adjust your CASE statement to look for instances where both cases exist:
CASE
WHEN ([Message] LIKE '"%' AND [Message] LIKE '%"') THEN LEFT(RIGHT([Message], LEN([Message])-1),LEN([Message]-2)
ELSE [Message]
EDIT
If assumption is not valid, combine above syntax with your existing CASE logic:
CASE
WHEN ([Message] LIKE '"%' AND [Message] LIKE '%"') THEN LEFT(RIGHT([Message],LEN([Message])-1),LEN([Message]-2)
WHEN ([Message] LIKE '"%') THEN RIGHT([Message],LEN([Message]-1)
WHEN ([Message] LIKE '%"') THEN LEFT([Message],LEN([Message]-1)
ELSE [Message]
Because your CASE statement is only evaluating the first condition met, it will only ever remove one of the statements.
Try something like the following:
SELECT REPLACE(SUBSTRING(Message, 1, 1), '"', '') + SUBSTRING(Message, 2, LEN(Message) - 2) + REPLACE(SUBSTRING(Message, LEN(Message), 1), '"', '')
EDIT: As Martin Smith pointed out, my original code wouldn't work if a string was under two characters, so ...
CREATE TABLE #Message (Message VARCHAR(20))
INSERT INTO #Message (Message)
SELECT '"SomeText"'
UNION
SELECT '"SomeText'
UNION
SELECT 'SomeText"'
UNION
SELECT 'S'
SELECT
CASE
WHEN LEN(Message) >=2
THEN REPLACE(SUBSTRING(Message, 1, 1), '"', '') + SUBSTRING(Message, 2, LEN(Message) - 2) + REPLACE(SUBSTRING(Message, LEN(Message), 1), '"', '')
ELSE Message
END AS Message
FROM #Message
DROP TABLE #Message
Try this:
SELECT REPLACE([Message], '"', '') AS [Message] FROM SomeTable