How to check if any values of an array is a substring of a column value in SQL Server 2017? - 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

Related

How to spread the values from a column in Hive?

One field of table is made up of many values seperated by comma,
for example, a record of this field is:
598423,4803510,599121,98181856,1666529,106317962,4061964,7828860,598752,728067,599809,8799578,1666528,3253720,601990,601235
I want to spread the values in every record of this field in Hive.
Which function or method I can use to realize this?
Thanks.
I'm not entirely sure what you mean by "spread".
If you want an output table that has a value in every row like:
598423
4803510
599121
Then you could use explode(split(data,',')
Otherwise, if each input row has exactly 16 numbers and you want each of the numbers to reside in a different column, you have two options:
Define the comma as a delimiter for the input table ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
Split a single column into 16 columns using the split UDF: SELECT split(data,',')[0] as col1, split(data,',')[1] as col2, ...

Extract alphanumeric value from varchar column

I have a table which contains a column having alphanumeric values which is stored as a string. I have multiple values in that column having values such as F4737, 00Y778, PP0098, XXYYYZ etc.
I want to extract values starting with a series of F and must have numeric values in that row.
Alphanumeric column is the unique column having unique values but the rest of the columns contain duplicate values in my table.
Futhermore, once these values are extracted I would like to pick up the max value from the duplicate row,for eg:
Suppose I have F4737 and F4700 as a unique Alphanumeric row, then F4737 must be extracted from it.
I have written a query like this but the numeric values are not getting extracted from this query:
select max(Alplanumeric)
from Customers
where Alplanumeric '%[F0-9]%
or
select max(Alplanumeric)
from Customers
where Alplanumeric like '%[0-9]%'
and Alplanumeric like 'F%'**
I run the above query but I am only getting the F series if I remove the numeric part from the above query. How do I extract both, the F starting series as well as the numeric values included in that row?
Going out on a limb, you might be looking for a query like this:
SELECT *, substring(alphanumeric, '^F(\d+)')::int AS nr
FROM customers
WHERE alphanumeric ~ '^F\d+'
ORDER BY nr DESC NULLS LAST
, alphanumeric
LIMIT 1;
The WHERE conditions is a regular expression match, the expression is anchored to the start, so it can use an index. Ideally:
CREATE INDEX customers_alphanumeric_pattern_ops_idx ON customers
(alphanumeric text_pattern_ops);
This returns the one row with the highest (extracted) numeric value in alphanumeric among rows starting with 'F' followed by one ore more digits.
About the index:
PostgreSQL LIKE query performance variations
About pattern matching:
Pattern matching with LIKE, SIMILAR TO or regular expressions in PostgreSQL
Ideally, you should store the leading text and the following numeric value in separate columns to make this more efficient. You don't necessarily need more tables like has been suggested.

Compare XML data to String

I have a table that houses a bunch of data in an XML field. I can get to the data and display what I need in the select statement, but I also need to use that to compare to another table that houses a translation I am trying to do. Is there a way to compare the value being returned from the XML data to a string value that exists in another table?
The code in my select to return the XML data is:
prv.reported_attributes.value('(/row[#ATTRIBUTE="FIELD"][1])/#VALUE', 'varchar(5)')
I need to compare that text output to another table, but I keep getting NULL like the values I am trying to compare do not match. I have confirmed they do in fact have matches.

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

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

Stored procedure that takes list of string as input and insert missing rows then return them

I have a table Names
Id Name
+----+---------------+
1 John
2 Kate
3 Mark
I want to create a stored procedure that does the following:
1) take a list of names as string as an input parameter
I have done some researches about this but couldn't find the best way to do it. I will call the stored procedure from the entity framework in a C# application. I was thinking of passing the names in one string separated with a comma and the split them in the procedure. But can't figure out how this is done.
2) for each name in the list, if the name does not exist in the Name column, insert a new row for it.
How can I do a switch case if it exists and insert it if not
3) Select * rows that are in the input list.
After adding all the missing Names, I want to select all the names that were in the input list with their id
Can this be done in one stored procedure, or do I have to divide them into multiple.
I am looking for hints on how to do each step, and if they can be combined.
Thanks
Keep your DB side lean and leave logic on the app side, especially if you have grumpy DBA's.
Use a MERGE/Upsert instead.
Check out this SO post.
If you pass a comma delimited list into a stored procedure as a parameter, you are going to need to understand how to use charindex, left, substring and right
As you split out each name - you would add them into a temporary table or a table valued variable.
Your decision about whether to insert the new names into the Names table would be made using an exists() subquery on an insert statement.
You could then, finally, fashion a select statement to join your temp table/table valued variable back to your Names table to pull out all of the keys (including the new ones) and pass them back to your front end.