How to select specific data between Quotes (") - sql

I am reposting my question as I am new to SQL 2012.
I want to fetch the numeric data between quotes (") in the following rows,
row1:'asdalknd,"1,2,3,4",slknsdl,"5,6,7,8",snlsn'
row2:'asknd,"111,267,387,4756",snsdl,"534,646,767,348",snlssdsdsdsjkvkn'
row3'....
row4'....
row5'....
row6'...
row7'...
row8'....
The above mentioned are the rows of a single column.
I just want to extract the numerics(may be in another column for each rows)
Can anybody pls help, as this is way above my basic knowledge of t-sql.
Thanks

this is Ugly, but will eventually work:
COLUMN = 'jksjdksls#$#$##kskjfjf,"123,456,789" lsnslkdswfnslsjfls'
left(
right(COLUMN,len(COLUMN)-instr(COLUMN,"""")),
instr(
right(COLUMN,len(COLUMN)-instr(COLUMN,"""")),
"""") -1
)
--> 123,456,789
This is what is done:
We take this string 'jksjdksls#$#$##kskjfjf,"123,456,789" lsnslkdswfnslsjfls'
find the first occurence of " with instr(COLUMN,"""") --> returns 24
take the right end of the string with. Therefore we need to take the length of the string with len(COLUMN)--> 55 and substract the position of the first " (24)
then we need to find the second " with instr()in the right string, which we need to create again with right(COLUMN,len(COLUMN)-instr(COLUMN,"""")) and substract 1 for the ".

Related

SQL Query with criteria of 6 chars and wildcards

I'm stuck with an SQL query to get what I need. My input is this:
COD SINOM
A 123456
B 987654, 123456, 111111
C 123456 , 234501
D 9912345699
E 99123456, 789012
F 77123456
Both fields are text type, even if they are numbers on it (we use text type because sometimes it has leading zeros). Mycriteria is 123456
I'm trying to query all COD where SINOM contains my criteria, but only as 6 chars in a single word. It does not matter if it has any leading blanks or commas or semicolons (all of them are possible), but len of string must be 6, and could be more text. And criteria can be at left, right or middle of SINOM.
My actual query is this:
SELECT Table1.cod, Table1.sinom
FROM Table1
WHERE (((Table1.sinom)="123456")) OR (((Table1.sinom) Like "*123456,*")) OR (((Table1.sinom) Like "*123456 *"))
But the output I'm getting is wrong:
The right ones would be only the first 3 rows (A,B,C). Rest of rows are wrong. My expected output would be:
9912345699, 99123456, 789012 and 77123456 are wrong because it contains 123456 but not as a single word.
Looking for an SQL query to apply this and trying to avoid the use of an VBA function if possible
Other SYNOM that would be correct would be:
998877, 123456, 029384
012310,123456
I hope this is clear, but please, do not hesitate to ask if any doubt arises.
Thanks!
I think you want something like this:
WHERE "," & REPLACE(Table1.sinom, " ", "") & ",") Like "*,123456,*"
The key is to look for the delimited strings -- but to be sure that the column has commas at the beginning and end.
One caveat here is the spaces. This removes the spaces, assuming they are merely spaces.
I might suggest something along the lines of the following:
select * from table1 where "," & table1.sinom & "," Like "*[!0-9]123456[!0-9]*"
The use of [!0-9] will account for the possibility of any delimiter surrounding the numerical values, and the concatentation with commas (which could be any non-numerical character) accounts for cases in which the number appears on its own or at the start or end of the string.

What does the trim function mean in this context?

Database I'm using: https://uploadfiles.io/72wph
select acnum, field.fieldnum, title, descrip
from field, interest
where field.fieldnum=interest.fieldnum and trim(ID) like 'B.1._';
What will the output be from the above query?
Does trim(ID) like 'B.1._' mean that it will only select items from B.1._ column?
trim removes spaces at the beginning and end.
"_" would allow representing any character. Hence query select any row that starts with "B.1."
For eg.
'B.1.0'
'B.1.9'
'B.1.A'
'B.1.Z'
etc
Optional Wildcard characters allowed in like are % (percent) and _ (underscore).
A % matches any string with zero or more characters.
An _ matches any single character.
I don't know about the DB you are using but trim usually remove spaces around the argument you give to it.
The ID is trimmed to be sure to compare the ID without any white-space around it.
About your second question, Only the ROWS with an ID like 'B.1.' will be selected.
SQL like
SQL WHERE

split string with character

using SQL 2008; I have the following string:
EMCo: 1 WorkOrder: 12770 WOItem: 10
I am trying to get the WorkOrder #.
When the string did not have the WOItem on end of it, I was able to use the following statement to get WorkOrder #.
[WorkOrder] = LTRIM(RTRIM(RIGHT(HQMA.KeyString,CHARINDEX(':',REVERSE(HQMA.KeyString))-1)))
This statement moves and may have double digits for the Co#, and it does not always have WOItem #. Was hoping to find something that would split after the ":" and just take 2nd group.
Any suggestions?
The patindex suggestion above will work beautifully, now if you still want to use your current statement, substring will pull the same values, and replace will take out WOItem. Not very elegant, it works whether you have WOItem or not:
select substring(LTRIM(RTRIM(RIGHT(REPLACE(HQMA.KeyString,'WOItem:',''),
CHARINDEX(':',REVERSE(REPLACE(HQMA.KeyString,'WOItem:','')))-1))),0,7)
select substring(LTRIM(RTRIM(RIGHT(REPLACE(HQMA.KeyString,'WOItem:',''),
CHARINDEX(':',REVERSE(REPLACE(HQMA.KeyString,'WOItem:','')))-1))),0,7)
How about using patindex()? Assuming the work order always has five characters:
select substring(HQMA.KeyString,
patindex('%WorkOrder: %', HQMA.KeyString) + 11,
5) as WorkOrder

Remove last x characters until a specific character

I got this string /uk-en/contact-us/frequently-asked-questions/your-trip/there-wi-fi-access-in-the-eurostar-terminals-and-board-your-trains and I need to get just the last part of the URL (until the last /).
Then I want to replace '-' with a space. The strings are not with the same number of characters.
How can I do?
Thank you!
Solution using BigQuery functions:
select regexp_replace(last(split(x, "/")), "-", " ") from
(select
"/uk-en/contact-us/frequently-asked-questions/your-trip/there-wi-fi-access-in-the-eurostar-terminals-and-board-your-trains"
as x)
Here is what I tried in SQL Server
DECLARE #s VARCHAR(max)= '/uk-en/contact-us/frequently-asked-questions/your-trip/there-wi-fi-access-in-the-eurostar-terminals-and-board-your-trains'
SELECT REVERSE(SUBSTRING(REVERSE(#s),CHARINDEX('/',REVERSE(#s)),LEN(REVERSE(#s))))+REVERSE(REPLACE(SUBSTRING(REVERSE(#s),1,CHARINDEX('/',REVERSE(#s))-1),'-',' '))
Sorry this was for SQL Server
did you try using split in big query
SPLIT('str' [, 'delimiter']) Returns a set of substrings as a repeated string. If delimiter is specified, the SPLIT function breaks str into substrings, using delimiter as the delimiter.

Remove extra spaces if more than one space through vb.net Oracle query

Just looking for another way to remove any excess spaces(>1) throughout a query. I'm taking information for my Oracle query from an Excel spreadsheet so there's bound to be some excess zeroes due to user error and whatnot so that when I'm taking the information from the excel it will have 2 sometimes 3 extra spaces after some values so we won't get any records returned from Oracle when we query it through the application. I have tried Trim(ds.Tables.Item(0).Rows.Item(k).Item(i).ToString.ToUpper) but this isn't removing the extra spaces in some of the values. Is there some SQL statement I don't know of or perhaps another reason why Trim isn't working?
Edit: Was writing replace function incorrectly.
Original: string.Replace(" ", " ")
New: string = string.replace(" ", " ")
Use String.Replace(" ", String.Empty) instead. Trim only removes leading and trailing spaces.
On the Oracle side, you can use the REGEXP_REPLACE function, but you need to apply it to each column you want to remove the extra spaces from:
SELECT
REGEXP_REPLACE(myCol1, ' {2,}', ' ') AS myCleanCol1,
REGEXP_REPLACE(myCol2, ' {2,}', ' ') AS myCleanCol2,
FROM myTable
The example here looks for all occurrences of two or more spaces (that's the second parameter) and replaces them with a single space (the third parameter).
You can use REGEXP_REPLACE
Could be something like this:
regexp_replace(your_column, '\s{2,}', '')
Here is a sqlfiddle demo