How can I write this query in PostgreSQL? Original from Oracle - postgresql

SELECT ACTOR_NAME
FROM MOVIES
WHERE MOVIE_NAME = 'Isle of Dogs'
ORDER BY SUBSTR(ACTOR_NAME, INSTR(ACTOR_NAME, ' ', -1)+1) ASC;

I think what you're looking for is the POSITION command in place of INSTR

Related

Sybase SQL - Select Most recent record and line up with the values from previous record

I am trying the following query on data table listed below....Sybase does not allow row_number() function. Any suggestions would be very helpful:
select
a.item_number,
a.item_rate,
a.item_code,
a.effective_dt,
r_prev.effective_dt,
r_prev.item_rate,
r.item_code
from A a LEFT OUTER join
(SELECT item_number, item_rate, item_code,effective_dt
FROM A
) a_prev
ON a.item_number = a_prev.item_number
AND a.rating_eff_dt < a_prev.rating_eff_dt
order BY a.item_number, r.item_rate, r.item_code, a.effective_dt desc, a_prev.rating_eff_dt
You can use
Select Number(*),<column list>
from table
Thank You.

postgresql how to use boolean in where statement

I'm a really bad in sql , my query is
select * from car_wash where
(select ST_Within((select car_wash.lon_lat from car_wash),(select ST_Buffer(ST_GeomFromText('POINT(65.3 323.2)'),20)))) = true
AND car_wash.was_deleted=false;
But i know that it isn't correct because nested query can return more than 1 column, how to modify this query to use where clause
select *
from car_wash cw
where
ST_Within (
cw.lon_lat,
ST_Buffer(ST_GeomFromText('POINT(65.3 323.2)'),20)
)
AND
not car_wash.was_deleted
I don't use postgresql, but maybe something like this works:
select * from car_wash
where EXISTS (select ST_Within((select car_wash.lon_lat from car_wash),
(select ST_Buffer(ST_GeomFromText('POINT(65.3 323.2)'),20))) within
WHERE within = true)
AND car_wash.was_deleted=false;
If it doesn't work, I have a variant, so tell me when.

postgresql - how to get a handle on fields with ':' character in them

can't seem to get access to the addr:housenumber field in osm data using psql.
Here is the command I'm trying and I'm getting a syntax error:
select planet_osm_polygon.addr:housenumber from planet_osm_polygon, planet_osm_line where planet_osm_line.name ilike '%washington street%' limit 3;
for simplicity, this won't even work:
select addr:housenumber from planet_osm_polygon limit 3;
What about
SELECT "addr:housenumber" FROM planet_osm_polygon LIMIT 3;
?
try with "
select planet_osm_polygon."addr:housenumber" from planet_osm_polygon, planet_osm_line where planet_osm_line.name ilike '%washington street%' limit 3;

How to fetch a part of string upto a chracter?

I want to fetch the names of employees from a table upto the character ':' but couldn't as substr and ltrim is not working as expected. Below given are given some examples:
ABINERI:REBECCA C
CARRINGTON:JAMES M
But I want them in the way given below:
REBECCA C ABINERI
JAMES M CARRINGTON
I just used the query below in Toad for Oracle:
<pre>
<b>select name from employees</b>
</pre>
Please try below query:
select SUBSTR(name,(INSTR(name,':')+1)) || ' ' || SUBSTR(name,1,(INSTR(name,':'))-1) from employees;
hope above query will resolve your issue.

How to translate the PostgreSQL array_agg function to SQLite?

This query works in PostgreSQL:
Select ot.MCode,array_to_string(array_agg(tk1.TName || ',' || ot.TTime), ' - ') as oujyu_name_list
From TR_A ot
inner join MS_B tk1 on ot.Code = tk1.Code
Where ot.Code in (Select Code From TR_C )
Group byot.MCode
but it does not work in SQLite, because SQLite does not have the array_agg() function. How can this query be converted to SQLite?
For this query, you can use group_concat, which directly returns a string:
SELECT ..., group_concat(tk1.TName || ',' || ot.TTime, ' - ')
FROM ...
SQLite now has the JSON1 extension (which ships in the default distribution) that can group and create arrays of JSON objects. For example,
select
ot.MCode,
json_group_array(json_object('tname', tk1.TName, 'ttime', ot.TTime)) as oujyu_name_list
from TR_A as ot
inner join MS_B as tk1
on (ot.Code = tk1.Code)
where ot.Code in (select code from TR_C)
group by ot.MCode;
The second column will be formatted as a JSON array e.g. [{"tname":...,"ttime":...},...].