is there a way to perform a where clause that will match only 2 t's independent off where they are located.
such as
Matthew --
would work
Thanatos --
would work
Thanatos T --
would not work
Tom --
would not work
I've been Googling but cant find anything specific about this
any help is apreciated
You could try
SELECT *
FROM Table
WHERE Field LIKE '%t%t%' AND Field NOT LIKE '%t%t%t%'
I'm curious which would be faster, this or Goat CO's answer.
You could use LEN() and REPLACE():
SELECT *
FROM Table
WHERE LEN(REPLACE(field,'t','tt')) - LEN(Field) = 2
Demo: SQL Fiddle
Related
Using Advantage SQL, I have the following query:
SELECT TOP 10 mytable.*
FROM "mytable.ADT" mytable
ORDER BY date DESC
This returns this data set:
INDEX NR NAME Date
---------------------------------------------------
"145443" 115 Bob 19.03.2021 12:26
"23545",1 215 Steve 19.03.2021 12:09
"564543","",0 215 John 19.03.2021 12:09
"456234" 215 Mark 19.03.2021 12:09
What I want to do is work with the data in the INDEX column. But if I run a normal SELECT query with this field name:
SELECT mytable.INDEX etc
it doesn't run. I also cannot add it as an alias or anything.
Are there any workarounds to pull the specific column into a SELECT? My goal is to do text manipulation to remove the quote marks and extract only the pure number that sits in the middle.
This is the first time I've had this issue - I'm guessing the word INDEX is somehow a function name which screws things up. But I also am guessing there is a way around it?
Thanks.
INDEX is a SQL keyword -- and probably reserved, which is the problem.
You need to escape it. I believe Advantage supports both double quotes and square braces:
SELECT mytable."INDEX", mytable.[INDEX]
FROM "mytable.ADT" mytable
order by date desc
Note that capitalization is important in some databases (but not Advantage SQL) when you escape column names.
I need to get exact name string match from multiple values using Contains function.
I used below query to get data which matches exactly JOHN SMITH OR MIKE DAVID but query is fetching all data which has JOHN SMITH, JOHN, SMITH, MIKE, DAVID, MIKE DAVID, JOHN..SMITH, JOHN/SMITH,....
where contains(names,'{JOHN SMITH} OR {MIKE DAVID}')>0
Note - I don't want to use multiple like in OR conditions.We need to pass around 200 to 300 values (names) to do match pattern.
Can anyone let me know how to get exact match from multiple values using CONTAINS?
Thanks
Anand
I don't use Oracle but i liked this question and i will try to answer it based on what i read on Oracle's documentation, regarding CONTAINS.
so i am currently reading about Stored Query Expression (SQE),
Stored Query Expression (SQE)
Use the SQE operator to call a stored query expression created with
the CTX_QUERY.STORE_SQE procedure.
Stored query expressions can be used for creating predefined bins for
organizing and categorizing documents or to perform iterative queries,
in which an initial query is refined using one or more additional
queries.
So perhaps you could pass all the names you want to query in such a way. For example:
begin
ctx_query.store_sqe('mynames', 'JOHN SMITH OR MIKE DAVID OR GEORGE HARRIS OR JOHN DOE');
end;
And then call it:
SELECT SCORE(1), nameid FROM mytable
WHERE CONTAINS(names, 'sqe(mynames)', 1)> 0
ORDER BY SCORE(1);
Hope this was helpful.
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)
)
everyone;
I am working on a database where path information are stored, one simplified table is shown below
path_id | path value
1 //a/b/c/d
2 //a/b/e
3 //a/b
4 //a/bcd
So here is the question, how can I get information where has '//a/b' as the prefix? In this case, the result should be:
path_id | path value
1 //a/b/c/d
2 //a/b/e
3 //a/b
I am seeking for a more elegant and optimized query, other than using logic operators like 'OR'.
Thanks.
SELECT * FROM YourTable WHERE path_value like '//a/b/%' OR path_value = '//a/b'
Note the extra slash before the wild card in the first part of the WHERE statement. This will exclude //a/bcd.
the % acts as a wild card so it anything can follow that part.
SELECT * FROM tableName WHERE path_value like '//a/b/%' OR path_value = '//a/b'
Suppose I have a SQL query like this:
SELECT
tickets.TicketNumber, history.remarks
FROM
AT_DeviceReplacement_Tickets tickets
INNER JOIN
AT_DeviceReplacement_Tickets_History history
ON tickets.TicketNumber = history.TicketNumber;
I get a table like this in repsonse:
ticketNumber | remarks
-------------+------------
1 | "Hello, there is a problem."
1 | "Did you check the power cable?
1 | "We plugged it in and now it works. Thank you!"
2 | "Hello, this is a new ticket."
Suppose that I want to write a query that will concatenate the remarks for each ticket and return a table like this:
ticketNumber | remarks
-------------+------------
1 | "Hello, there is a problem.Did you check the power cable?We plugged it in and now it works. Thank you!"
2 | "Hello, this is a new ticket."
Yes, in the real code, I've actually got these sorted by date, among other things, but just for the sake of discussion, how would I edit the above query to get the result I described?
Have a look at the following questions:
Can I Comma Delimit Multiple Rows Into One Column?
Is it possible to concatenate column values into a string using CTE?
The cleanest solution to this problem is DB dependent. Lentine's links show very ugly solutions for Oracle and SQL Server and a clean one for MySQL. The answer in PostgreSQL is also very short and easy.
SELECT ticket_number, string_agg(remarks, ', ')
FROM
AT_DeviceReplacement_Tickets tickets
INNER JOIN
AT_DeviceReplacement_Tickets_History history
ON tickets.Ticket_Number = history.Ticket_Number
GROUP BY tickets.ticket_number;
(Note you have both ticket_number and TicketNumber in your sample code.)
My guess is that Oracle and SQL Server either (1) have a similar aggregate function or (2) have the capability of defining your own aggregate functions. [For MySQL the equivalent aggregate is called GROUP_CONCAT.] What DB are you using?