I have a very curious question.
We have query to select records from table based on some condition. In general the syntax for the query is as below
SELECT * FROM TABLENAME WHERE COLUMNNAME='VALUE';
Now the question is that will this query will work if we interchange the position of COLUMNNAME and 'VALUE'.
Yes, it will. =)
Why did you not just try?
Yes. The following will work:
SELECT * FROM TABLENAME WHERE 'VALUE' = COLUMNNAME;
In fact, in Oracle at least, you can do some twisted but somewhat useful things like:
select *
from tablename
where 'VALUE' in (field1, field2, field3)
You mean
SELECT * FROM TABLENAME WHERE 'VALUE' = COLUMNNAME;
I tested it, it works on MSSQL Servver 2008
SELECT * FROM TABLENAME WHERE 'VALUE' = COLUMNNAME;
if write something like this.. it'll work for sure..
Related
I wanted to know a Query where in which i want to locate a specific table that contains two or more columns:
I tried:
SELECT *
FROM DB
WHERE TableName = 'TableName'
AND ColumName in('column1' , 'column2')
But this query will look if any of those columns are there, but i want it to return only if all of them are a match.
I hope this questions makes sense.
This should work for you in MySQL:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME in ('column1','column2')
AND TABLE_SCHEMA='your_database'
GROUP BY table_name
HAVING COUNT(COLUMN_NAME) =2;
The operator IN is a concatenation of OR. However, there's no way to creare a short concatenation of AND as well.
See this question.
I am using Oracle SQL Developer. I need to find the rows where column1 starts with ‘987’ or ‘I’. Column1 is a String(18). Some sample patterns in this column include: 9(9), 9(12), and others. I am not familiar with the code to see how a column starts with certain values in Oracle SQL. Sample Code is below. Attempt below.
Code
select * from table1
where column1
Attempt Code
SELECT
REGEXP_SUBSTR(column1,
'987') "REGEXP_SUBSTR"
FROM table1;
You can use a regular expression for this:
where regexp_like(column1, '^(987|I)')
You just need to use LIKE.
select *
from table1
where column1 like '987%' or column1 like 'I%';
CREATE TABLE hs(WH VARCHAR2(100));
SELECT
*
FROM
hs
WHERE
REGEXP_LIKE(WH,'^987|^I', 'i')
ORDER BY WH;
What is the most efficient and elegant SQL query looking for a string containing the words "David", "Moses" and "Robi". Assume the table is named T and the column C.
Select * from table where
columnname like'%David%' and
columnname like '%Moses%' and columnname like'%Robi%'
In SQL Server 2005+ with Full-Text indexing switched on, I'd do the following:
SELECT *
FROM T
WHERE CONTAINS(C, '"David" OR "Robi" OR "Moses"');
If you wanted your search to bring back results where the result is prefixed with David, Robi or Moses you could do:
SELECT *
FROM T
WHERE CONTAINS(C, '"David*" OR "Robi*" OR "Moses*"');
Here is what I uses to search for multiple words in multiple columns - SQL server
Hope my answer help someone :) Thanks
declare #searchTrm varchar(MAX)='one two three ddd 20 30 comment';
--select value from STRING_SPLIT(#searchTrm, ' ') where trim(value)<>''
select * from Bols
WHERE EXISTS (SELECT value
FROM STRING_SPLIT(#searchTrm, ' ')
WHERE
trim(value)<>''
and(
BolNumber like '%'+ value+'%'
or UserComment like '%'+ value+'%'
or RequesterId like '%'+ value+'%' )
)
If you care about the sequence of the terms, you may consider using a syntax like
select * from T where C like'%David%Moses%Robi%'
Oracle SQL :
select *
from MY_TABLE
where REGEXP_LIKE (company , 'Microsodt industry | goglge auto car | oracles database')
company - is the database column name.
results - this SQL will show you if company column rows contain one of those companies (OR phrase)
please note that : no wild characters are needed, it's built in.
more info at : http://www.techonthenet.com/oracle/regexp_like.php
if you put all the searched words in a temporaray table say #tmp and column col1, then you could try this:
Select * from T where C like (Select '%'+col1+'%' from #temp);
Maybe EXISTS can help.
and exists (select 1 from #DocumentNames where pcd.Name like DocName+'%' or CD.DocumentName like DocName+'%')
Oracle SQL:
There is the "IN" Operator in Oracle SQL which can be used for that:
select
namet.customerfirstname, addrt.city, addrt.postalcode
from schemax.nametable namet
join schemax.addresstable addrt on addrt.adtid = namet.natadtid
where namet.customerfirstname in ('David', 'Moses', 'Robi');
i have 2 fields in database and i would like to find the difference of them in percentage.
So mathematical the formula should be
ABSOLUTE((field1-field2)/(MAXIMUM(field1, field2)))
the problem is I dont know how to ask for maximum of 2 numbers. Since MAX in sql returns the max of column.
SELECT ABS(field1 - field2) / GREATEST(field1, field2)
FROM mytable
maybe someting like:
if (field1 > field2, (field1 - field2)/field1, (field2 - field1) / field1)
How about simply this
ABS((field1-field2)/(GREATEST(field1, field2)))
Try an IF:
SELECT IF(field1 < field2, field1, field2).
You are looking for the function named GREATEST, since MAX is used elsewhere in SQL.
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_greatest
I am using sql.
How to select all items from a table.
where a column (varchar) path is start with a certain string : "stringABC"
select * from ATable where path like 'stringABC';
The above sql is wrong, can any one help me to fix it?
Many thanks!
select *
from ATable
where path like 'stringABC%'
select * from ATAble where path like 'string%'