Sql error 42703 ошибка столбец не существует

I am trying to do a cohort analysis and compare average number of rentals based on the renter’s first rental year(= the year where a renter rented first time). Basically, I am asking the question: are we retaining renters whose first year renting was 2013 than renters whose first year was 2015?

Here is my code:

SELECT renter_id, 
       Min(Date_part('year', created_at)) AS first_rental_year, 
       ( Count(trip_finish) )             AS number_of_trips 
FROM   bookings 
WHERE  state IN ( 'approved', 'aboard', 'ashore', 'concluded', 'disputed' ) 
  AND  first_rental_year = 2013 
GROUP  BY 1 
ORDER  BY 1; 

The error message I get is:

ERROR:  column "first_rental_year" does not exist
LINE 6: ... 'aboard', 'ashore', 'concluded', 'disputed') AND first_rent...
                                                             ^

********** Error **********

ERROR: column "first_rental_year" does not exist
SQL state: 42703
Character: 208

Any help is much appreciated.

Wondering how to fix PostgreSQL Error code 42703? We can help you.

One of the most common error codes with the PostgreSQL database is 42703. It will be seen along with the error message “column does not exist”. This error indicates either that the requested column does not exist, or that the query is not correct.

Here at Bobcares, we often handle requests from our customers to fix similar PostgreSQL errors as a part of our Server Management Services. Today we will see how our support engineers fix this for our customers.

How to fix PostgreSQL Error code 42703

Often, the error is caused by a lack of quotes. We can add double quotes to the column name to fix this error.

For example:

We will try to run a simple select query:

SELECT return_part_i.CntrctTrmntnInd FROM return_part_i LIMIT 10;

And get the following error:

ERROR: column return_part_i.cntrcttrmntnind does not exist LINE 1: SELECT return_part_i.CntrctTrmntnInd FROM return_part_i LIMI... ^ HINT: Perhaps you meant to reference the column "return_part_i.CntrctTrmntnInd". SQL state: 42703 Character: 8

if we have a camel case in our column name we must ensure to wrap the column name with a double quote.

This can be done in the following way:

SELECT "CntrctTrmntnInd"  FROM return_part_i LIMIT 10;

PostgreSQL columns (object) names are case sensitive when specified with double quotes. Unquoted identifiers are automatically used as lowercase so the correct case sequence must be written with double quotes.

If we want a LIMIT in result we must use an order by

SELECT "CntrctTrmntnInd" FROM return_part_i ORDER BY "CntrctTrmntnInd" LIMIT 10;

When used with quotes, Postgresql is case sensitive regarding identifier names like table names and column names.
So a common issue that triggers this error is when we use the column name in our commands in any other cases other than that of the original one.

For instance, if the column name is “Price”, using “price” in the command can trigger the error.

Thus we need to make sure that the cases are correct.

[Need assistance? We can help you]

Conclusion

In short, we saw how our Support Techs fix PostgreSQL Error code 42703 for our customers.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = «owonCMyG5nEQ0aD71QM»;

PostgreSQL database code 42703 is a common error encountered and triggers an error message «column does not exist«. Basically, this error indicates either that the requested column does not exist, or that the query is not correct.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related  PostgreSQL queries.

In this context, we shall look into how to fix this PostgreSQL error.

How to resolve PostgreSQL Error code 42703 ?

Sometimes this error is a result of lack of quotes. We can add double quotes to the column name to fix this error.

For example:

We will try to run a simple select query:

SELECT return_part_i.CntrctTrmntnInd FROM return_part_i LIMIT 10;

And get the following error:

ERROR: column return_part_i.cntrcttrmntnind does not exist LINE 1: SELECT return_part_i.CntrctTrmntnInd FROM return_part_i LIMI... ^ HINT: Perhaps you meant to reference the column "return_part_i.CntrctTrmntnInd". SQL state: 42703 Character: 8

if we have a camel case in our column name we must ensure to wrap the column name with a double quote.

This can be done in the following way:

SELECT "CntrctTrmntnInd"  FROM return_part_i LIMIT 10;

PostgreSQL columns (object) names are case sensitive when specified with double quotes. Unquoted identifiers are automatically used as lowercase so the correct case sequence must be written with double quotes.

If we want a LIMIT in result we must use an order by:

SELECT "CntrctTrmntnInd" FROM return_part_i ORDER BY "CntrctTrmntnInd" LIMIT 10;

When used with quotes, Postgresql is case sensitive regarding identifier names like table names and column names.

So a common issue that triggers this error is when we use the column name in our commands in any other cases other than that of the original one.

For instance, if the column name is «Price», using «price» in the command can trigger the error.

Thus we need to make sure that the cases are correct.

[Need assistance in fixing PostgreSQL Errors? We can help you. ]

Asked

Viewed
37k times

I am getting an error while running an update query with table name specified along with column name:

UPDATE Temp SET Temp.Id='234',Temp.Name='Test'WHERE Id='245'

This is the error:

ERROR:  column "temp" of relation "temp" does not exist
LINE 1:      UPDATE Temp SET Temp.Id='23...
                               ^
********** Error **********

ERROR: column "temp" of relation "temp" does not exist
SQL state: 42703
Character: 24

dezso's user avatar

dezso

30.7k13 gold badges98 silver badges144 bronze badges

asked Aug 3, 2016 at 9:02

You cannot (and need not) use table aliases (or tablename qualified column names) in the SET clause of an UPDATE. This even makes sense, as you can only update a single table in a single UPDATE, so there is no ambiguity in column names there.

Fortunately, the ever helpful documentation explicitly mentions your case:

column_name

The name of a column in the table named by table_name. The column name can be qualified with a subfield name or array subscript,
if needed. Do not include the table’s name in the specification of a
target column — for example, UPDATE tab SET tab.col = 1 is invalid.

So, the solution is to simply remove temp. from the SET clause:

UPDATE temp SET id = '234', name = 'Test' WHERE id = '245'

Notes:

  • Are you really storing numbers as text? If yes, why? It is usually a recipe for disaster. For example, how do you prevent something like 'mkjcvnd7y78r3tgbhvcjh' entering your id column?
  • The way you are using object names starting with capital letters is confusing. Without double-quoting its name, your table in reality is called temp as opposed to Temp. Using it the latter way may decrease readability (depending on your preferences and habits, of course).

answered Aug 3, 2016 at 9:13

dezso's user avatar

dezsodezso

30.7k13 gold badges98 silver badges144 bronze badges

What would happen in the case of doing an update for 2 tables that have the same field name?

update car c, airplane a
set c.nr_rute = 1
set a.nr_rute = 1

answered Dec 8, 2021 at 17:52

LUIS FERNANDO GUERRERO ORTEGA's user avatar

2

I tried to create exactly the same but new table from old table in another database using dblink. This procedure used to worked last two times, but this time I got message:
«SQL state: 42703
Context: Error occurred on dblink connection named «unnamed»: could not execute query.»

Anyone knows where is the problem or how to solve it?
Please!

  • postgresql

asked Jun 8, 2010 at 12:32

Z77's user avatar

Z77Z77

1,1076 gold badges19 silver badges30 bronze badges

2 Answers

I encountered this problem and fixed it by using single quotes, rather than double quotes, for the string I was searching for:

select * from table where column = 'value';

answered Oct 7, 2013 at 23:00

Ashwin Balamohan's user avatar

Ashwin BalamohanAshwin Balamohan

3,3132 gold badges25 silver badges47 bronze badges

answered Jun 8, 2010 at 12:38

Tom Gullen's user avatar

Tom GullenTom Gullen

61.3k84 gold badges283 silver badges456 bronze badges

3

  • And by the way, I tried exactly the same query just with new name of new table, and the result is still the same as I mentioned above…Maybe a bug?!?!?

    Jun 8, 2010 at 13:29

  • Unlikely to be a bug, if you paste the actual SQL query into here that you are trying to execute it would be more helpful

    Jun 8, 2010 at 13:38

  • The above link seems unlikely to be relevant. Its for DB2 right? But the question is about postgres.

    Oct 27, 2013 at 10:10

Понравилась статья? Поделить с друзьями:
  • Sql error 42501 ошибка нет доступа к таблице
  • Sql error 22p02 ошибка ошибочный литерал массива
  • Spn 97 fm1 3 код ошибки камаз
  • Spn 806 fmi 5 камаз код ошибки
  • Spn 792 fmi 5 cummins камаз ошибка