Android SQL statement - sql

I'm new to SQL and I believe there is maybe a mistake.
I would search my database with
SELECT * FROM Data WHERE xx='4*'
I need all data where in colum xx is the first number 4 (string)
44545 and 493435 and 4111111 and so on with all other columns
Is that Statement ok?

You must use the LIKE syntax instead of the equality operator =:
SELECT * FROM Data WHERE xx LIKE '4%'

Related

SQL Query to pull rows starting with a certain 3 digits

I have a numbers column in my sql table and I want to pull all the numbers that start with 3 specific digits; how would I query this?
Use the "LIKE" query:
123 is the prefix. I think you have to store the numbers as strings though, just try it out on your data set :-)
SELECT * from TableName Where ColumnName LIKE '123%'
See also this Q&A: In MySql, find strings with a given prefix

SQL query for any value

I'm building SQL queries on the fly and am a bit of a beginner with them.
How would I do something like:
Select * from X where Type = *Any*
Basically, I want to select all of them. I know I could not include the where but often times the Type variable does in fact have values. I want to be able to replace the "Any" part with something else on the fly rather than a whole different expression...
I think this is what you are looking for:
--please change the datatype int to match your datatype for type column
DECLARE #type int = 1;
SELECT * FROM X
WHERE x.[Type] = COALESCE(#type,x.[type]);
If you don't pass a value in variable #type, it will default to your type and return everything. Otherwise, it will supply the variable value as needed. Hope this helps!
This kind of depends on what your SQL implementation is. That being said the two concepts you are missing are using the keyword "LIKE" instead of "=", and using wildcards. I advise going through W3Schools SQL section to get started, or better yet buy some introduction to SQL book. http://www.w3schools.com/sql/default.asp
SELECT * FROM X
WHERE Type LIKE '%';
select * from X where Type Like '%';
If you want everything it is simple:
SELECT *
FROM my_table
There is no where clause , in this case. You usually want to first try something like this , though:
SELECT *
FROM my_table
WHERE rownum < 2
But rownum is an Oracle-specific column. You need to look it up for your specific database .

How to select data items of a certain length?

How do I select the row of a column such that the row size is <= 5 ?
Is there a query for this which will work on most/all databases ?
eg. id, first_name
Select only those people whose firstname is more than 10 characters. Their name is too long ?
If you are bound to use a specific RDBMS then the solution is easy.
Use the LENGTH function.
Depending upon your database the length function can be LEN, Length, CarLength. Just search google for it.
According to your question
How do I select the row of a column such that the row size is <= 5 ?
Is there a query for this which will work on most/all databases ?
solution can be
SELECT * FROM TableName WHERE LENGTH(name) <= 5
If you want something that can work with almost all the database and I assume that the length of your string that you want to fetch is of a significant small length. Example 5 or 8 characters then you can use something like this
SELECT *
FROM tab
WHERE
colName LIKE ''
OR colName LIKE '_'
OR colName LIKE '__'
OR colName LIKE '___'
OR colName LIKE '____'
OR colName LIKE '_____'
This works with almost all major DBMS.
see example:
SQL Server
MySQL
Oracle
Postgre SQL
SQLite
Assuming you want the length in characters, the function names vary with RDBMS;
MySQL: CHAR_LENGTH().
Oracle: LENGTH().
SQL Server: LEN().
PostgreSQL: CHAR_LENGTH() or LENGTH().
SQLite: LENGTH().
If you want the length in bytes, it's instead;
MySQL: LENGTH().
Oracle: LENGTHB().
SQL Server: DATALENGTH().
PostgreSQL: OCTET_LENGTH().
For example, selecting all rows with names longer than 10 characters in MySQL would be;
SELECT * FROM myTable WHERE CHAR_LENGTH(name) > 10;
In sql servers TSQL you could use the len function.
eg
SELECT * FROM people WHERE LEN(firstname) > 10
Where people is the table name.
In mysql the function is called "Length" instead of len.
In case you call from Java, there is portable JDBC escape function:
select {fn length('some text expression')} from sometable

Sequence restrictions of SELECT statement

In my table I have a column of type nVarchar or nText
Suppose value of a this col is like this '... xxx yyy zzz ...'
I use SELECT to search table with this column
SELECT * FROM tbl_name WHERE col_name like '%xxx yyy%'
Since the I can't force users to enter words with my sequence I want to give same result with this query too:
SELECT * FROM tbl_name WHERE col_name like '%yyy xxx%'
It looks to me like you might want to consider normalizing your table. Specifically, if "yyy xxx" is the same as "xxx yyy", you're storing an array (or list or whatever you want to call it) in one column, which breaks first normal form. Normalize it so that each of those atoms (i.e. "xxx", "yyy") are stored separately and are related back to your original record. Then it's almost trivial to do the kind of thing you're looking to do.
You have to tokenize the user input into separate words inside your application code (not in sql) and then construct queries like
SELECT * FROM tbl_name WHERE
(col_name like '%yyy%') AND (col_name like '%xxx%') AND ...
Your best bet is to use both criteria, and "OR".
SELECT *
FROM tbl_name
WHERE (col_name like '%xxx yyy%')
OR (col_name like '%yyy xxx%');

MySQL question about "reverse LIKEs"

Well given I have a value I want to check for potential matches in a database (in one varchar field) so I write something like:
SELECT * FROM table WHERE column LIKE "%value%"
Which will work if the value is something like "test" and the column has a value of "this is a test" however if it is reversed then I will not get a match I have tried things along the lines of:
SELECT * FROM table WHERE CONCAT("%",column,"%") LIKE "value"
but don't know exactly how to phrase this to Google to get a response I need, please help!
You can reverse a like statement. Just using the same syntax as a regular like query:
select
*
from
table
where
'value' like concat('%', column, '%')
Of course, if you felt wild and crazy, you could also use instr:
select * from table where instr('value', column) > 0
I don't know which one is faster, since I don't have a MySQL instance to test against, but it's worth trying both to see which wins.
SELECT *
FROM table
WHERE 'value' LIKE CONCAT('%', column, '%')