Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

postgres query count characters and order by longest length

Tags:

postgresql

I'm using the following query to print all title from stories table

select title from stories;

which show me all titles , The point here how to tweak it and do the following :

`order by longest title or even count longest title` 

any tips

like image 336
LeoSam Avatar asked Apr 02 '14 07:04

LeoSam


People also ask

How do I count characters in PostgreSQL?

The PostgreSQL char_length function or character_length function is used to count the number of characters in a specified string. The CHARACTER_LENGTH() function is similar to CHAR_LENGTH() function. A string whose length is to be retrieved.

What is length function in PostgreSQL?

The PostgreSQL length() function is used to find the length of a string i.e. number of characters in the given string. Syntax: length(<string>) PostgreSQL Version: 9.3.

How do I get last 24 hours record in PostgreSQL?

Get rows from past 24 hours in PostgreSQL In the above SQL query, we use PostgreSQL system function now() to get current datetime. Then we use INTERVAL clause to select those rows where order_date falls within past 24 hours of present datetime. You can also specify time interval in days, instead of hours.


1 Answers

If you to order by the length of the title (longest to shortest) you should use the following query

select title from stories order by length(title) desc
like image 171
user sflskjdf Avatar answered Oct 07 '22 20:10

user sflskjdf