David Bijoyan
David Bijoyan

Reputation: 117

How to compose query properly using dbGetQuery command?

I have a small database in PostgresSQL. And I connected to it via R-studio and try to retrieve data from database.

I wrote a command, but it doesn't work. And I don't know what's the problem. Please, help me.

So, the code works well.

data1 <- dbGetQuery(con, "select  a.date from bank_schema.ratios_135 a")

But these two codes fail

data1 <- dbGetQuery(con, "select a.'n1.2' from bank_schema.ratios_135 a")
data1 <- dbGetQuery(con, "select a.REGN from bank_schema.ratios_135 a")

Where con is the postgres connection object.

And of course query with any combination of these vars gives an error.

I would be thankful to all of you if you help me.

Upvotes: 0

Views: 206

Answers (1)

sempervent
sempervent

Reputation: 893

You need to properly quote the odd names:

data1 <- dbGetQuery(con, 'select a."n1.2" from bank_schema.ratios_135 a')
data2 <- dbGetQuery(con, 'select a."REGN" from bank_schema.ratios_135 a')

Single quotes in PostgreSQL are used to create a text string, double quotes are used to name an identifier without changing its case.

Upvotes: 1

Related Questions