I have table that contains column FULL_NAME, and I'm doing search and getting result. I need to sort these columns by position of search(search term).
Example Of data
Column (FULL_NAME)
ID
FULL_NAME
1
zaid said Alabri
2
said sleem salim AlAhmedi
3
salim zaid Ahmed AlZaid
4
Ahmed said zaid AlSalimi
Search example:
SELECT FULL_NAME
FROM Table
WHERE(FULL_NAME LIKE N'%ِAhmed%')
I need result to be like this
ID
FULL_NAME
1
Ahmed said zaid AlSalimi
2
said Ahmed salim AlAhmedi
3
salim zaid Ahmed AlZaid
4
said sleem salim AlAhmedi
This what I'm seeking result sort by position after query result.
Since you have tagged SQL Server as your database please try this:
select *
from table
where full_name like N'%Ahmed%'
order by charindex('Ahmed',full_name,1)
Something like this could work for you:
select *
from table
where full_name like N'%Ahmed%'
order by charindex(N'Ahmed', full_name)
The charindex function returns the character number where the search matches a string.
I think if you are using SQL server you can use a conditional order by and using CHARIndex
as follow
SELECT EnglishName
FROM Employees
where EnglishName like N'%Ahmed%'
order by charindex(N'Ahmed',EnglishName,1)
Related
Table description
Table Name:- Name condition
Name | Pattern
A | %A% or Name like %a%
B | %B% or Name like %b%
C | %C% or Name like %c%
D | %D% or Name like %d%
E | %E% or Name like %e%
F | %F% or Name like %f%
G | %G% or Name like %g%
Table name:- Employees
Emp_ID | EMP_NAME
1 | Akshay
2 | Akhil
3 | Gautam
4 | Esha
5 | bhavish
6 | Chetan
7 | Arun
[Table description] [1]: https://i.stack.imgur.com/wvOgr.png
Above are my two tables now my query is (in the image)
Select * from Employees,Name_condition where EMP_NAME like Pattern
Here the query is correct syntactically but produces wrong output.
It takes the column Pattern as a string and searches for it in EMP_NAME and it will find nothing.
So my question is how we can take the values present in the Pattern column as a condition and not as a string so that the query will become like this
Select * from Employees,Name_condition where EMP_NAME like ‘%A%’ or Name like ‘%a%’
what i need is when i pass colunm name(Pattern) in the where condition it takes %A% or Name like %a% whole as a string but i want that select * from Employees,Name_condition where EMP_NAME like Pattern Here the column name pattern internally must be replace by the value present in the column and the the query produces o/p like this
Select * from Employees,Name_condition where EMP_NAME like ‘%A%’ or Name like ‘%a%’
Desired Result:-I expect all the rows in my result which includes bhavish but as we see we have a like condition in the column itself like %B% or Name like %b%
What i want is when it matches
where EMP_NAME like Pattern
The value of pattern must internally replaced by
%B% or Name like %b%
and the it produces the output which includes bhavish which starts with b
try:
select *
from employees
where emp_name like '%oh%'
or emp_name like '%a%';
Good luck.
Try this from orafaq:
SELECT * FROM employees
WHERE emp_name LIKE '\%a\%' ESCAPE '\';
It's not that simple (and it shouldn't be). If you really have to use such tables you have to write a piece of PL/SQL to handle your conditions.
Two things you have to read about:
dynamic sql
sql injection (because you want to prevent it)
Try to Put && instead of 'And' in condition
I have the following table
Id Author
1 Alexander Mccall Smith
2 Ernest Hemingway
3 Giacomo Leopardi
4 Henry David Thoreau
5 Mary Higgins Clark
6 Rabindranath Tagore
7 Thomas Pynchon
8 Zora Neale Hurston
9 William S. Burroughs
10 Virginia Woolf
11 William tell
I want to search the Author by putting first few characters of the first and last name.
eg: Search Text: Will tel
Then the search result show the following result
William tell
eg: Search Text: will Burrou
Then the search result show the following result
William S. Burroughs
eg: Search Text: Will
Then the search result show the following result
William S. Burroughs
William tell
What is the efficient way to achieve this in sql server ?
As you mentioned this can be achieved using Full Text Search. You have to create the FTS catalog and then index on the table and column(s). You stated in the title 'Columns' but I only see one table column in your example so I will create the queries using that.
-- example 1 searching on Will and Tel
SELECT Id, Author
FROM Authors
WHERE CONTAINS(Author, '"Will*" AND "tel*"')
-- example 2 searching on Will and Burrou
SELECT Id, Author
FROM Authors
WHERE CONTAINS(Author, '"will*" AND "Burrou*"')
-- example 3 searching on Will
SELECT Id, Author
FROM Authors
WHERE CONTAINS(Author, '"will*"')
For further reference see
The Contains clause which searches for precise or fuzzy matches.
Article Query with Full-Text Search.
Less efficient than #Igor's answer as the table size grows, but you can also use the Like statement.
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
-- example 1 searching on Will and Tel
SELECT Id, Author
FROM Authors
WHERE Author Like('Will%Tel%')
-- example 2 searching on Will and Burrou
SELECT Id, Author
FROM Authors
WHERE Author Like('Will%Burrou%')
-- example 3 searching on Will
SELECT Id, Author
FROM Authors
WHERE Author Like('Will%')
Cons: It is slower than the contains statement.You need to include the % sign after any other keyword you're looking to search for.
Pros: Can be faster than contains statement in cases of smaller(<1000) row tables.
So, I have two tables in SQL Sever 2008 R2:
Table A:
patient_id first_name last_name external_id
000001 John Smith 4753-23314.0
000002 Mike Davis 4753-12548.0
Table B:
guarantor_id visit_date first_name last_name
23314 01/01/2013 John Smith
12548 02/02/2013 Mike Davis
Notice that the guarantor_id from Table B matches the middle section of the external_id from Table A. Would someone please help me strip the 4753- from the front and the .0 from the back of the external_id so I can join these tables?
Any help/examples is greatly appreciated.
Assuming the prefix and suffix are always the same length, just do this:
SUBSTRING(external_id, 6, 5)
The documentation for SUBSTRING is here if you want to look at that.
If the prefix and suffix change, also use CHARINDEX AND LEN.
SUBSTRING(external_id, CHARINDEX(external_id,'-') + 1, CHARINDEX(external_id,'.') - CHARINDEX(external_id,'-') + 1)
Try this one
SELECT *
FROM TABLE_A inner join TABLE_B on TABLE_A.external_id like '%'+TABLE_B.guarantor_id+'%'
This also works. :)
select LEFT(right(external_id, 7), 5)
from table_a
As #woz said, you can use SUBSTRING, if the length is not fixed, you can use the CHARINDEX function, to determine the positions of the dot and dash to make it more flexible.
On another note, joining based on a function will heavily degrade your performance, I suggest updating the field with the function result, or creating a new column STRIPPED_GUARANTOR_ID that has the stripped value, then joining on that column
use substring and charindex. So long as you are looking for the value between the first '-' and '.' characters...
SUBSTRING (
externalid,
CHARINDEX('-',externalid)+1,
CHARINDEX('.',externalid)-CHARINDEX('-',externalid)
)
I have the following data structure :
FIRSTNAME AGE NICKNAME
Jack 28 Benny
Robert 30 Benny
Pascal 20 Benny
Charles 19 Lence
Anthony 20 Lence
The first column is unique.
The idea is that I have to count how many times the "nickname" is used and I want to output it so that I can I have the following result :
Benny 3
Lence 2
What is the best performant way to do so knowing that I have millions of lines?
Try this:
SELECT NICKNAME,COUNT(NICKNAME)
FROM MyTable
GROUP BY NICKNAME
SELECT NICKNAME, SUM(1) FROM table GROUP BY NICKNAME
agregate count group by firstname, there's no other solution.
by the way, the benny nickname only appears 3 times, not 4.
I have problem with sql query.
For example, I have table like this:
ID Name1 Name2 Country
1 Greg Torr Poland
2 John Smith England
3 Tom Jerry USA
I want get all record, which have for example, "la" in Country. In this case:
PoLAnd
EngLAnd
How I can put this in Where clausule?
Greets
Use the LIKE keyword:
SELECT * FROM table
WHERE Country LIKE '%la%'
I think you can use the Like Clause here. The examples are available on
http://www.sql-tutorial.net/SQL-LIKE.asp
http://www.w3schools.com/sql/sql_like.asp
etc.
WHERE CHARINDEX('LA', Country) > 0
alternatively
WHERE Country LIKE '%la%'
If your RDBMS is case-sensitive, convert Country to upper case using the appropriate string function, and compare against the upper case LA with a LIKE:
SELECT *
FROM tbl
WHERE UPPER(Country) LIKE '%LA%'
Enclose your keyword before and after with Percent Symbol
SELECT *
FROM tableName
WHERE Country LIKE '%LA%'