Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The RIGHT() function in PostgreSQL gives an error

Tags:

postgresql

I'm trying to use the RIGHT() function it so it will only display the last 4 digits of a credit card number pulled from the customer table. This is what I have so far:

create function get_customer(text) returns setof cusinfo as

$$

select upper(first_name)||' '||upper(last_name) as full_name, upper(address), upper(city)||', '||upper(state)||' '||zip as citystatezip, email, '************'||right(cc_number,4), cc_name

from customer

where customer_id = $1;

$$ language sql;

The error I am being given is:

psql:finalproject.sql:273: ERROR: function right(text, integer) does not exist LINE 3: ...|' '||zip as citystatezip, email, '****'||right(cc_n...

Any ideas as to why this is happening? I tried only using RIGHT() by itself and putting in something like RIGHT('Help me', 2), but I get the same error.

like image 945
Big Charlie Avatar asked May 15 '12 01:05

Big Charlie


People also ask

What is error in PostgreSQL?

All messages emitted by the PostgreSQL server are assigned five-character error codes that follow the SQL standard's conventions for “SQLSTATE” codes. Applications that need to know which error condition has occurred should usually test the error code, rather than looking at the textual error message.

What does %s mean in PostgreSQL?

Using parameter notation here ( %s ) will cause the inserted name value to be the provided string ( "my_name'); DROP TABLE my_table;" ), rather than allowing that string to execute arbitrary SQL in the database.

What does := mean in PostgreSQL?

:= is the assignment operator in PL/pgSQL. The expression searchsql:= searchsql || ' WHERE 1=1 ' ; appends the string ' WHERE 1=1 ' to the current value of the variable searchsql. See the manual for details: http://www.postgresql.org/docs/current/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-ASSIGNMENT.


1 Answers

I'm assuming psql is PostgreSQL. If that's the case, you should read the PostgreSQL documentation describing the string functions that are available to you.

right is not one of them.

Try substring(cc_number from char_length(cc_number) - 3).

In future you may want to use Google to help answer questions like this. Google is a search engine; you can use search engines to find documentation; documentation tells you how to use a product.

like image 140
ta.speot.is Avatar answered Oct 14 '22 19:10

ta.speot.is