i have a mySql database, and it has an field for each entry called xml, which contains XML.
i would like to know if there is syntax to select xml values like normal fields.
for example if i wanted to select all entries with the date of 11/21/2010
i try something like this, but the syntax is wrong, since i don't know how to work with xml well in mySql.
SELECT *
FROM `table`
WHERE `xml<date>` = '11/21/2010'
Ideally, I would like to compare it to another field:
SELECT *
FROM `table`
WHERE `xml<date>` != `date`
You can use the ExtractValue function with an XPath expression to get your desired field:
SELECT *
FROM `table`
WHERE ExtractValue(xml,'/a/suitable/xpath/expression') != `date`
Related
I am attempting to query a table in BigQuery programmatically, and have a WHERE-IN clause that I would like to support having NULL as the value. Current Query looks like:
SELECT * FROM DATASET.TABLE_NAME WHERE HIT_DATE = 'YYYY-MM-DD' AND ID IN (LIST_OF_IDS);
I am wondering if there is a way so that I can query everything from the table for a specific date in the case that the LIST_OF_IDS input is NULL (The idea being that I want to support choosing between an inputting of a list of ids or return everything if no list of ids are given).
I tried
SELECT * FROM DATASET.TABLE_NAME WHERE HIT_DATE = 'YYYY-MM-DD' AND ID IN (LIST_OF_IDS) IS NOT NULL;
But got this error thrown at me:
Syntax error: Expression to the left of IS must be parenthesized
If I understand correctly you are looking for this :
SELECT *
FROM DATASET.TABLE_NAME
WHERE HIT_DATE = 'YYYY-MM-DD'
AND (ID IN (LIST_OF_IDS) OR LIST_OF_IDS IS NULL);
I need to query a SQLite database for some entries containing a field the value of which can be one of a defined list: 'Token1', 'Token2', ..., 'TokenN' - potentially a long one.
The straightforward SELECT statement would be something like
SELECT * FROM `my_table` WHERE `token` = 'Token1' OR `token` = 'Token2' OR ...
- far from elegant. I wonder, is there a better way to formulate the statement?
You can use IN Clause
SELECT * FROM `my_table`
WHERE `token` in ('Token1','Token2', 'Token3', ....);
Use the wildcard character %%.
SELECT * FROM `my_table` WHERE `token` like '%Token%'
If your defined list is or can be placed in a table, you can do the following:
SELECT
*
FROM [employeeName] Where dept In (Select dept From #tbl)
I'm trying to write a SP that will allow users to search on multiple name strings, but supports LIKE functionality. For example, the user's input might be a string 'Scorsese, Kaurismaki, Tarkovsky'. I use a split function to turn that string into a table var, with one column, as follows:
part
------
Scorsese
Kaurismaki
Tarkovsky
Then, normally I would return any values from my table matching any of these values in my table var, with an IN statement:
select * from myTable where lastName IN (select * from #myTableVar)
However, this only returns exact matches, and I need to return partial matches. I'm looking for something like this, but that would actually compile:
select * from myTable where CONTAINS(lastName, select * from #myTableVar)
I've found other questions where it's made clear that you can't combine LIKE and IN, and it's recommended to use CONTAINS. My specific question is, is it possible to combine CONTAINS with a table list of values, as above? If so, what would that syntax look like? If not, any other workarounds to achieve my goal?
I'm using SQL Server 2016, if it makes any difference.
You can use EXISTS
SELECT * FROM myTable M
WHERE
EXISTS( SELECT * FROM #myTableVar V WHERE M.lastName like '%'+ V.part +'%' )
Can your parser built the entire statement? Will that get you what you want?
select *
from myTable
where CONTAINS
(lastName,
'"Scorsese" OR "Kaurismaki" OR "Tarkovsky"'
)
This can be done using CHARINDEX function combined with EXISTS:
select *
from myTable mt
where exists(select 1 from #myTableVar
where charindex(mt.lastName, part) > 0
or charindex(part, mt.lastName) > 0)
You might want to omit one of the conditions in the inner query, but I think this is what you want.
SQL newbie here, but I can't find the solution to something that looks easy:
The following query does not seem to have a valid syntax (ORA-00904: invalid identifier), but its logic should be clear. How can I achieve this in a query that needs to be speedy?
SELECT * FROM table WHERE LEFT(column,4)="abcd"
For this purpose, you should use like rather than left(). First, Oracle doesn't support left() (you need substr() instead). Second, like can make use of indexes because the wildcard is not at the beginning of the string:
SELECT *
FROM table
WHERE column like 'abcd%';
Oracle and some other products have substr.
SELECT * FROM tablename WHERE substr(columnname, 1, 4) = 'abcd'
I.e. single quotes for string literals!
ANSI SQL has substring:
SELECT * FROM tablename WHERE substring(columnname from 1 for 4) = 'abcd'
And others have left:
SELECT * FROM tablename WHERE LEFT(columnname,4) = 'abcd'
I'm looking for a character that I can use in an Oracle contains to get ALL results.
If I search for the string "test" in a title column I use this statement:
select *
from my_table
where contains (title,
'<query><textquery grammar="CTXCAT">test</textquery></query>') > 0
With this statement I get the rows which have the "test"-string included in title-column.
BUT: Is there any way to use contains and select ALL rows?
Would this work for you?
select * from my_table
where title like
'<query><textquery grammar="CTXCAT">%</textquery></query>'
This uses the LIKE syntax and the % wildcard to achieve what I think you want.
The documentation says wildcards with the Oracle Text CONTAINS() predicate are % and _ just like the LIKE predicate.
Does the following work? I don't have an instance of Oracle handy to test.
select *
from my_table
where contains (title,
'<query><textquery grammar="CTXCAT">%</textquery></query>') > 0