Updating a PostgreSQL column that contains dot (.) in its name - sql

I should update the value of a row, but the column name has the dot.
I tried name.name but nothing, even though it seems to work on MySQL.
How can I do with postgresql? I swear that before creating this thread I searched all over.
Thanks
UPDATE:
Thanks for the quick answers, I tried to use "" but this is the result
ERROR: column "name.name" of relation "my_table" does not exist
My query:
update my_table set "name.name"='a081613e-2e28-4cae-9ff7-4eaa9c918352';

You can use "" around the column name

Wrap name with double quotation marks: "name.name"
UPD:
UPDATE: Thanks for the quick answers, I tried to use "" but this is the result
Are you sure then that it's your case?
psql (13.2)
Type "help" for help.
postgres=# CREATE DATABASE example_db;
CREATE DATABASE
postgres=# \c example_db
You are now connected to database "example_db" as user "postgres".
example_db=# CREATE TABLE example_table ("example.field" int);
CREATE TABLE
example_db=# \d example_table
Table "public.example_table"
Column | Type | Collation | Nullable | Default
---------------+---------+-----------+----------+---------
example.field | integer | | |
example_db=# SELECT "example.field" FROM example_table;
example.field
---------------
(0 rows)
example_db=# SELECT "example_table"."example.field" FROM example_table;
example.field
---------------
(0 rows)
example_db=#

Related

How to call a column named "group" in Snowflake?

I have a table in Snowflake with the following structure:
| id | group | subgroup |
_________________________
| 1 | verst | burg |
| 2 | travel| plane |
| 3 | rest | bet |
I need to call only the column "group", so I tried the following code:
select t2.group
from table as t2
but the following error arises
SQL compilation error: syntax error line 1 at position 7 unexpected 'group'. syntax error line 2 at position 0 unexpected 'from'.
I have also tried using:
select group
from table as t2
select "group"
from table as t2
but I always get the same error.
I know I can call the whole table using * but the real table where I get this data from has many more columns and we want to display this data in a dashboard. Additionally, I am not the owner of the table since it is filled by a microservice, so I cannot change the column names and I can't modify the microservice process.
I would appreciate any suggestion.
Given the table could not be created without double quotes, you need to know how it was created to know how to refer to the column. Which is to say it the create code was CREATE TABLE awsome ("GrOuP" string); there you will need to type "GrOuP"
There is also a session setting to ignore case in double quotes that might help.
see QUOTED_IDENTIFIERS_IGNORE_CASE
But by default things are upper case, thus try "GROUP"
Putting group in double quotes worked fine when I tried it:
create or replace temporary table foo ( "group" string );
insert into foo values ('Hello world.');
select "group" from foo;

PostgreSQL query -- column does not exist

I am trying to update a table using a temporary table.
Schema | Name | Type | Owner
------------+----------------------+----------+----------
pg_temp_11 | tmp_x | table | postgres
public | entities | table | postgres
However I am getting this error:
UPDATE entities SET "Name" = "tmp_x.Name" FROM tmp_x WHERE "entities.Ent_ID" = "tmp_x.Ent_ID";
ERROR: column "tmp_x.Name" does not exist -- the column Name exists
LINE 1: UPDATE entities SET "Name" = "tmp_x.Name" FROM tmp_x WHERE "...
What is the problem? The quotes around table columns?
You are surrounding multiple individual objects with double quotes. If you are using object delimiters (double quotes), they need to be on each item, not on the entire combination:
UPDATE entities SET "Name" = "tmp_x"."Name" FROM tmp_x WHERE "entities"."Ent_ID" = "tmp_x"."Ent_ID";

Changing a column type from integer to string

Using PostgreSQL, what's the command to migrate an integer column type to a string column type?
Obviously I'd like to preserve the data, by converting the old integer data to strings.
You can convert from INTEGER to CHARACTER VARYING out-of-the-box, all you need is ALTER TABLE query chaning column type:
SQL Fiddle
PostgreSQL 9.3 Schema Setup:
CREATE TABLE tbl (col INT);
INSERT INTO tbl VALUES (1), (10), (100);
ALTER TABLE tbl ALTER COLUMN col TYPE CHARACTER VARYING(10);
Query 1:
SELECT col, pg_typeof(col) FROM tbl
Results:
| col | pg_typeof |
|-----|-------------------|
| 1 | character varying |
| 10 | character varying |
| 100 | character varying |
I suggest a four step process:
Create a new string column. name it temp for now. See http://www.postgresql.org/docs/9.3/static/ddl-alter.html for details
Set the string column. something like update myTable set temp=cast(intColumn as text) see http://www.postgresql.org/docs/9.3/static/functions-formatting.html for more interesting number->string conversions
Make sure everything in temp looks the way you want it.
Remove your old integer column. Once again, see http://www.postgresql.org/docs/9.3/static/ddl-alter.html for details
Rename temp to the old column name. Again: http://www.postgresql.org/docs/9.3/static/ddl-alter.html
This assumes you can perform the operation while no clients are connected; offline. If you need to make this (drastic) change in an online table, take a look at setting up a new table with triggers for live updates, then swap to the new table in an atomic operation. see ALTER TABLE without locking the table?

SQLite table with some rows missing a column

I have a table in a SQLite database that looks something like this, but with more columns and rows:
| Field1 | Field2 |
|---------|---------|
| A | 1 |
| B | 2 |
| C | |
What I need to do is run a SQL query like this:
SELECT * FROM <tablename> WHERE <conditions> ORDER BY Field2
The problem is, I'm getting the error: no such column: Field2
So now I've been asked to set all the missing values to 99. But when I run
UPDATE <tablename> SET Field2='99' WHERE Field2 IS NULL;
I get the same error. How do I fix this and update all those missing cells?
EDIT: I should also add that the missing values don't seem to be null since if I add a new column in my database GUI browser, all the cells show as [NULL], though this column doesn't.
This turned out to be caused by a very subtle problem in the table:
Several of the column names (the ones that were causing me problems) ended in a newline (\n). Removing the newline solved all my problems!

PostgreSQL Error while describe or any operation on imported data

I have imported a SQL dump into a database named Test and now when I try to describe the table it give me an error as the relation does not exits. But when I do the same operation on information_schema.columns I get the results. Also modification or select operation is getting failed with same error.
# \connect Test
Password for user postgres:
psql (8.4.18)
You are now connected to database "Test".
Test=# \d
List of relations
Schema | Name | Type | Owner
--------+-----------------------+----------+----------
. . .
public | TESTEMP | table | postgres
. . .
Test-# \d+ TestEmp;
Did not find any relation named "Test".
Test=# select column_name from INFORMATION_SCHEMA.COLUMNS where table_name = 'TestEmp';
column_name
--------------------
lastname
firstname
Test=# alter table TestEmp Add column "PerEmp" BOOLEAN DEFAULT FALSE;
ERROR: relation "testemp" does not exist
Test=# select * from TestEmp;
ERROR: relation "testemp" does not exist
LINE 1: select * from testemp;
^
MediaService=#
Any thoughts why these errors are being reported.
Solved. Seems the tables are created using quoted strings due to which it become case sensitive. So one I did below it describe the tables without any errors.
#\d+ "TESTEMP"
Table "public.TESTEMP"
Column | Type | Modifiers
--------------------+-----------------------------+-----------
. . .
Just keeping this question for future.