I have a VARCHAR column category_text in the table that contain tags to a notification stored. I have three tags Query, Complaint and Suggestion and column can have one or more values separated by comma. I am applying a filter and filter can have one or more values as well in comma separated pattern.
Now what I want is to retrieve all the rows that contain at least one tag based on the filter user is applying, for instance user can select 'query,suggestion' as a filter and result would be all the rows that contain one of the tags i.e. query or suggestion.
select
t.category_text
from
real_time_notifications t
where
charindex('query, suggestion, complaints', t.category_text) > 0
order by
t.id desc
Create a new table, like user_category (user.id link to user table, category) and create an index on both. It will speed up a lot for searching and ease your future maintenance a lot.
If you still persist to do that, create an inline function to split string to records and then merge to test.
Related
I'm working on a query that pulls demographic information for people who have visited a location. However, the fields required for this report aren't all available in the DB, and some will need to be added manually from a separate Excel file after I've exported the results of my query. The person who will be responsible for merging the two files asked if it would be possible to create blank columns so they can more easily see where the missing data needs to go, and I wasn't sure how to go about that. Obviously those blank columns could just be created in the exported spreadsheet, but I wondered if there was a way to add them in the SQL query itself.
My SELECT statement currently looks something like this—I've just added comments to mark the missing fields so I can keep track of what order all the fields for this report need to be in.
SELECT DISTINCT
PersonID,
PersonFName,
PersonLName,
PersonDOB,
VisitID,
--StaffFName,
--StaffLName,
FacilityPhone,
FacilityAddress,
...and so on
Since those two staff name fields don't exist in my DB, I obviously can't actually include them in the SELECT list. But is there a way to still include those two fields as blank columns in my query results? Something along the lines of "select [nothing] as StaffFName"?
Just add literal nulls to the select clause:
SELECT DISTINCT
PersonID,
PersonFName,
PersonLName,
PersonDOB,
VisitID,
null as StaffFName,
null as StaffLName,
FacilityPhone,
FacilityAddress,
...
Or, if you prefer, you can use empty strings instead:
...
'' as StaffFName,
'' as StaffLName,
...
But null is the canonical way to represent the absence of data.
We have a huge table and one of the column contains queries like e.g. in row 1
1. (((firstname:Adam OR firstname:Neil ) AND lastname:Lee) ) AND category:"Legal" AND type:Individual
and in row 2 of same column
2. (((firstname:Adam* OR firstname:Neil ) AND lastname:Lee) ) AND category:"Legal" AND type:Organization
Similarly there are few other types of Query strings which are used eventually to query external services.
Issue is based on certain criteria I have to group and remove duplicates from this table.
There are few rules to determine grouping of Strings in different rows.One of them is that if first name and lastname are same then ignore category and type values, therefore above two rows will be grouped to one. There are around million rows. Comparing Strings and doing grouping is not looking elegant solution. What could be best possible solution using sql.
This is my first post here and I am a database newbie. I tried to find an answer, but I'm not sure I understand how it applies to my case.
My access program generates a query with different types of fields (Autonumber, shorttext, memo) which is then used to create a report.
It has been working fine until now, but since the DB has grown I run into a problem.
I use the ID (primary key) IN () the where condition to filter the report. I make a long string that get all the id of the selected records:
WHERE ID IN (1200,1201,1203,1226,1227,1228,1229,...)
When a certain amount of characters in the query is reached (around 4000), I get Chinese characters instead of all the memo fields, and only the memo fields, of the query results (and then in the report).
Is there a limit in the query size? Isn't it 32000 characters?
Why do these characters shows only if I select too many records?
Is there a substite to IN () that could help me reduce the query lengh or should I completely avoid memo fields?
EDIT : That's the query (stripped down a little to be readable) :
SELECT ObjetsLegislatifs.IDobjet, ObjetsLegislatifs.TitreObjet, ObjetsLegislatifs.ContenuObjet
FROM ObjetsLegislatifs
WHERE IDobjet IN(1200,1201,1203,1226,1227,1228,1229,1230,1231,1232,)
GROUP BY ObjetsLegislatifs.IDobjet, ObjetsLegislatifs.TitreObjet, ObjetsLegislatifs.ContenuObjet
ORDER BY ObjetsLegislatifs.IDobjet;
Basicaly, th IDobjet is a autonumber, the "TitreObjet" and "ContenuObjet" are Memos fields.
While IDobjet always shows the proper number, the memo fields start showing chinese when the query is too long, when a certain threshold is reached. I tried with a text field instead in the query and they work fine.
The best thing for you to do is create a temporary table (which is a special type of table - see here) with a single ID column.
When generating the query, you can then insert your ID into this table and join to it instead of using an IN list, something like this:
select a.*
from table a
INNER JOIN temp_table b ON a.ID = b.ID
I have a table (a) that contains imported data, and one of the values in that table needs to be joined to another table (b) based on that value. In table b, sometimes that value is in a comma separated list, and it is stored as a varchar. This is the first time I have dealt with a database column that contains multiple pieces of data. I didn't design it, and I don't believe it can be changed, although, I believe it should be changed.
For example:
Table a:
column_1
12345
67890
24680
13579
Table b:
column_1
12345,24680
24680,67890
13579
13579,24680
So I am trying to join these table together, based on this number and 2 others, but when I run my query, I'm only getting the one that contain 13579, and none of the rest.
Any ideas how to accomplish this?
Storing lists as a comma delimited data structure is a sign of bad design, particularly when storing ids, which are presumably an integer in their native format.
Sometimes, this is necessary. Here is a method:
select *
from a join
b
on ','+b.column_1+',' like '%,'+cast(a.column_1 as varchar(255))+',%'
This will not perform particularly well, because the query will not take advantage of any indexes.
The idea is to put the delimiter (,) at the beginning and end of b.column_1. Every value in the column then has a comma before and after. Then, you can search for the match in a.column_1 with commas appended. The commas ensure that 10 does not match 100.
If possible, you should consider an alternative way to represent the data. If you know there are at most two values, you might consider having two columns in a. In general, though, you would have a "join" table, with a separate row for each pair.
I have a table 'Asset' with a column 'AssetDescription'. Every row of it has some group of words/sentences, seprated by comma.
row1: - flowers, full color, female, Trend
row2:- baby smelling flowers, heart
Now if I put a search query like:-
select * from Asset where contains(AssetDescription,'flower')
It returns nothing.
I have one more table 'SearchData' with column 'SearchCol', having similar rows as mentioned above in table 'Asset'.
Now if a put a search query like:-
select * from SearchData where contains(SearchCol,'flower')
It returns both the rows.
QUESTION:-
Why first query doesn't return any result, but second one does correctly.
If 'Full Text Search' has something to do with 1st ques, than what to do regarding that. As I'm using SQL server 2000.
Clearing a comment doubt on my question:-
Table 'SearchData' has more than 100,000 rows and so as the table 'Asset'.
Those two tables are NOT identical. But their respective columns has rows that contains some group of words seperated by commas. (So flowers, flower etc etc are in plenty in both of those columns.)
Screenshot of the Indexes of both the tables (Asset and SearchData):-
it probably has something to do with your full-text index configuration.
Can you post some info on your index and catalog?
If you read the article on CONTAINS you will see that when searching for
contains(AssetDescription,'flower')
'flower' is treated as a simple term which
matches an exact word or phrase
However for
contains(AssetDescription,'flower*')
'flower' is treated as a prefix term which
specifies a match of words or phrases beginning with the specified
text
and will match 'flowers' in your data.
So, are you sure that data in your two tables is the same, or does 'Asset' contain 'flowers' and 'SearchData' contain 'flower'?
GOT THE SOLUTION.
THANKS TO ALL and especially DIEGO for support.
To allow the FULL TEXT SEARCH (FTS) to work properly:-
Enable FTS for the required table.
Enable FTS for the required column under that table.
Open the properties for the same table and check if attributes 'Full-text change tracking' and 'Full-text update-index' are enabled. If not, enable them.
DONE.
: )