问题
In Access, I have a field like this:
From the start until end at 01/05/2013, the XXXXXXX device was used.
I'm looking for a query that can extract the device name (in this case XXXXXXX). I was able to use mid() to get it somewhat working, but the sentence could be longer or shorter. What are some other ways to do this? Is there a find() I can use to find the location of "device"?
My SQL looks like:
SELECT Mid(Note, 68, 30)
FROM My_Log
WHERE Note LIKE "*, the*"
;
回答1:
If the device name is the word before "device", and you want to find that word using only functions supported directly by the Access db engine, Mid()
, InStr()
, and InstrRev()
can get the job done ... but it won't be pretty.
Here is an Immediate window session ...
Note = "From the start until end at 01/05/2013, the XXXXXXX device was used."
? Mid(Note, _
InstrRev(Note, " ", InStr(1, Note, " device") -1) +1, _
InStr(1, Note, " device") - InstrRev(Note, " ", InStr(1, Note, " device") -1) -1)
XXXXXXX
So you would then need to use that complex Mid()
expression in a query.
However, if you can use a user-defined function in your query, the logic could be easier to manage.
Public Function FindDevice(ByVal pInput As String) As Variant
Dim astrWords() As String
Dim i As Long
Dim lngUBound As Long
Dim varReturn As Variant
varReturn = Null
astrWords = Split(pInput, " ")
lngUBound = UBound(astrWords)
For i = 0 To lngUBound
If astrWords(i) = "device" Then
varReturn = astrWords(i - 1)
Exit For
End If
Next i
FindDevice = varReturn
End Function
Then the query could be ...
SELECT FindDevice(Note) AS device_name
FROM My_Log
WHERE Note LIKE "*, the*"
回答2:
If you know the name of the device, you can use Instr:
... WHERE Instr([Note], "Mydevice")>0
You can also use a table that lists devices:
SELECT Note FROM MyTable, ListTable
WHERE Note Like "*" & ListTable.Devices & "*"
Re comment:
SELECT Note
FROM MyTable
WHERE [Note] Like "*" & [enter device] & "*"
If you need to know where in notes you will find device
SELECT Note, InStr([Note],[enter device]) ...
回答3:
In addition to my comments this is Oracle query that may help you. Replace dual with with your table name and query should probably work. As I mentioned all functions I'm using in this query are ANSI SQL standard. The syntax you can fix yourself. You may run all queries separately to get start, end positions and lenght of your machine name, etc...:
SELECT SUBSTR(str, start_pos, end_pos) machine_name
FROM
(
SELECT str, INSTR(str,'XXXXXXX') start_pos, Length('XXXXXXX') end_pos
FROM
(
SELECT 'From the start until end at 01/05/2013, the XXXXXXX device was used.' str FROM dual
)
)
/
SQL>
MACHINE
-------
XXXXXXX
More generic approach with the same result - eliminating Length:
SELECT SUBSTR(str_starts, 1, end_pos) machine_name FROM -- Final string
(
SELECT str_starts, INSTR(str_starts, ' ')-1 end_pos FROM -- end pos = last X pos in string starts - in 'XXXXXXX'
(
SELECT SUBSTR(str, start_pos) str_starts FROM -- strig starts from first X
(
SELECT str, INSTR(str,'XXXXXXX') start_pos FROM -- start_pos
(
SELECT 'From the start until end at 01/05/2013, the XXXXXXX device was used.' str FROM dual
))))
/
来源:https://stackoverflow.com/questions/14483667/select-part-of-word-from-query