Ordering Postgresql query result on Housenumber column by custom comparer - sql

I'm using postgresql as DB.
Using my query to select column housenumber of varchar type(and some other columns) from table with buildings info. So I want the result to be ordered other way, rather then string comparison.
For example, if I have following results:
"1"
"1 block2"
"1 b30"
"1 b3"
"1 b3 s4"
"10"
"2"
I want this result to be sorted by following logic:
1) getting source string "1 b3 s4"
2) split it into ["1" , "b3" , "s4"]
3) try to parse all substrings to integer, ignoring letters, which
are not numbers into [1 , 3, 4]
4) calculate bigger number for future sorting as
1 * 1000000 + 3 * 1000 + 4 = 1003004.
Is this possible and how could I implement this methoad and use it for sorting query result?
Here is my sql query(shorted):
SELECT housenumber, name
FROM osm_buildings
where
housenumber <> ''
order by housenumber
limit 100

I'm not sure why you would want to convert to some big integer for sorting. You can do the following:
Remove all characters that are not digits or spaces.
Convert to an array, splitting on one or more spaces.
Convert the array to an integer array.
Then you can can sort on this:
order by regexp_split_to_array(regexp_replace(v.addr, '[^0-9 ]', '', 'g'), ' +')::int[]
You can store this as a value in a table, if you want to persist it.
Here is a db<>fiddle.

Related

Count spaces before a string value SQL

so I have a table TEST with 2 columns - id and value. I would like a count of the number of spaces before a specific string value.
id value
1 AB CD EFD RA
I would like a query to count the number of spaces in the value column to the left of "EFD". In this case it should be 2.
Thanks!
I tried:
select ID,
VALUE,
regexp_substr(Value, 'EFD') AS "SUBSTRING",
regexp_instr(Value, 'EFD') AS "POSITION"
I would like to get the position of EFD in this array as well. I should get 3.
select id, value,
regexp_substr(Value, 'EFD') AS substring,
regexp_count(substring(Value, 1, regexp_instr(Value, 'EFD')-1), ' ') AS spaces_before_EFD,
regexp_instr(Value, 'EFD') AS position
from test;

Search PostgreSQL column for substring

I have a DB column that has entries like this:
"56/45/34"
"78/34/145"
"45"
"" (i.e. NULL)
I want to search for the rows that match a certain number - for example "45" would should return the first and third rows but not the second.
We can try using a regex approach here with word boundaries:
select col
from your_table
where col ~* '\y45\y';
Demo
You can convert the delimited string to an array and then test the array
select *
from the_table
where '45' = any(string_to_array(the_column, '/'))

sql query about string function

ID MOBILE
1 9869600733
2 9869600793
3 9869600799
all id whose mobile number containing 9 three times(using string functions like replace, substr, etc)... ? (without like , % , etc)
You can use LEN and Replace
Where len(MOBILE)-len(replace(MOBILE ,'9',''))>=3
Note : Some DBMS uses LENGTH instead of LEN
Where length(MOBILE)-length(replace(MOBILE ,'9',''))>=3
DEMO
replace(MOBILE ,'9','') will replace all the 9's with empty
string
length(MOBILE) will count the number of characters in Mobile
column
length(replace(MOBILE ,'9','')) will count the number of characters
in Mobile column as replacing 9's with empty string
length(MOBILE)-length(replace(MOBILE ,'9','')) here the
difference will tell the number of missing characters that is our 9, you can use this difference to count the 9
exactly three '9's:
Select * from mytable
Where len(mobile) - len(replace(mobile, '9', '')) = 3
at least three '9's:
Select * from mytable
Where len(mobile) - len(replace(mobile, '9', '')) >= 3

SQL SELECT statement with Cast, LPad and Max

Are there any issues with this select statement ??
SELECT SUBSTR(FIELD_A,10,3) as MOVID,
MAX(LPAD((CAST(SUBSTR(FIELD_A,-3,3) as INT) + 1 ), 3, 0)) as NEXTMOVID
FROM ...
The field is a VARCHAR2.
I want to maintain 3 characters(which are numbers) and add 1 and concatenate with another varchar2
retrieve a portion of the FIELD_A
convert is to an integer
add 1
Left Pad it to 3 characters with 0
Grab the MAX
Later on I concatenate with another field
Wondering if there was a better way to do this ??
As I understand, you need following:
SELECT SUBSTR(FIELD_A,10,3) as MOVID,
to_char(to_number(SUBSTR(FIELD_A, -3, 3)) + 1), '000') as NEXTMOVID
FROM ...

sql FInding strings with duplicate characters

I have a list of strings:
HEAWAMFWSP
TLHHHAFWSP
AWAMFWHHAW
AUAWAMHHHA
Each of these strings represent 5 pairs of 2 character combinations (i.e. HE AW AM FW SP)
What I am looking to do in SQL is to display all strings that have duplication in the pairs.
Take string number 3 from above; AW AM FW HH AW. I need to display this record because it has a duplicate pair (AW).
Is this possible?
Thanks!
Given current requirements, yes this is dooable. Here's a version which uses a recursive CTE (text may need to be adjusted for vendor idiosyncracies), written and tested on DB2. Please note that this will return multiple rows if there is more than 2 instances of a pair in a string, or more than 1 set of duplicates.
WITH RECURSIVE Pair (rowid, start, pair, text) as (
SELECT id, 1, SUBSTR(text, 1, 2), text
FROM SourceTable
UNION ALL
SELECT rowid, start + 2, SUBSTR(text, start + 2, 2), text
FROM Pair
WHERE start < LENGTH(text) - 1)
SELECT Pair.rowid, Pair.pair, Pair.start, Duplicate.start, Pair.text
FROM Pair
JOIN Pair as Duplicate
ON Duplicate.rowid = Pair.rowid
AND Duplicate.pair = Pair.pair
AND Duplicate.start > Pair.start
Here's a not very elegant solution, but it works and only returns the row once no matter how many duplicate matches. The substring function is for SQLServer, not sure what it is for Oracle.
select ID, Value
from MyTable
where (substring(Value,1,2) = substring(Value,3,4)
or substring(Value,1,2) = substring(Value,5,6)
or substring(Value,1,2) = substring(Value,7,8)
or substring(Value,1,2) = substring(Value,9,10)
or substring(Value,3,4) = substring(Value,5,6)
or substring(Value,3,4) = substring(Value,7,8)
or substring(Value,3,4) = substring(Value,9,10)
or substring(Value,5,6) = substring(Value,7,8)
or substring(Value,5,6) = substring(Value,9,10)
or substring(Value,7,8) = substring(Value,9,10))