Skip to content
Advertisement

How to use select statements for column name ending in +

I am attempting to run a spearman correlation on some data under the column name solar+. However, when attempting to use select statements to select the desired number of rows from the egauge13830 table, the plus sign is considered an operator and causes an error. These are the 2 statements I’ve tried and their results:

1)

sql1 = """    
SELECT solar+    
FROM egauge13830    
WHERE tstamp < 1434870960    
"""

sqlalchemy.exc.ProgrammingError: (psycopg2.errors.SyntaxError) syntax error at or near “FROM”

LINE 3: FROM egauge13830

      ^

2)

sql1 = """    
SELECT 'solar+'    
FROM egauge13830    
WHERE tstamp < 1434870960    
"""

df1 = pd.read_sql_query(sql1, engine)    
print(df1.head())

0   solar+    
1   solar+    
2   solar+    
3   solar+    
4   solar+

I have also attempted changing the name of this column, but can’t find a way to do so which doesn’t involve select statements, the use of which causes the exact same problems illustrated above.

So, is there something I can add to the select statement to make it run or another way of changing the column name?

Thanks for any and all help.

Advertisement

Answer

Just realized after Gordon’s comment that its double quotes in postgres and square brackets in sql server. ‘solar+’ would treat it as a plain string instead of column name and that is why it is printing the string ‘solar+’

sql1 = """

       SELECT "solar+"

       FROM egauge13830

       WHERE tstamp < 1434870960

       """

Ideally the name of the column is not standard so you can change the name of the column given you have permissions.

ALTER TABLE egauge13830
RENAME COLUMN "solar+" TO "solar";
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement