I know how to use range tables with SELECTs.
However, is it possible to use it in a LOOP's WHERE condition?
If not, is there a workaround?
The syntax is the same like by SELECTs:
LOOP AT ...
INTO|ASSIGNING ...
WHERE field IN range.
...
ENDLOOP.
Related
Can I make SQL statements like this?
SELECT
ARRAY[array]
FROM table1
ORDER BY
FOR i in array_length(array,1) LOOP
array[i]::numeric
END LOOP;
The result I want is:
SELECT
ARRAY[array]
FROM table1
ORDER BY array[1]::numeric, array[2]::numeric, ...
Can I? :)
No, there is no LOOP in SQL. It's an element of procedural languages like PL/pgSQL, though. See:
Postgres FOR LOOP
But there is a simpler way:
SELECT ...
FROM table1
ORDER BY array_column::numeric[];
Arrays values are sorted by element values left-to-right out of the box.
Requirements:
Column array_column must be an array with an element type that has a registered cast to numeric. Like text or varchar or some others.
Else, you may be able to use text[] as stepping stone: array_column::text[]:numeric[].
Each element can be projected to a legal numeric value.
Assume I have a query that returns a result set of columns A and B from table First_Table. I want to limit the result set to those columns if the value of column X in table Second_Table is 0, and I want to add column C from table First_Table if the value of column X is 1.
The problem is easily resolved using a Python for example whereby I just have a variable as an empty string if value in column X is 0 or it would be equal to the string 'First_Table.ColumnC AS [Dynamic Value],', and I just format the sql in the script accordingly.
If Else solution is not an elegant way because I have multiple columns to add dynamically depending on multiple values...
I am just looking for some ideas on directions.. I have been looking at this for a while, might be bogged up
Dynamic sql is the best way to resolve this as suggested in the comments.
the following function deletes all blanks in a text or varchar column and returns the modified text/varchar as an int:
select condense_and_change_to_int(number_as_text_column) from mytable;
This exact query does work.
Though my goal is to apply this function to all rows of a column in order to consistently change its values. How would I do this? Is it possible with the UPDATE-clause, or do i need to do this within a function itself? I tried the following:
UPDATE mytable
SET column_to_be_modiefied = condense_and_change_to_int(column_to_be_modiefied);
Basically i wanted to input the value of the current row, modify it and save it to the column permanantly.
I'd welcome all ideas regarding how to solve scenarios like these. I'm working with postgresql (but welcome also more general solutions).
Is it possible with an update? Well, yes and sort-of.
From your description, the input to the function is a string of some sort. The output is a number. In general, numbers should be assigned to columns with a number type. The assumption is that the column in question is a number.
However, your update should work. The result will be a string representation of the number.
After running the update, you can change the column type, with something like:
alter table mytable alter column column_to_be_modiefied int;
I have a column where I have values like:
Email_Password+oi8hu907b;New_eMail+Y;Email_Username+iugbhijhb8
Now I want to update New_eMail attribute for all rows which has Y to N without affecting anything else.
Please advise.
i hate it but...
update table
set column = replace(column,'New_eMail+Y','New_eMail+N')
where column like '%New_eMail+Y%'
you don't need the WHERE clause but if you put a functional index on the table it may be quicker with it
Since it may be the only place in the string where '+Y;' occurs the following statement may do the trick:
update <your_table>
set <your_column> = replace(<your_column>,'+Y;','+N;')
where instr(<your_column>,'+Y;')>0
This solution differs from the others provided because it does not depend on the value of the email address.
My answer is a slight improvement over the answer from user davegreen100
Since they don't allow me to post it as a comment, I add it here.
update <<tablename>>
set <<columnname>> = replace(<<columnname>>,';New_eMail+Y;',';New_eMail+N;')
where <<columnname>> like '%;New_eMail+Y;%'
I have a table validationmaster with a column called vrformula. It contains a formula like:
pf > 1
In that pf is one of the column names in the datasource table. I have to check whether pf of all the entries in the datasourse table is > 1 or not, but I don't know how to make it work.
I can fetch that formula correctly but Sql Server considers that formula as a string, I don't know how to change that whole expression into a formula.
For example: select * from datasource where meterid=4716 and pf>=1 is the statement I want to execute, with that formula at the end of the where clause being generated from the vrformula column.
You'll have to use Dynamic SQL.
This is probably obvious, nevertheless, a quick and dirty solution is to execute a SELECT for retrieving the column value, then executing a second SELECT containing the condition.
Here I assume that you are executing the SQL statements from a code written in a programming language (not SQL).