SQlite full-text search MATCH miltiple columns with OR and AND - sqlite

I am learning how to use FTS4 in SQlite and trying to translate something like this:
SELECT * FROM table
WHERE (postal_code LIKE q1 OR place_name LIKE q1) AND (address LIKE q2)
which works perfectly with the normal table, to something like that:
SELECT * FROM table
WHERE table MATCH '(postal_code: q1 OR place_name: q1) AND (address: q2)'
which is not working with the same table converted to VIRTUAL .. USING fts4...
Searching for match in specific column with ":" is working, but how can I combine this with OR and AND?
Background - I try to work with a flexible query, where q1 can be one of the both: postal-code or place name and q2 has to be address.
EDIT:
This is what I got so far (partially) working
SELECT * FROM table WHERE table MATCH ('postal_code:q1 OR place_name:q1')
INTERSECT
SELECT * FROM table WHERE table MATCH ('address:q2');
(building on this post from #CL)
The first problem here are words with space - if I search for something like
...MATCH ('postal_code:"new haven*" OR place_name:"new_haven*"')
then I have "malformed MATCH expression"
The second problem is when I have empty query, like
...MATCH ('*')
then I get nothing

Related

SQLLite howto join mutiple tables without column ambiguity

I've the following simplified SQLLite query:
SELECT SPECIALTABLE.DETAILS AS Details
FROM SPECIALTABLE
INNER JOIN CUSTOMERTABLE
ON CUSTOMERTABLE.CUSCODE = SPECIALTABLE.CUSCODE
INNER JOIN CUSTOMERNAMETABLE
ON CUSTOMERNAMETABLE.NAMECODE = CUSTOMERTABLE.NAMECODE
WHERE NAMECODE LIKE '%' LIMIT 10
SPECIALTABLE does not contain the NAMECODE column.
But when I run the query, it gives the "ambiguous column name" error on NAMECODE. I do not want to specify the table however on namecode (e.g. WHERE CUSTOMERTABLE.NAMECODE LIKE ...).
To my understanding, SQLLite should treat the result of these joins like one big table containing all columns that are contained in each of the tables, so why is there ambiguity?
The problem is there is no column directly linking SPECIALTABLE and CUSTOMERNAMETABLE.
When you write your last line, you do not specify what NAMECODE is in condition. So, the SQL can't catch the NAMECODE correctly, because there is two definitions for them (CUSTOMERNAMETABLE.NAMECODE and CUSTOMERTABLE.NAMECODE)
WHERE NAMECODE LIKE '%' LIMIT 10
Try to Specify the Table that you want the NAMECODE condition, like this:
WHERE CUSTOMERNAMETABLE.NAMECODE LIKE '%' LIMIT 10
Or this:
WHERE CUSTOMERTABLE.NAMECODE LIKE '%' LIMIT 10

Use case statement for parameter for column name in sql where clause

I have been looking all day for a solution that works for my situation. I have found some things that are very similar but don't work for my situation, I tried them.
Here is the scenario; I have two table base and partdetails. I have an asp website (internal ONLY) that has drop down lists to select the parameters for a SQL query that fills a data grid view.
My problem is this, I need to be able, based on the drop down list boxes on the page, assign the column name that the criteria that is entered to be searched for.
Here is the query that I am trying to define: (This one returns 0 rows)
sqlCmd.CommandText = ("Select ba.referenceid, ba.partnum, pd.width, pd.length, CONVERT(varchar(12), pd.dateentered, 101) As [dateentered], ba.partqty, ba.status, ba.material From tbl_dlbase ba Join tbl_partdetails pd On ba.referenceid = pd.referenceid Where Case #field1 When 'part #' Then 'ba.partnum' When 'Spacing' Then 'pd.spacing' When 'Surface' Then 'pd.surface' When 'Height' Then 'pd.height' When 'Thickness' Then 'pd.thickness' End Like '%' + #criteria1 + '%'")
sqlCmd.Parameters.AddWithValue("#field1", ddlSc1.SelectedItem.Text)
sqlCmd.Parameters.AddWithValue("#criteria1", txbCriteria1.Text)
This is the latest version of the SQL statement that I have tried. I need to be able to set the field/column name based on the selection from the drop down list ddlsc1 on the asp page.
I have also been trying the queries in Studio manager to see if maybe I have fat fingered something but it also returns 0 rows so I know something is wrong with the query.
So how can I set the column name field using a parameter for the name. I know this is a huge security concern with SQL injection but this is an internal only site, and more importantly my boss said he wants it done with variables.
I don't really see a problem with this other than you have single quotes around your THEN values. Does this fix it?
SELECT ba.referenceid
,ba.partnum
,pd.width
,pd.length
,CONVERT(VARCHAR(12), pd.dateentered, 101) AS [dateentered]
,ba.partqty
,ba.STATUS
,ba.material
FROM tbl_dlbase ba
JOIN tbl_partdetails pd ON ba.referenceid = pd.referenceid
WHERE CASE #field1
WHEN 'part #'
THEN ba.partnum
WHEN 'Spacing'
THEN pd.spacing
WHEN 'Surface'
THEN pd.surface
WHEN 'Height'
THEN pd.height
WHEN 'Thickness'
THEN pd.thickness
END LIKE '%' + #criteria1 + '%'

How to query Unicode characters from SQL Server 2008

With NVARCHAR data type, I store my local language text in a column. I face a problem how to query that value from the database.
ዜናገብርኤልስ is stored value.
I wrote SQL like this
select DivisionName
from t_Et_Divisions
where DivisionName = 'ዜናገብርኤልስ'
select unicode (DivisionName)
from t_Et_Divisions
where DivisionName = 'ዜናገብርኤልስ'
The above didn't work. Does anyone have any ideas how to fix it?
Thanks!
You need to prefix your Unicode string literals with a N:
select DivisionName
from t_Et_Divisions
where DivisionName = N'ዜናገብርኤልስ'
This N prefix tells SQL Server to treat this string literal as a Unicode string and not convert it to a non-Unicode string (as it will if you omit the N prefix).
Update:
I still fail to understand what is not working according to you....
I tried setting up a table with an NVARCHAR column, and if I select, I get back that one, exact row match - as expected:
DECLARE #test TABLE (DivisionName NVARCHAR(100))
INSERT INTO #test (DivisionName)
VALUES (N'ዜናገብርኤልስ'), (N'ዜናገብርኤልስ,ኔትዎርክ,ከስተመር ስርቪስ'), (N'ኔትዎርክ,ከስተመር ስርቪስ')
SELECT *
FROM #test
WHERE DivisionName = N'ዜናገብርኤልስ'
This returns exactly one row - what else are you seeing, or what else are you expecting??
Update #2:
Ah - I see - the columns contains multiple, comma-separated values - which is a horrible design mistake to begin with..... (violates first normal form of database design - don't do it!!)
And then you want to select all rows that contain that search term - but only display the search term itself, not the whole DivisionName column? Seems rather pointless..... try this:
select N'ዜናገብርኤልስ'
from t_Et_Divisions
where DivisionName LIKE N'%ዜናገብርኤልስ%'
The LIKE searches for rows that contain that value, and since you already know what you want to display, just put that value into the SELECT list ....

select update within sama table oracle sql or pl/sql -

I need to select v_col1, from table_x and that column gives me string that i need to put(update) into same
rowid but into diffrent column(h_col2) in sama table table_x - sorry it seems easy but i am beginner....
tabl_x
rowid V_col1, h_col2 etc .....
1 672637263 GVRT1898
2 384738477 GVRT1876
3 263237863 GVRT1832
like in this example i need to put GVRT1898 (update) instead of 672637263 and i need to
go into every row in this table_x and fix -
like next line would be (rowid2 would be GVRT1876 instead of 384738477 :-)
this table has 40000 lines like this and i need to loop for every rowid
THX for your responce Justin - this is a little more complex,
i have this string in h_col and need to take only GVRTnumber out and put into v_col - but it's
hard becouse GVRTnumber is in various place in column see down here....
"E_ID"=X:"GVRT1878","RCode"=X:"156000","Month"=d:1,"Activate"=d:5,"Disp_Id"=X:"4673498","Tar"=X:"171758021";
2"E_ID"=X:"561001760","RCode"=X:"156000","Month"=d:1,"Activate"=d:5,"Disp_Id"=X:"GVRT1898","Tar"=X:"171758021";
h_col column have this number that i want but in various place like somethimes it's in this 600byte column it's in byte nr 156 - sometimes in 287 but the only unique is "GVRT...." how can i take that string and put it to v_col -
Can you show me how to write such SQL pl/sql ?
regards & thanks
It sounds like you just want
UPDATE tabl_x
SET h_col2 = v_col1
Of course, if you do something like this, that implies that one of the two columns should be dropped or the data model needs to get fixed. Having two copies of the same data in each row is a bad idea from a normalization standpoint if nothing else.

Search "[" or "]" inside a table using SQL command

In an SQL table I have following columns
id, title, content
And I would like to search text inside title or content having "[" or "]".
What will be the SQL command to fetch such kind of data?
(edit: removed "efficient")
(added)
As per "rsbarro"
I have tried and my findings are:
If I try for
SELECT * FROM MyTable WHERE Title LIKE '%[%'
returns 0 row.
If I try for
SELECT * FROM MyTable WHERE Title LIKE '%]%'
returns number of rows.
Try using the LIKE operator, but remember if you are searching for a [ you need to escape it as [[] (see #billinkc's answer for an explanation on the escaping, or check the MSDN documentation and look for the section "Using Wildcard Characters As Literals"). The statement:
SELECT * FROM MyTable WHERE Title LIKE '%[[]%'
will search for any record in MyTable where Title contains a [. The % is a wildcard character.
I'm not exactly sure from the question what you are trying to find, but if you want to search for a title containing both brackets (i.e., '??[match]??'), try:
SELECT * FROM MyTable WHERE Title LIKE '%[[]%]%'
EDIT
If you are trying to match either [ or ] in title or in content, the query would be:
SELECT * FROM MyTable
WHERE
Content LIKE '%[[]%' OR Content LIKE '%]%' OR
Title LIKE '%[[]%' OR Title LIKE '%]%'
This query would probably not be terribly efficient if you have a table with a large number of rows.
The examples above are failing for like matching because LIKE offers a single character match option with [].
Any single character within the specified range ([a-f]) or set ([abcdef]).
To actually match a [, you will need to embed it in a pair of brackets
;
WITH DEMO (Title) AS
(
SELECT 'Where are you [ I am here]'
UNION ALL SELECT 'WHO ARE YOU'
UNION ALL SELECT 'asdafasfa][asdfadfasdfasdfa'
)
SELECT
D.*
FROM
DEMO D
WHERE
D.Title LIKE '%[[]%'
OR D.Title LIKE '%[]]%'
Returns
Where are you [ I am here]
asdafasfa][asdfadfasdfasdfa
In Sql [ is a special character and where [[] means [, so the below Sql query will work accordingly.
SELECT * FROM MyTable WHERE (Title LIKE '%[[]%' OR content LIKE '%[[]%')
OR (Title LIKE '%]%' OR content LIKE '%]%')
Please see herefor more details.

Resources