Pull specific numbers from a column that has numbers and words - sql

Column
1
7
f
3
2
c
1
d
6
4
e
g
b
I want to be able to filter this using the IN() operator in the where clause and pull out only the numbers. The column is a varchar so it is coming back as an error in postgres

select substring(colname FROM '[0-9]+') from tablename

You can filter the numbers using the ISNUMERIC() function on the WHERE Clausule.
Something like this:
SELECT *
FROM Table1
WHERE ISNUMERIC(column_name)=1
As mentioned on the comments, this is for SQL Server, but you can create your own ISNUMERIC function in PostgreSQL following this example:
isnumeric() with PostgreSQL

I ended up subquerying with this in the SELECT --- cast(substring(column FROM '[0-9]+') as int) and this in the WHERE column ~ '^\d+$' in the FROM as its own table. Pulling just the integers i needed from that with IN (1,2,3)

Related

How to bring rounded integer as output by multiplying two columns in SQL

I have two columns A and B in SQL. Column A has value 5.2 and Column B has value 2.1. I am multiplying column A and B in Column C. Answer is 10.92. I would like to get rounded number 11 as Integer in column C instead of 10.92 as float or decimal. How should I write query. Should there be a Cast function or similar?
Sample query is:
SELECT *, A multiply B As C
FROM Table X
You need query like this:
select *, cast(round((a*b), 0) as int) from X
Here you can see on DB FIDDLE
select round((columnA*columnB),0)
from Table

Use SQL regex (LIKE) in SELECT statement

I'm using SQL in pgadmin4 (Postgres).
I query a list of IP's and occurences of the IP's. If it matches the regex I want to print the regex and the sum of occurences.
I'm querying the following:
select distinct dstaddress, _col1 from test where _col1 > 1 AND
dstaddress LIKE '10.228.55.%' OR
dstaddress LIKE '10.228.9.%'
group by dstaddress, _col1
And this gives me as output the IP and its occurences:
"10.228.55.17" 365942
"10.228.9.104" 8
"10.228.9.105" 4
"10.228.9.106" 2
"10.228.9.107" 8
"10.228.9.108" 10
"10.228.9.109" 434
"10.228.9.110" 127
But as output I want to have the regex and the sum of occurences:
"10.228.55.%" 365942
"10.228.9.%" 593
How can I achieve this?
Here is one method:
select v.pat, sum(t._col1)
from (values ('10.228.55.%'), ('10.228.9.%')) v(pat) left join
test t
on t._col1 > 1 and
t.dstaddress like v.pat
group by v.pat;
Note: "occurrences" appears to be _col1. You would, of course, use whatever the appropriate column is, if that is not the case.
This puts the patterns in the derived table. They can then be used for the aggregation, without having to repeat them (a source of errors in the query).

sql how to convert multi select field to rows with totals

I have a table that has a field where the contents are a concatenated list of selections from a multi-select form. I would like to convert the data in this field into in another table where each row has the text of the selection and a count the number of times this selection was made.
eg.
Original table:
id selections
1 A;B
2 B;D
3 A;B;D
4 C
I would like to get the following out:
selection count
A 2
B 3
C 1
D 2
I could easily do this with split and maps in javascript etc, but not sure how to approach it in SQL. (I use Postgresql) The goal is to use the second table to plot a graph in Google Data Studio.
A much simpler solution:
select regexp_split_to_table(selections, ';'), count(*)
from test_table
group by 1
order by 1;
You can use a lateral join and handy set-returning function regexp_split_to_table() to unnest the strings to rows, then aggregate and count:
select x.selection, count(*) cnt
from mytable t
cross join lateral regexp_split_to_table(t.selections, ';') x(selection)
group by x.selection

How can I "dynamically" split a varchar column by specific characters?

I have a column that stores 2 values. Example below:
| Column 1 |
|some title1 =ExtractThis ; Source Title12 = ExtractThis2|
I want to remove 'ExtractThis' into one column and 'ExtractThis2' into another column. I've tried using a substring but it doesn't work as the data in column 1 is variable and therefore it doesn't always carve out my intended values. SQL below:
SELECT substring(d.Column1,13,24) FROM dbo.Table d
This returns 'Extract This' but for other columns it either takes too much or too little. Is there a function or combination of functions that will allow me to split consistently on the character? This is consistent in my column unlike my length count.
select substring(col1,CHARINDEX('=',col1)+1,CHARINDEX (';',col1)-CHARINDEX ('=',col1)-1) Val1,
substring(col1,CHARINDEX('=',col1,CHARINDEX (';',col1))+1,LEN(col1)) Val2
from #data
there is duplicate calculation that can be reduced from 5 to 3 to each line.
but I want to believe this simple optimization done by SQL SERVER.

SQL Select where id is in `column`

I have a column that has multiple numbers separated by a comma. Example for a row:
`numbers`:
1,2,6,66,4,9
I want to make a query that will select the row only if the number 6 (for example) is in the column numbers.
I cant use LIKE because if there is 66 it'll work too.
You can use like. Concatenate the field separators at the beginning and end of the list and then use like. Here is the SQL Server sytnax:
where ','+numbers+',' like '%,'+'6'+',%'
SQL Server uses + for string concatenation. Other databases use || or the concat() function.
You should change your database to rather have a new table that joins numbers with the row of your current table. So if your row looks like this:
id numbers
1 1,2,6,66,4,9
You would have a new table that joins those values like so
row_id number
1 1
1 2
1 6
1 66
1 4
1 9
Then you can search for the number 6 in the number column and get the row_id