What is the best way to segregate the data alphabetically using SQL?
What is the be开发者_开发知识库st way to segregate the data alphabetically using SQL?
I would like to divide the data into 3 parts based on starting character of data in some columns??
You can use REGEX_LIKE statement to get the result.
Example:
-- For Range [A--G]
SELECT target_col
FROM target_table
WHERE REGEXP_LIKE( target_col, '^[A-G].$' ) ;
-- For Range [H--O]
SELECT target_col
FROM target_table
WHERE REGEXP_LIKE( target_col, '^[H-O].$' ) ;
-- For Range [P--Z]
SELECT target_col
FROM target_table
WHERE REGEXP_LIKE( target_col, '^[P-Z].$' ) ;
-- For Range [A--G]
SELECT target_col
FROM target_table
WHERE target_col >= 'A'
AND target_col < 'H'
If there is a simple index on target_col
, the query will probably use it.
Also you can use this:
select *
from your_table
where your_column between 'A' and 'G';
精彩评论