How to count the number of commas (',') in a SQL table column? - sql

I have a table in Teradata. This table has a column named text_column that contains some text in format varchar(25).
I need to count the number of ',' in this column.

One method uses the difference of lengths of strings:
select ( character_length(text_column) - character_length(oreplace(text_column, ',', '')) as num_columns

Related

Oracle DB, concat all values from 1 column using "," separator [duplicate]

This question already has answers here:
SQL Query to concatenate column values from multiple rows in Oracle
(10 answers)
Closed 3 years ago.
I have Oracle DB table with 1 column UUID
How can I concatenate all the UUID values in one string separated by "," so the output will be like
"id1","id2","id3","id4"....
You can use listagg():
select listagg(uuid, ',') within group(order by uuid) uuids
from mytable
This will give you a single record with a single column containing all uuids, separated with a comma.
Note that listagg() returns a varchar, hence the output cannot be more than 4000 bytes.
Also note that:
the comma is the default separator, so in your case it is not strictly necessary to pass a second argument to the function
I added an order by clause in order to generate a deterministic result: this option is not mandatory, so if the ordering of items in the list does not matter to you then you can leave it apart (in which case you will get the uuids in an undefined order)
You can use listagg() -- assuming there are not too many values. The maximum string size is 4000 bytes.
selest '"' || listagg(uuid, '","') within group (order by uuid) || '"'

sql query to retrieve numeric and alphanumeric entries

Hi can anyone help with a quire to retrieve both numeric and alpha numeric entries from a column.
Example: Column is 'ID' and entries could be = abc0000 or could be all 9999999999.
The search is a list of numeric and alphanumeric items.
Example: 8765398
ha34789
ss0487667
7629
You could do:
SELECT * FROM table WHERE ID REGEXP '^[A-Za-z0-9]+$'
You could also use a named character:
SELECT * FROM table WHERE ID REGEXP '^[[:alnum:]]+$'

SQL Select column as substring at first occurrence of characters?

I am using Postgresql and I need to run a query where I do a SELECT DISTINCT on a single string column. However I don't want to select the column as is, I need to substring it on the first occurrence of this string ' ('.
I do not know how to do this substring part..
Here is an example of the query without the substring part:
SELECT DISTINCT ON (Table.Column1) Table.Column2
FROM Table
ORDER BY Table.Column1
I am not sure what functions to use in postgres or possibly I need to use plpgsql to do this?
I managed to work this out. The function to be used is SPLIT_PART. Which takes three parameters ColumnName, Characters, And the substring occurance.
Here is an example of how I have used it.
SELECT DISTINCT ON (SPLIT_PART(Table.Column1, ' )', 1)) Table.Column2
FROM Table
ORDER BY SPLIT_PART(Table.Column1, ' )', 1)

Select number of comma delimited list items

I have a column that has comma seperated values and I need to select all rows that have 13 commas. They seperate numbers so I don't need to worry about any strings that contain commas. How would I do this?
alternative to like (I do not like the like, and the above will fail if contains 14 commas or more)
select * from table
where length(replace(your_column, ',', ''))=length(your_column)-13;
for better utilize the index, you should seek to normalize your table
If you're using PostgreSQL, you could also use regular expressions.
However, a better question might be why you have a single column with comma-separated values instead of multiple columns.
If you count a string with 14 commas as having 13 commas, then this will work:
SELECT * FROM table WHERE column LIKE '%,%,%,%,%,%,%,%,%,%,%,%,%,%'
% matches any string (including zero length).
In PostgreSQL you can do:
select col from table where length(regexp_replace(col, '[^,]', '', 'g')) = 13;

How to select rows where column contained a string

I want to find rows where a column contains a particular string.
I have a table that contains a column which contains values separated by commas (,).
How can I find the rows that contain a certain value?
This should give you what you want:
SELECT * FROM yourtable
WHERE yourcolumn LIKE 'value,%'
OR yourcolumn LIKE '%,value,%'
OR yourcolumn LIKE '%,value'