Search code examples
postgresqlsubstringpgadmin-4

Substring last 3 characters of an id of an object within an area


I am intending to grab the last three characters from an id. In my code, you can see that I am using ST_WITHIN() to get an object within another object. I am then grabbing the "node_id" of all objects within that area. Below is the code:

SELECT SUBSTRING ((
    SELECT "node_id" from sewers.structures
            WHERE(
            ST_WITHIN(
                ST_CENTROID((ST_SetSRID(structures.geom, 4326))), 
                ST_SetSRID((SELECT geom FROM sewers."Qrtr_Qrtr_Sections" WHERE "plat_page" = '510C'),4326)) )),5,3)

This portion of the code works without issue:

SELECT "node_id" from sewers.structures
        WHERE(
        ST_WITHIN(
            ST_CENTROID((ST_SetSRID(structures.geom, 4326))), 
            ST_SetSRID((SELECT geom FROM sewers."Qrtr_Qrtr_Sections" WHERE "plat_page" = '510C'),4326)) )

But when I run the SELECT SUBSTRING() on the selection, I get the following error:

ERROR:  more than one row returned by a subquery used as an expression
SQL state: 21000

Solution

  • The substring function should be called on each element, not on the entire query:

    SELECT SUBSTRING("node_id",5,3) 
    FROM sewers.structures
    WHERE ST_WITHIN ...