Hi is it possible to have a update SQL statement with a SELECT substring SQL statement?
How would I convert this to an update statement?
SELECT
SUBSTRING([Col1], 2, LEN([Col1])-2)
FROM table1
Any help would be much appreciated.
UPDATE table1 set Col1 = SUBSTRING([Col1], 2, LEN([Col1])-2)
Related
I need to execute the same exact query on oracle and postgresql, in which i need to delete rows from table 1, using a join on table 2.
I already had a working query, but it stopped working for oracle with 1000+ results in the "IN" statement.
delete from t1
where
t1.oid IN
(SELECT oid from t2 WHERE [condition])
I've read about joins but postgresql uses the "using" keyword instead
DELETE [target table]
FROM [table1]
INNER JOIN [table2]
ON [table1.[joining column] = [table2].[joining column]
WHERE [condition]
Any help is appreciated, thanks
Oracle does not support a JOIN or USING or something similar for the DELETE statement. Your only choices are IN or EXISTS, if you need something that works on Postgres and Oracle.
If an IN condition is too slow then try an EXISTS condition:
delete from t1
where exists (select *
from t2
where t2.oid = t1.oid
and [condition])
You can use and multi-dimensional IN expression (which should work up to 100,000 items):
DELETE FROM t1
WHERE ( t1.oid, 1 ) IN ( SELECT oid, 1 FROM t2 );
or EXISTS (which should work for any volume of rows):
DELETE FROM t1
WHERE EXISTS ( SELECT 1 FROM t2 WHERE t1.oid = t2.oid );
And add the WHERE condition into the sub-query as required.
PostgreSQL db<>fiddle
Oracle db<>fiddle
I want to check the MAX(LENGTH) of all VARCHAR columns of my Redshift table, so I have an idea of a better limitation I could put on the column sizes. (Right now all columns are VARCHAR(65535))
I can write a query like:
SELECT MAX(LENGTH(col1)), MAX(LENGTH(col2)), ...
FROM my_table
But isn't there a way I could write my query so it basically says "apply this for every column"? I've tried the answer from this post but it seems it only applies to classic PostgreSQL
You can use the following SQL the generate your select
select sql from (
select 1 o, 'select ' sql
union
select 2, 'max(length('+attname+')),'
from pg_class c
join pg_attribute a on c.oid = a.attrelid
where relname = '<your_table>'
and attnum > 0
union
select 3, 'from <your_table>'
)
order by o
The output will look like this
select
max(length(col1)),
max(length(col2)),
...
max(length(coln)), -- <- remove the last comma
from <your_table>
You can run this sql to get all max lengths from your table
Please let me know if this helps you.
Table columns structured like:
longitude, latitude, gid, Hash
-78.885636, 36.854, 1, empty
Using PostgreSQL 9.4 and trying to update column Hash with results of a geohash function:
SELECT ST_GeoHash(ST_SetSRID(ST_MakePoint(longitude::float, latitude::float), 4326))
FROM my_table;
To update the column, I am using:
UPDATE my_table SET Hash = (SELECT ST_GeoHash(ST_SetSRID(ST_MakePoint(longitude::float, latitude::float), 4326))
FROM my_table);
But I get an error:
ERROR: more than one row returned by a subquery used as an expression.
I'm new to this so I may be asking a tedious question. Any help would be appreciated. For now I'll be RTFM-ing.
To update Hash column with a calculated value you need to use this query, where gid is a primary key(probably you need to change it).
UPDATE my_table
SET Hash = my_table_2.geo_hash
FROM
(SELECT gid,
ST_GeoHash(ST_SetSRID(ST_MakePoint(longitude::float, latitude::float), 4326)) as geo_hash
FROM my_table) as my_table_2
WHERE my_table.gid = my_table_2.gid
This worked for me:
UPDATE my_table SET "Hash" = (
SELECT ST_GeoHash(ST_SetSRID(ST_MakePoint(longitude::float, latitude::float), 4326))
FROM my_table p
WHERE my_table.gid = p.gid
);
I found this solution on the SQL Server forum on how to reorder records in a table.
UPDATE SomeTable
SET rankcol = SubQuery.Sort_Order
FROM
(
SELECT IDCol, Row_Number() OVER (ORDER BY ValueCOL) as SORT_ORDER
FROM SomeTable
) SubQuery
INNER JOIN SomeTable ON
SubQuery.IDCol = SomeTable.IDCol
When I try doing the same on PostgreSQL, I get an error message -
ERROR: table name "sometable" specified more than once
Any help will be appreciated.
Thanks!
You don`t need to explicitly join SomeTable, how cool is that? :)
UPDATE SomeTable
SET rankcol = SubQuery.Sort_Order
FROM
(
SELECT IDCol, Row_Number() OVER (ORDER BY ValueCOL) as SORT_ORDER
FROM SomeTable
) SubQuery
where SubQuery.IDCol = SomeTable.IDCol
remark: Postgres is case insensitive, better use lower-case, like row_number, sort_order, id_col , etc.
I am using IN clause for column "job_no". In this in clause i checking 1000 values, query retreiving the values but some of the job number are not existed, then how to find unmattched values in the in clause.
assuming you really are using Oracle:
create type table_of_integers is table of integer;
/
select * from table(table_of_integers(1, 2, 3))
where column_value not in (select job_no from my_table);
or you should be able to achieve the same thing using an outer join, such as this example for postgres:
select *
from (select unnest(array[1, 2, 3]) as job_no) j
left outer join my_table using(job_no)
where my_table.job_no is null;
Insert the values into a temporary table instead and do a LEFT OUTER JOIN to join with your data.