string.IndexOf exact match - indexof

I have the following:
string text = "Select [id] AS [FROMId] FROM [TASK] ORDER BY id"
and I want to use text.IndexOf("FROM") in order to find where the FROM starts.
I want to find the position of FROM and not the position of FROMId.
LastIndexOf or FirstIndexOf are not correct answers cause the text could be anything like
string text = #"Select [id] AS [FROMId],
newId as [newFROMId] FROM [TASK] ORDER BY [FROMId]"
I need the indexof to do exact matching.
Any ideas?

Since FROM is an SQL reserved word that will generally have spaces on either side, you could look for that then, since that will give you the address of the space before the F, add one to get the location of the F itself:
int index = text.IndexOf(" FROM ") + 1
This may not necessarily take care of all edge cases(a) but, to do that properly, you may have to implement an SQL parser to ensure you can correctly locate the real from keyword and distinguish it from other possibilities.
(a) Such as things like:
select [a]FROM[tble] ...
select 'got data from unit #' | unit from tbl ...
and so on.

Related

How can I use 'update where' select in FoxPro?

I am totally new to FoxPro (and quite fluent with MySQL).
I am trying to execute this query in FoxPro:
update expertcorr_memoinv.dbf set 'Memo' = (select 'Memo' from expertcorr_memoinv.dbf WHERE Keymemo='10045223') WHERE Keydoc like "UBOA"
I got the error:
function name is missing )
How can I fix it?
In FoxPro SQL statements you would not 'single-quote' column names. In Visual FoxPro version 9 the following sequence would run without errors:
CREATE TABLE expertcorr_memoinv (keydoc Char(20), keymemo M, Memo M)
Update expertcorr_memoinv.dbf set Memo = (select Memo from expertcorr_memoinv.dbf WHERE Keymemo='10045223') WHERE Keydoc like "UBOA"
If you would provide a few sample data and an expected result, we could see whether the line you posted would do what you want after correcting the single-quoted 'Memo' names.
NB 1: "Memo" is a reserved word in FoxPro.
NB 2: As you know, the ";" semicolon is a line-continuation in Visual FoxPro, so that a longer SQL statement can be full; of; those;
So that the Update one-liner could be written as:
Update expertcorr_memoinv ;
Set Memo = (Select Memo From expertcorr_memoinv ;
WHERE Keymemo='10045223') ;
WHERE Keydoc Like "UBOA"
NB 3: Alternatively, you can SQL Update .... From... in Visual FoxPro, similar to the Microsoft SQL Server feature. See How do I UPDATE from a SELECT in SQL Server?
I would do that just as Stefan showed.
In VFP, you also have a chance to use non-SQL statements which make it easier to express yourself. From your code it feels like KeyMemo is a unique field:
* Get the Memo value into an array
* where KeyMemo = '10045223'
* or use that as a variable also
local lcKey
lcKey = '10045223'
Select Memo From expertcorr_memoinv ;
WHERE Keymemo=m.lcKey ;
into array laMemo
* Update with that value
Update expertcorr_memoinv ;
Set Memo = laMemo[1] ;
WHERE Keydoc Like "UBOA"
This is only for divide & conquer strategy that one may find easier to follow. Other than that writing it with a single SQL is just fine.
PS: In VFP you don't use backticks at all.
Single quotes, double quotes and opening closing square brackets are not used as identifiers but all those three are used for string literals.
'This is a string literal'
"This is a string literal"
[This is a string literal]
"My name is John O'hara"
'We need 3.5" disk'
[Put 3.5" disk into John's computer]
There are subtle differences between them, which I think is an advanced topic and that you may never need to know.
Also [] is used for array indexer.
Any one of them could also be used for things like table name, alias name, file name ... (name expression) - still they are string literals, parentheses make it a name expression. ie:
select * from ('MyTable') ...
copy to ("c:\my folder\my file.txt") type delimited

I want fetch substring from in oracle table between last '/' and before '.' from last in images table

I want to fetch substring from string in column between last '/' and last '.' .
Here is sample date for IMAGE_PATH column name:
sph/images/30_Fairhall_Court.jpeg
sph/images/9_Pennethorne_House.jpeg
rbkc/images/TAVISTOCK_CRESCENT.jpeg
haringey/images/399932thumb.jpg
urbanchoice/images/18190862.jpg
wandle/images/f13c10d2-2692-457d-a208-8bb9e10b27dc.png
housingmoves/images/No14_Asterid Heights_DS37620.jpg
wandle/images/f13c10d2-2692-457d-a208-8bb9e10b27dc.png
So the required output is like
30_Fairhall_Court
9_Pennethorne_House
TAVISTOCK_CRESCENT
399932thumb
18190862
f13c10d2-2692-457d-a208-8bb9e10b27dc
No14_Asterid Heights_DS37620
f13c10d2-2692-457d-a208-8bb9e10b27dc
Please suggest how to fetch. I need to update another blank column in table with this value. The table has around 10 lacks records.
One of possible solutions is to use functions substr() and instr() with negative third parameter:
select image_path,
substr(image_path,
instr(image_path, '/', -1) + 1,
instr(image_path, '.', -1)-instr(image_path, '/', -1) - 1) img
from test
SQL Fiddle
Results:
IMAGE_PATH IMG
-------------------------------------------------------- -------------------------------------
sph/images/30_Fairhall_Court.jpeg 30_Fairhall_Court
sph/images/9_Pennethorne_House.jpeg 9_Pennethorne_House
rbkc/images/TAVISTOCK_CRESCENT.jpeg TAVISTOCK_CRESCENT
haringey/images/399932thumb.jpg 399932thumb
urbanchoice/images/18190862.jpg 18190862
wandle/images/f13c10d2-2692-457d-a208-8bb9e10b27dc.png f13c10d2-2692-457d-a208-8bb9e10b27dc
housingmoves/images/No14_Asterid Heights_DS37620.jpg No14_Asterid Heights_DS37620
wandle/ima.ges/f13c10d2-2692-457d-a208-8bb9e10b27dc.png f13c10d2-2692-457d-a208-8bb9e10b27dc
This regex works with the sample data you provided:
select regexp_substr(image_path
, '(/)([a-z0-9_ \-]+)(\.)([a-z]+)$'
, 1
, 1
, 'i'
, 2)
from t23
/
We have to include all the optional parameters after pattern so we can use the subexpr parameter to select just the filename element. Find out more.
As far as the updating goes, a million row table isn't that big. Given that you have to update all the rows there's not much you can do to tune it. Just issue the UPDATE statement and let it rip.
"its not working"
Hmmm, here's a SQL Fiddle which proves it does work. You've probably introduced a typo.
"The regexp looks unnecessary complex. Why not simply"
Perhaps it is too complicated. However your simplified version doesn't produce the correct result if there's more than one dot in the IMAGE_PATH. If that's never going to happen then your solution works just fine.

Select in ADO (vb6) with a numeric variable

Excuse me, occasionally I refer with some problem that maybe it's already been fixed. In any case, I would appreciate a clarification on vs.
I have a TariffeEstere table with the fields country, Min, Max, tariff
from which to extract the rate for the country concerned, depending on whether the value is between a minimum and a maximum and I should return a single record from which to extract its tariff:
The query is:
stsql = "Select * from QPagEstContanti Where country = ' Spain '
and min <= ImpAss and max >= ImpAss"
Where ImpAss is a variable of type double.
When I do
rstariffa.open ststql,.....
the recodset contains a record if e.g. ImpAss = 160 (i.e. an integer without decimals), and then the query works, but if it contains 21,77 ImpAss (Italian format) does not work anymore and gives me a syntax error.
To verify the contents of the query string (stsql) in fact I find:
Select * from QPagEstContanti Where country = 'Spain' and min < = 21,77 and max > = 21,77
in practice the bothering and would like a comma decimal, but do not know how do.
I tried to pass even a
format (ImpAss, "####0.00"),
but the value you found in a stsql is 21,77 always.
How can I fix the problem??
It sounds like the underlying language setting in SQL is expecting '.' decimals instead of ',' decimal notation.
To check this out - run the DBCC useroptions command and see what the 'language' value is set to. If the language is set to English or another '.' decimal notation - it explains why your SQL string is failing with values of double.
If that's the problem, the simplest way to fix it is to insert the following line after your stsql = statement:
  stsql = REPLACE(stsql, ",", ".")
Another way to fix it would be to change the DEFAULT_LANGUAGE for the login using the ALTER LOGIN command (but this changes the setting permanently)
Another way to fix it would be to add this command to the beginning of your stsql, which should change the language for the duration of the rs.Open:
  "SET LANGUAGE Italian;"

Whats the XPath equivalent to SQL In query?

I would like to know whats the XPath equivalent to SQL In query. Basically in sql i can do this:
select * from tbl1 where Id in (1,2,3,4)
so i want something similar in XPath/Xsl:
i.e.
//*[#id= IN('51417','1121','111')]
Please advice
(In XPath 2,) the = operator always works like in.
I.e. you can use
//*[#id = ('51417','1121','111')]
A solution is to write out the options as separate conditions:
//*[(#id = '51417') or (#id = '1121') or (#id = '111')]
Another, slightly less verbose solution that looks a bit like a hack, though, would be to use the contains function:
//*[contains('-51417-1121-111-', concat('-', #id, '-'))]
Literally, this means you're checking whether the value of the id attribute (preceeded and succeeded by a delimiter character) is a substring of -51417-1121-111-. Note that I am using a hyphen (-) as a delimiter of the allowable values; you can replace that with any character that will not appear in the id attribute.

appending string in existing string in sqlite3 manager fro firefox?

hello i have i want to do something like this.
i have 4 rows with unique id 1,2,3,4 all four rows contains some string like
option1,option2,option3,option4
now i want to add "a ) " to the option1, "b ) " to the option2 and so on so is there a way i can do this with a query.currently i am adding these to a lots of rows manually
It's not clear exactly by what logic you want to select the letter to prepend to field somestring, but if for example it's a "Caesar's cypher" (1 gives 'a', 2 gives 'b' etc) based on the id field, as your question suggests, then this should work:
UPDATE sometable
SET somestring = (
substr('abcdefghijklmnopqrstuvwxyz', id, 1) ||
' ) ' || somestring)
WHERE id <= 26;
...for no more than 26 rows of course, since beyond that the logic must change and obviously we can't guess just how you want to extend it (use id modulo 26 + 1, use more characters than just lowercase letters, or ...?) since you give no clue on why you want to do this.

Resources