Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert all records in postgres to Titlecase, first letter uppercase

Tags:

postgresql

I have a simple table in PostgreSQL called keywords with a simple text field called name. I want to convert all names of keywords in first letter uppercase. Is there a way to do it from psql console?

like image 598
topless Avatar asked May 30 '11 12:05

topless


People also ask

How do I get the first letter in uppercase in SQL?

Use the INITCAP() function to convert a string to a new string that capitalizes the first letter of every word. All other letters will be lowercase. This function takes one parameter as a string and changes the capitalization for each word as described.

How do I make uppercase in PostgreSQL?

In PostgreSQL, the UPPER function is used to convert a string into upper case. Like the LOWER function, the UPPER function accepts a string expression or string-convertible expression and converts it to an upper case format.

Which option converts the first letter of every word to uppercase?

Or use Word's keyboard shortcut, Shift + F3 on Windows or fn + Shift + F3 for Mac, to change selected text between lowercase, UPPERCASE or capitalizing each word.

Which function converts the first letter of string in uppercase?

The toUpperCase() method converts the string to uppercase. Here, str.


4 Answers

There is an initcap() function, if you're meaning to uppercase the first letter of each keyword and to lowercase the following characters:

update foo set bar = initcap(bar) 

Else combine substring() and upper():

update foo set bar = upper(substring(bar from 1 for 1)) ||           substring(bar from 2 for length(bar)) 

http://www.postgresql.org/docs/current/static/functions-string.html

like image 74
Denis de Bernardy Avatar answered Sep 21 '22 01:09

Denis de Bernardy


select initcap('r. lópez viña tondonia Rioja White Viña'); 

This does give the correct answer (R. López Viña Tondonia Rioja White Viña) in our version of Postgres (9.0.7).

like image 23
Scott Pritchett Avatar answered Sep 22 '22 01:09

Scott Pritchett


@denis, Gave the Right Answer!

But in my case I use PgAdmin3 , so after selecting the database there is SQL query Options , So there we can directly add the above query in it.

I had a table called subcategory_subcategory(name of table) in that i wanted to change a column values whose name was Item_name(name of column ) , so my query was like this

update subcategory_subcategory
set Item_name = initcap(Item_name)
like image 36
MD Shahrouq Avatar answered Sep 21 '22 01:09

MD Shahrouq


The initcap function capitalizes letters after special characters (dashes, apostrophes, etc). I only want to capitalize after a space.

Similar to Denis' answer, this function will convert the first letter of every word (separated by a space)

CREATE OR REPLACE FUNCTION titlecase(instr varchar) RETURNS varchar AS $$
DECLARE
  strarray varchar[] := string_to_array(inStr, ' ');
  outstrarray varchar[];
  word varchar;
BEGIN
  FOREACH word IN ARRAY strarray
  LOOP
    outstrarray := array_append(outstrarray, (upper(left(word,1))::varchar || 
lower(right(word,-1))::varchar)::varchar);
  END LOOP;
  RETURN array_to_string(outstrarray,' ','');
END;
$$ LANGUAGE 'plpgsql';
like image 31
Garret K Avatar answered Sep 22 '22 01:09

Garret K