SQL query inside function

I am using PostgreSQL with PostGis. I execute the following request:

select st_geomfromtext('point(22 232)',432) 

It works great. But now I want to take the value through a query. eg:

 select st_geomfromtext('point((select x from data_name where id=1) 232)' , 432) 

Here, data_name is some table that I use, and x stores some values. Now the request inside is processed as a string and the value is not returned.
Please help.

 ERROR: syntax error at or near "select" 
+6
source share
3 answers

Try the following:

 select st_geomfromtext('point(' || x || ' 232)', 432) from data_name where id=1 
+2
source

Postgis has an ST_MakePoint function, which is faster than ST_GeomFromText .

 select ST_SetSRID(ST_MakePoint(x),432) from data_name where id=1; 
0
source

While @muratgu's answer is usually the way to go , one small note:
A subquery gets an excellent result if no rows are found for id = 1 . Then you will not get anything back (no line), and not:

 select st_geomfromtext( NULL , 432) 

If you need a replacement:

 select st_geomfromtext('point(' || (select x from data_name where id=1) || ' 232)' , 432) 
0
source

All Articles