I need to check whether a string matches the pattern in postgresql - string

I need to check if a string is in the format '4.3.10'.
For example, If compare the strings 'AS45' or '456' or '4.1' with the above pattern, I should get a error message or boolean false.

Use the Postgres SIMILAR TO statement for pattern matching. In a query, the pattern matcher would like like this:
SELECT * FROM table WHERE column SIMILAR TO '[0-9]\.[0-9]\.[0-9][0-9]';
You can change the ranges within the brackets as needed.
http://www.postgresql.org/docs/current/static/functions-matching.html

We can use 'SIMILAR TO' operation.
IF we want to match more than one pattern at the same time use the following query.
SELECT * FROM table WHERE column SIMILAR TO '(.._|.|_|..|..|_..|_..|_..|..__)';
Separate each pattern with an 'OR' (|) operator.
In the above query I am comparing 9 different patterns.

Related

Excel: Find words of certain length in string?

I have this file where I want to make a conditional check for any cell that contains the letter combination "_SOL", or where the string is followed by any numeric character like "_SOL1524", and stop looking after that. So I don't want matches for "_SOLUTION" or "_SOLothercharactersthannumeric".
So when I use the following formula, I also get results for words like "_SOLUTION":
=IF(ISNUMBER(FIND("_SOL",A1))=TRUE,"Yay","")
How can I avoid this, and only get matches if the match is "_SOL" or "_SOLnumericvalue" (one numeric character)
Clarification: The whole strings may be "Blabla_SOL_BLABLA", "Blabla_SOLUTION_BLABLA" or "Blabla_SOL1524_BLABLA"
Maybe this, which will check if the character after "_SOL" is numeric.
=IF(ISNUMBER(VALUE(MID(A1,FIND("_SOL",A1)+4,1))),"Yay","")
Or, as per OP's request and suggestion, to include the possibility of an underscore after "SOL"
=IF(OR(ISNUMBER(VALUE(MID(A1,FIND("_SOL",A1)+4,1))),ISNUMBER(FIND("_SOL_",A1))),"Yay","")
Here is an alternative way to check if your string contains SOL followed by either nothing or any numeric value up to any characters after SOL:
=IF(COUNT(FILTERXML("<t><s>"&SUBSTITUTE(A1,"_","1</s><s>")&"</s></t>","//s[substring-after(.,'SOL')*0=0]")>0),"Yey","Nay")
Just to use in an unfortunate event where you would encounter SOL1TEXT for example. Or, maybe saver (in case you have text like AEROSOL):
=IF(COUNT(FILTERXML("<t><s>"&SUBSTITUTE(A1,"_","</s><s>")&"</s></t>","//s[translate(.,'1234567890','')='SOL']")>0),"Yey","Nay")
And to prevent that you have text like 123SOL123 you could even do:
=IF(COUNT(FILTERXML("<t><s>"&SUBSTITUTE(A1,"_","1</s><s>")&"</s></t>","//s[starts-with(., 'SOL') and substring(., 4)*0=0]")>0),"Yey","Nay")

how to filter empty string in azure search

How can I filter out an empty string in a field?
I have tried search.ismatch() using "filter" option and regex using "search" option, but none of them worked.
As commented by juunas, $filter=FieldName ne '' should do the trick for you if your field is of type Edm.String. In addition if you want to filter out empty collection fields, you can do something like $filter=FieldName/any()
Also, to clarify search.ismatch() is a way to include search ranking in filter expressions. The matching criteria takes affect only on documents that satisfy the filter expression. For more details, look at https://learn.microsoft.com/en-us/rest/api/searchservice/odata-expression-syntax-for-azure-search

Elasticsearch like or wildcard query against numeric fields

The problem is, if the long field has value 120450, 120445, 120656. Please find the query below.
{"from":0,"size":10,"query":{"nested":{"query":{"bool":{"must":[{"querystring":{"query":"120","fields":["alist.articleId"]}}]}},"path":"alist"}}}_
The response should return all the three documents which has partial match for 120. Is it possible to achieve this in long or a numeric field ?
For partial matching on numerics, you can store them as string values.
Now, you can use either of the following
use edgeNGram tokenizer
use prefix query, your field needs to be marked not_analyzed in this case

SSIS Conditional Split - check if a string has a substring

I am wondering if there is a function in SSIS Conditional Split, that would tell me if the string in my datacell has a substring "XYZ" or not. The condition would look like this:
CheckIfValueContainsSubstring("XYZ")
Unfortunately I can't find such function. Is there any way of achieving the goal of separating the record which have the substring from the records which don't have it?
An important note: the substring can be anywhere (so the typical substring function doesn't work for me)
The correct expression for this in SSIS is FINDSTRING.
FINDSTRING( «character_expression», «string», «occurrence» )
MSDNArticle
Similar SO Question

Quick SQL question

Working on postgres SQL.
I have a table with a column that contains values of the following format:
Set1/Set2/Set3/...
Seti can be a set of values for each i. They are delimited by '/'.
I would like to show distinct entries of the form set1/set2 and that is - I would like to trim or truncate the rest of the string in those entries.
That is, I want all distinct options for:
Set1/Set2
A regular expression would work great: I want a substring of the pattern: .*/.*/
to be displayed without the rest of it.
I got as far as:
select distinct column_name from table_name
but I have no idea how to make the trimming itself.
Tried looking in w3schools and other sites as well as searching SQL trim / SQL truncate in google but didn't find what I'm looking for.
Thanks in advance.
mu is too short's answer is fine if the the lengths of the strings between the forward slashes is always consistent. Otherwise you'll want to use a regex with the substring function.
For example:
=> select substring('Set1/Set2/Set3/' from '^[^/]+/[^/]+');
substring
-----------
Set1/Set2
(1 row)
=> select substring('Set123/Set24/Set3/' from '^[^/]+/[^/]+');
substring
--------------
Set123/Set24
(1 row)
So your query on the table would become:
select distinct substring(column_name from '^[^/]+/[^/]+') from table_name;
The relevant docs are http://www.postgresql.org/docs/8.4/static/functions-string.html
and http://www.postgresql.org/docs/8.4/static/functions-matching.html.
Why do you store multiple values in a single record? The preferred solution would be multiple values in multiple records, your problem would not exist anymore.
Another option would be the usage of an array of values, using the TEXT[] array-datatype instead of TEXT. You can index an array field using the GIN-index.
SUBSTRING() (like mu_is_too_short showed you) can solve the current problem, using an array and the array functions is another option:
SELECT array_to_string(
(string_to_array('Set1/Set2/Set3/', '/'))[1:2], '/' );
This makes it rather flexible, there is no need for a fixed length of the values. The separator in the array functions will do the job. The [1:2] will pick the first 2 slices of the array, using [1:3] would pick slices 1 to 3. This makes it easy to change.
If they really are that regular you could use substring; for example:
=> select substring('Set1/Set2/Set3/' from 1 for 9);
substring
-----------
Set1/Set2
(1 row)
There is also a version of substring that understands POSIX regular expressions if you need a little more flexibility.
The PostgreSQL online documentation is quite good BTW:
http://www.postgresql.org/docs/current/static/index.html
and it even has a usable index and sensible navigation.
If you want to use .*/.* then you'd want something like substring(s from '[^/]+/[^/]+'), for example:
=> select substring('where/is/pancakes/house?' from '[^/]+/[^/]+');
substring
-----------
where/is
(1 row)

Resources