Sql query to select records starting with + - sql

Basically I have a user table where mobile is not stored. I want to run a query to select all records starting with +91.
My current query is
Query - select count(*) from temp_table where cell_phone_no like "+91%"; Results - 0
While I know for a fact that there are certain rows with mobile numbers starting with +91.

Important: you must to know us your DBMS, because the sintax can be different, but I try to answer to your question
If you have really these rows (the row as + 91 is not good), your query can be re-write in this way:
select count(*) from temp_table where cell_phone_no like '+91%'
So, I've changed the " with a single quote '

You should check for other hidden values. I might suggest starting with this:
select cell_phone
from temp_table
where cell_phone_no like '%+%91%'
This should get any number that has a + and a 91 in it somewhere. If you get rows from this query, you need to investigate why yours doesn't work.

Related

Sql Limit clause based in input Parameter

I have been trying to find a solution for a limit-clause based on an input parameter from a Json-File. The current code looks somewhat like this
With myJsonTable (JsonText)
as (
Select JsonText)
Select * from Data
Where...
Limit
Case
WHEN (Select JSON_VALUE(JsonText, '$."Amount"') From myJsonTable is not null
THEN (Select JSON_VALUE(JsonText, '$."Amount"') From myJsonTable)
ELSE (10000000)
END
Which I cant seem to get work. The Output I am getting is
Non-negative integeter value expected in LIMIT clause
Is there a way to cast the select done? Trying different Selects anywhere in the Case clause caused the same error.
Exasol only allows constant expression in the limit clause, so it's not directly possible to specify a select statement that references myJsonTable there.
However, you can workaround this issue by using a approach similar to SQL query for top 5 results without the use of LIMIT/ROWNUM/TOP

Query to output results using "like"

I might be over-analyzing this but I have 13,000 records stored in a temp table that only has one column.
I'm trying to determine if those records exist in another database/table but there's no key between the two other than the one column.
The query I run has to use LIKE so something like this...
declare #string Varchar(25) = (select top 1 * from accts)
select content from db2..[mc3] where content like '%'#string+'%'
But I have check to see which one's are in there but I don't want to do it manually one at a time.
Is there a way to have it programmatically go through all of my accounts to see which one's are in that database/table?
This may take a while, but you can get the matching ones using:
select a.??
from accts a
where exists (select 1
from db2..mc3
where mc3.content like '%' + a.?? +'%'
);
This gets accounts that are in mc3 according to your rule.
I should note: performance will be pretty bad. Better than a cursor but that's not saying much.

Counting Values in a repeated field in BigQuery

I want to select rows that have more thank k values in a repeated field. (consider for example selecting user that have more than 3 email addresses)
In Standard SQL I know I can use
SELECT * FROM dataset.users
WHERE array_length(email_address) > 3
But what is the way to do this in BigQuery legacy SQL?
No need for a subquery; you should be able to filter with OMIT RECORD IF directly:
SELECT *
FROM dataset.users
OMIT RECORD IF COUNT(email_address) <= 3;
Do you mind commenting on why you want to use legacy SQL, though? If you encountered a problem with standard SQL I'd like to understand what it was so that we can fix it. Thanks!
Counting Values in a repeated field in BigQuery
BigQuery Legacy SQL
SELECT COUNT(email_address) WITHIN RECORD AS address_count
FROM [dataset.users]
If you want then to count output rows - you can use below
SELECT COUNT(1) AS rows_count
FROM (
SELECT COUNT(email_address) WITHIN RECORD AS address_count
FROM [dataset.users]
)
WHERE address_count> 3

Fastest way to check if any case of a pattern exist in a column using SQL

I am trying to write code that allows me to check if there are any cases of a particular pattern inside a table.
The way I am currently doing is with something like
select count(*)
from database.table
where column like (some pattern)
and seeing if the count is greater than 0.
I am curious to see if there is any way I can speed up this process as this type of pattern finding happens in a loop in my query and all I need to know is if there is even one such case rather than the total number of cases.
Any suggestions will be appreciated.
EDIT: I am running this inside a Teradata stored procedure for the purpose of data quality validation.
Using EXISTS will be faster if you don't actually need to know how many matches there are. Something like this would work:
IF EXISTS (
SELECT *
FROM bigTbl
WHERE label LIKE '%test%'
)
SELECT 'match'
ELSE
SELECT 'no match'
This is faster because once it finds a single match it can return a result.
If you don't need the actual count, the most efficient way in Teradata will use EXISTS:
select 1
where exists
( select *
from database.table
where column like (some pattern)
)
This will return an empty result set if the pattern doesn't exist.
In terms of performance, a better approach is to:
select the result set based on your pattern;
limit the result set's size to 1.
Check whether a result was returned.
Doing this prevents the database engine from having to do a full table scan, and the query will return as soon as the first matching record is encountered.
The actual query depends on the database you're using. In MySQL, it would look something like:
SELECT id FROM database.table WHERE column LIKE '%some pattern%' LIMIT 1;
In Oracle it would look like this:
SELECT id FROM database.table WHERE column LIKE '%some pattern%' AND ROWNUM = 1;

Sub-Queries in Sybase SQL

We have an application which indexes data using user-written SQL statements. We place those statements within parenthesis so we can limit that query to a certain criteria. For example:
select * from (select F_Name from table_1)q where ID > 25
Though we have discovered that this format does not function using a Sybase database. Reporting a syntax error around the parenthesis. I've tried playing around on a test instance but haven't been able to find a way to achieve this result. I'm not directly involved in the development and my SQL knowledge is limited. I'm assuming the 'q' is to give the subresult an alias for the application to use.
Does Sybase have a specific syntax? If so, how could this query be adapted for it?
Thanks in advance.
Sybase ASE is case sensitive w.r.t. all identifiers and the query shall work:
as per #HannoBinder query :
select id from ... is not the same as select ID from... so make sure of the case.
Also make sure that the column ID is returned by the Q query in order to be used in where clause .
If the table and column names are in Upper case the following query shall work:
select * from (select F_NAME, ID from TABLE_1) Q where ID > 25