How to retrieve column values in the following format using SQL query? - sql

I have a database with the following kind of values in two columns:
OPERATIONCONTEXT MANAGEDOBJECT
.oc.IN_HSI_service NNMi_NODE .nodei_v1_tns.OMi-DP NODEPrepaid_HSI_Service_MUMBAI
I have the requirement to write a SQL query to retieve these columns values separeted by a comma (,) in such a way that the OPERATIONCONTEXT column value is retrieved as it is but the MANAGEDOBJECT value is retrieved in a way that i get just the first two words separeted by a space.
Ex: I need to write a SQL query to retrieve the following result from the above sample DB data:
.oc.IN_HSI_service,NNMi_NODE .nodei_v1_tns.OMi-DP
I am able to get the two full column values separeted by a comma (,) with the following query:
SELECT distinct OPERATIONCONTEXT ||','|| MANAGEDOBJECT from $ALB_BASE_TABLE where OPERATIONCONTEXT is not NULL;
But, ofcourse along with the result i would like to put a restriction to check for not NULL and distinct values instead of repeated results and want to put the result in a CSV file through shell script. Any idea how to write the query?
PS: This is Oracle Database.

you can use
substr(MANGEDOBJECT, 0, INSTR(MANAGEDOBJECT, ' ', 1, 2))
Of course, it would be good to check if they are two spaces in string.
see SqlFiddle, and OracleDoc for Instr

Related

How do you query a table filtering on a substring of one of the columns?

I have a table I wish to query. It has a string variable called comment which contains an ID along with other things. (i.e. "123456;varA;varB")
rowNo
comment
1
"123456;varA;varB"
2
"987654;varA;varB"
I want to filter based on the first substring in the comment variable.
That is, I want to filter the table on rows where the first substring of comment is "123456" (which in the example would return the first row)
How do I do this?
I was thinking something along the lines of the code below, using the "string_split" function, but it doesn't work.
SELECT *,
FROM table
WHERE (SELECT value FROM STRING_SPLIT(comment,';',1)="123456")
Does anyone have any ideas?
Note, I am querying in SQL in SAS, and this is on a large dataset, so I don't want to create a new table with a new column to then query on instead. Ideally I'd want to query on the existing table directly.
You can use the SCAN() function to parse a string.
WHERE '123456'=scan(comment,1,';')

How to check if any values of an array is a substring of a column value in SQL Server 2017?

I have a table with a column equipments VARCHAR(100). I put comma-separated equipment names as string into this column.
A sample value might be:
"PKM_119160.000, PKM_119160.135"
Now, I want to filter the values of this table using an array of equipment names, checking if any value of that array is a substring of equipments. So If I pass an array like
["PKM_119160.000", "PKM_119160.216"]
I need to fetch the above row as "PKM_119160.000" is a substring of "PKM_119160.000, PKM_119160.135". I could use IN if the equipments column contains single equipment name.
How can I get the proper values here? What should I do now? TIA

Query Sql Like String

I need help for sql query LIKE.
Value for column in database is same below:
record 1 : "3,13,15,20"
record 2 : "13,23,14,19"
record 3 : "3,14,15,19,20"......
for now I want to get the most accurate record with a value of 3
This is my query :
SELECT * FROM accounts where type like '%3%'
This query will find all record with value exist is '3' eg: 13,23 ....
And It does not solve my problem.
Try this:
SELECT *
FROM accounts
WHERE CONCAT(',', type, ',') LIKE '%,3,%';
Demo
This trick places commas around the end of the type CSV string, so that we all we have to do is then check for ,3, anywhere in that string.
By the way, it is generally not desirable to store CSV data like this in your SQL tables. Instead, consider normalizing your data and storing those CSV values across separate rows.

SQL - just view the description for explanation

I would like to ask if it is possible to do this:
For example the search string is '009' -> (consider the digits as string)
is it possible to have a query that will return any occurrences of this on the database not considering the order.
for this example it will return
'009'
'090'
'900'
given these exists on the database. thanks!!!!
Use the Like operator.
For Example :-
SELECT Marks FROM Report WHERE Marks LIKE '%009%' OR '%090%' OR '%900%'
Split the string into individual characters, select all rows containing the first character and put them in a temporary table, then select all rows from the temporary table that contain the second character and put these in a temporary table, then select all rows from that temporary table that contain the third character.
Of course, there are probably many ways to optimize this, but I see no reason why it would not be possible to make a query like that work.
It can not be achieved in a straight forward way as there is no sort() function for a particular value like there is lower(), upper() functions.
But there is some workarounds like -
Suppose you are running query for COL A, maintain another column SORTED_A where from application level you keep the sorted value of COL A
Then when you execute query - sort the searchToken and run select query with matching sorted searchToken with the SORTED_A column

Store SQL query result (1 column) as Array

After running my query I get 1 column result as
5
6
98
101
Is there a way to store this result as array so that I can use it later
in queries like
WHERE NOT IN ('5','6','98','101')
I am aware of storing single variable results but is this possible?
I can not use #Table variable as I will be rerunning the query again in the future and it goes out of scope
There are multiple way of storing those column data like using Temporary Tables or View or Table valued function but IMO there is no need of storing that column data anywhere. You can directly use that column in any query saying below (or) perform a JOIN which would be much better option than NOT IN
select * from
table2
where some_column not in (select column1 from this_table);
While this method is not recommended, storing an array in a single column can be done using CSV's(Comma Separated Values). Simply create a VARCHAR array and store it by storing a string containing the values in a specific order. Basically store all of your values into a string with each value being separated by a comma in that string. Store that into a column of your choice. You can later fetch the string and parse it with a string parser i.e using the .split() function in python. AGAIN I do not recommend doing this, I would instead use multiple columns, one referring to each value and access them that way instead
Using separate columns would make it easy to use in a Stored Procedure.