I am migrating from mssql to postgresql
I am trying to assign a value to a temporary variable in select query
Below is the query in mssql
select flag = 0
It gives 0 as output wit flag as column identifier in mssql
I tried the following in postgresql
select flag := 0
select flag [:=0]
It says syntax error at or near :=
Does anybody know where I am going wrong?
It gives 0 as output wit flag as column identifier in mssql
Postgres honors the SQL standard and a column alias is defined with the AS keyword there.
So to get a column named flag with the value zero use the following:
select 0 as flag
flag = 0 in standard SQL (and Postgres) is a boolean expression that compares the the column flag with the value zero and returns either true, false or null depending on the column's value.
Related
I am trying to use jsonb_set to change a single attribute of a jsonb object within my Postgres table.
I am using WITH random_string AS to set the variable random_string to a random hexidecimal value and then pass that string into my UPDATE query. However, it is not working.
This is the code I am using:
WITH random_string AS (SELECT substr(md5(random()::text), 0, 25)::varchar)
UPDATE
teams
SET
profile = jsonb_set(profile, '{api_key}', random_string)
WHERE
team_id="abc123";
The error I get seems to think I am trying to access a column that does not exist, because this is how you would normally reference a column.
Postgres query failed, PostgresPlugin query failed to execute: error: column "random_string" does not exist
Question: How do I use my random_string variable in the jsonb_set function to update this attribute?
Three issues. First, WITH statement gives a table-like result, not a variable. Define a table with a single column and use its name in the FROM clause in UPDATE. Next, the third argument of jsonb_set() is jsonb, use to_jsonb(). And last, a proper text literal is in single quotes 'abc123'.
WITH var(random_string) AS (SELECT substr(md5(random()::text), 0, 25)::varchar)
UPDATE
teams
SET
profile = jsonb_set(profile, '{api_key}', to_jsonb(random_string))
FROM
var
WHERE
team_id = 'abc123';
I am using Postgresql together with HikariCP and my query is something like
SELECT * FROM my_table WHERE int_val = ? ...
Now, I would like to set NULL value to my variables - I have tried
ps.setNull(1, Types.INTEGER); // ps is instance of PreparedStatement
try (ResultSet rs = ps.executeQuery()) {
... // get result from resultset
}
Although I have rows matching the conditions ( NULL in column 'int_val'), I have not received any records..
The problem is (I think) in query produced by the Statement, looks like:
System.out.println(ps.toString());
// --> SELECT * FROM my_table WHERE int_val = NULL ...
But the query should look like:
"SELECT * FROM my_table WHERE int_val IS NULL ..." - this query works
I need to use dynamically create PreparedStatements which will contain NULL values, so I cannot somehow easily bypass this.
I have tried creating connection without the HikariCP with the same result, so I thing the problem is in the postgresql driver? Or am I doing something wrong?
UPDATE:
Based on answer from #Vao Tsun I have set transform_null_equals = on in postgresql.conf , which started changing val = null --> val is null in 'simple' Statements, but NOT in PreparedStatements..
To summarize:
try (ResultSet rs = st.executeQuery(SELECT * FROM my_table WHERE int_val = NULL)){
// query is replaced to '.. int_val IS NULL ..' and gets correct result
}
ps.setNull(1, Types.INTEGER);
try (ResultSet rs = ps.executeQuery()) {
// Does not get replaced and does not get any result
}
I am using JVM version 1.8.0_121, the latest postgres driver (42.1.4), but I have also tried older driver (9.4.1212). Database version -- PostgreSQL 9.6.2, compiled by Visual C++ build 1800, 64-bit.
It is meant behaviour that comparison x = null is equal to null (no matter what x is equal to). Basically for SQL NULL is unknown, not the actual value... To bypass it you can set transform_null_equals to on or true. Please checkout docs:
https://www.postgresql.org/docs/current/static/functions-comparison.html
Some applications might expect that expression = NULL returns true if
expression evaluates to the null value. It is highly recommended that
these applications be modified to comply with the SQL standard.
However, if that cannot be done the transform_null_equals
configuration variable is available. If it is enabled, PostgreSQL will
convert x = NULL clauses to x IS NULL.
I have just found a solution, which works the same for "values" and "NULLs" by using IS NOT DISTINCT FROM instead of =.
More on postgresql wiki
It is important to recognize that null is not a value with SQL. It is encoding the logical notion of "unknown". This is why null = var results in false always, even for cases where var has a value of null. So even if if you are replacing the value of your variable (aka ? in your case) with a value of null, the result be definition must not be what you do expect as long as SQL standard is complied with.
Now there are some databases around that try to outsmart SQL standard by assuming a column value of null should be taken as a programming language null (nil, undef or whatever is used for that purpose).
This creates some convenience for the unwary programmer, but in the long run causes grieve as soon as you need a true distinction between a SQL null and a programming language null.
Nevertheless, for ease of porting from such databases to PostgresQL (or simple for ease of lazy programming) you may resort to setting transform_null_equals.
BUT, you are using prepared statements. As such, prepared statements are converted to query plan once and such query plan needs to be valid for all potential values of the variables used in the prepared statement query. Now, a VAR is null is fundamentally different from a VAR = ?. So there is no chance for the query parser, query optimizer or even query execution engine to dynamically rewrite the (already prepared) query based on the actual parameter values passed in.
From this, you should take the recommendation serious that is given with the documentation of transform_null_equals and change your code to use VAR is null when a null value is to be searched for and a VAR = ? for other cases.
I have to change the value of a determined column in postgre but I never worked before with maskbits, (it's a legacy code). The current value is 1 but it's stored as a integer maskbit so if I try to do
update table set field=0 where ...
It doesn't work and it keeps returning me 1. So I read the documentation and tried to do a AND:
update table set field=field&0 where ...
But it doesn't worked too. Then I tried to cover all the 30 bits, without success too:
update table set field=field&0000000000000000000000000000000 where ...
Can someone please show me how to properly change the value of a integer maskbit on postgresql?
EDIT: I found this case here in StackOverflow
UPDATE users SET permission = permission & ~16
which seen's to be the closest to mine so I tried to do
UPDATE table SET field = field & ~1
because that's the only bit I have to deactivate but it still active and retuning 1 when I do a SELECT
PostgreSQL may be pickier than you're used to when it comes to combining numbers of different types. You can't just use a 0, you need to use a bitstring constant of the right length, or explicitly cast an integer to the proper bit(n) type.
Bitstring constants are entered like B'00101', which would have type bit(5).
Some examples you can type into psql:
select 5;
select (5::bit(5));
select (30::bit(5));
select ~(1::bit(5));
select ~B'00001';
select (B'00101' & B'11110');
select (B'00101' & ~(1::bit(5)));
Using DB2 10.5 on Windows x64
UPDATE dbo.datasource_databases
SET HOST = ''
WHERE ID = 1
Assignment of a NULL value to a NOT NULL column "TBSPACEID=2, TABLEID=6, COLNO=1" is not allowed.. SQLCODE=-407, SQLSTATE=23502, DRIVER=3.67.28
HOST is a VARCHAR(512) NOT NULL
Is this expected behavior? If so, how should I work around this and if not, what could be causing this?
Edit: Along the same lines, SELECT ID, HOST from dbo.datasource_databases WHERE HOST != '' returns 0 rows, where checking for HOST != 'some gibberish' will return rows. To me this makes even less sense than the above behavior (shouldn't it just treat it as HOST NOT NULL?).
That could only happen if Oracle compatibility or at least Oracle's VARCHAR2 data type compatibility is enabled for the database, as described here. Normally DB2, as prescribed by the ANSI SQL standard, treats empty strings as strings of zero length, not NULLs.
I am do replicating of two database (SQL Server 2000 and PostgreSQL). I use http://blog.hagander.net/archives/103-Replicating-from-MS-SQL-Server-to-PostgreSQL.html for this. Then I do last step the
ERROR: operator does not exist: character = integer; Error executing the query
appeared. I use the PostgreSQL 8.4.6 for that and ODBC drivers (all psqlodbc_08_04_0100.zip, psqlodbc_08_04_0200.zip) from here i also try to delete and install version that Synaptic called 9.0.2-1 and update odbc drivers i try (psqlodbc_09_00_0100.zip, psqlodbc_09_00_0101.zip, psqlodbc_09_00_0200.zip) it also return that error. The query launched from delphi where i use only System DSN runs normally
You need to fix your SQL statement.
I bet you have something like
WHERE character_column = 1
and you need to change that to
WHERE character_column = '1'
use single quote 'your_value' for non numeric data and double quote for column name and never the opposite.
select status, sum(amount) as sum from "sales" where ("date" <= '2017-04-30 23:59:59' and "customer_id" = 1) and "sales"."deleted_at" is null group by "status"