How to select part of the string with T-SQL? - sql

I have a string comma delimited. I need to select with t-sql just first part of it.
Example
table
id items values
1 item, item2, item3, item4 5
I need to get result like
ID Items Values
1 item 5

Try this one
select id,
substring(items,0,charindex(',',items)),
values
from mytable

Related

Select rows from table with specific string value endings from list

I have a table with two columns item_name, value where item_names looks like "abracadabra_prefix.tag_name". And I need to select rows with tag_names from a list that doesn't have a prefix.
Should be somthing like:
tag_names = ['f1', 'k500', '23_g']
SELECT * FROM table WHERE item_name IN (LIKE "%{tag_names});
input table:
item_name
value
fasdaf.f1
1
asdfe.f2
2
eywvs.24_g
2
asdfe.l500
2
asdfe.k500
2
eywvs.23_g
2
output table:
item_name
value
fasdaf.f1
1
asdfe.k500
2
eywvs.23_g
2
I have tried concatenating a string in a loop to get a query like this:
SELECT * FROM table WHERE item_name LIKE '%f1' OR item_name LIKE '%k500' OR item_name LIKE '%23_g';
But I can have from 1 to 200 tags, and with a large number of tags, this makes the query too complicated,as I understand it.
You can extract the suffix of item_name using substring with regexp and then use the any operator for comparison in the where clause.
select * from the_table
where substring (item_name from '\.(\w+)$') = any('{f1,k500,23_g}'::text[]);
SQL fiddle demo
If you intend to use the query as a parameterized one then it will be convenient to replace '{f1,k500,23_g}'::text[] with string_to_array('f1,k500,23_g', ','), i.e. pass the list of suffixes as a comma-separated string. Please note that this query will result in a sequential scan.
You can use:
UNNEST to extract tag values from your array,
CROSS JOIN to associate tag value to each row of your table
LIKE to make a comparison between your item_name and your tag
SELECT item_name, value_
FROM tab
CROSS JOIN UNNEST(ARRAY['f1', 'k500', '23_g']) AS tag
WHERE item_name LIKE '%' || tag || '%'
Output:
item_name
value_
fasdaf.f1
1
asdfe.k500
2
eywvs.23_g
2
Check the demo here.

How to select rows that are like other rows in the same table & column

I'm trying to find rows that contain the "&" character and are like the rows with the "&" character but do not contain any characters after and including the "&" character from the examples below. Both rows would be selected from the same item column in the items table. I would like both the rows with the "&" and the rows without the "&" returned. If no rows exist for the item without the "&", I still would like the row returned as blank or null.
What I have below currently selects the items with the "&" and selects the characters after and including the "&" character, but I need help figuring out how to find the rows that do not have the "&" character or characters after the "&" based on the data currently selected. This query is being run in Oracle.
item
abc&123
def&456
xyz&789
123456
123457
select item, substr(item, instr(item, '&')) as Trimmed_Item
from items where item like '%&%';
Current result:
item
Trimmed_Item
abc&123
&123
def&456
&456
xyz&789
&789
Desired result:
item
Trimmed_Item
abc&123
abc
def&456
def
xyz&789
Does anybody have an idea of how to do this? Thanks!
You can use this:
with items as
(
select 'abc'||chr(38)||'123' as item from dual union all
select 'def'||chr(38)||'456' as item from dual union all
select 'xyz'||chr(38)||'789' as item from dual union all
select '123456' as item from dual union all
select '123457' as item from dual
)
select item, decode(regexp_substr(item, '[^&]+', 1, 1),item,' ',regexp_substr(item, '[^&]+', 1, 1)) as Trimmed_Item
from items
ITEM TRIMMED_ITEM
------- -------------
abc&123 abc
def&456 def
xyz&789 xyz
123456
123457
Thank you

Get the sum of partial value in a column

Consider there is a table tableA
col1 col2
1 some random string and number 1213 aa5 string aaasome number
2 some random string 432682 aa3 test
1 aa7
I need to get the result as below.
1 12
2 3
group by col1 and the result will be 5+7 (the partial int after the 'aa' string)
To add more clarity to the question,the col2 has some other strings as well.. like test test test aa2 again test test 23u45 ajsdk 4834... . Here i need to pick the 2 alone.
kindly suggest a solution for this.
You need to get rid of the prefix, cast to a number, and sum. One method looks like:
select col1, sum(cast(replace(col2, 'aa', '') as number)
from tablea a
group by col1;
You can use regular expression to get the required digits from the string:
Select col1, sum(regexp_replace(col2,'(^|.*\s)aa(\d+)(\s.*|$)', '\2'))
From t
Group by col1
demo

DB2: fill a dummy field with values in for loop while a select

I want to fill a dummy field with values in a for loop during a select:
Somethinhg like (table account e.g. has a field "login")
select login,(for i= 1 to 3 {list=list.login.i.","}) as list from account
The result should be
login | list
aaa | aaa1,aaa2,aaa3
bbb | bbb1,bbb2,bbb3
ccc | ccc1,ccc2,ccc3
Can someone please help me if that is possible !!!!
Many Thanks !
If this is an one-off task and the size of your loop is fixed, you can make up a table of integers and do a cartesian product with your table containing the column login:
SELECT ACC.LOGIN || NUMBRS.NUM FROM
ACCOUNT ACC, TABLE (
SELECT '1' AS NUM FROM SYSIBM.SYSDUMMY1 UNION
SELECT '2' AS NUM FROM SYSIBM.SYSDUMMY1 UNION
SELECT '3' AS NUM FROM SYSIBM.SYSDUMMY1
) NUMBRS
which will give you strings like 'aaa1', 'aaa2', 'aaa3' one string per row. Then, you can aggregate these strings with LISTAGG.
If the size is not fixed, you can always make up a temporary table and fill it up with appropriate data and use it instead of the NUMBRS table above.

Select a column from two tables and put them into one

I don't just mean put them together as in I have one row in table 1 that says "Stuff" and another row in table 2 that says "Things" and that puts them together into a single cell that says "StuffThings", I just mean to simply have all the data in table 1 and table 2 combined into one column. For example....
Table 1:
Item 1
Item 2
Item 3
Table 2:
Item 4
Item 5
Item 6
New Table:
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
All my attempts to do this have been something like
Select (Table1.Row + Table2.Row) AS JobNumber
FROM Table1, Table2;
It just does something like
Item 1Item 2
Item 1Item 3
.....
etc.
I think you want a UNION query:
SELECT Table1.Row AS JobNumber
FROM Table1
UNION
SELECT Table2.Row AS JobNumber
FROM Table2
Or UNION ALL, if you want duplicates.