Replacing comma with concatenating operator in sql server - sql

I have a base table where a field contains value of field names separated with comma.
I am trying to get the field value and query it another table to get the concatenated value.
E.g Base table A has a field Target_field which contains value such as : Addr_1,Addr2,Zip. I am trying to replace the , with + ' ' + so that when i use that field to query from another table, i get the concatenated value.
I could have used concat() function, but i want a space after a each field value.
Could you please help.

I think you may looking for REPLACE function
REPLACE('Addr_1,Addr2,Zip', ',' ,'+'' ''+')

Related

How to allow parameter to pass blank entry and comma separated multi entry?

I have inherited some code for a query to build a report off of, with a parameter that needs to be able to be left blank and return all rows, take one value and return the value of just that row, and take in multiple values and return all the rows where that value appears.
I am able to set up the query the report is running off of so that I can leave the parameter and blank and return all rows and enter one value and return that one row, or enter one value and return one row and enter multiple comma separated values and return multiple rows, but not both at the same time.
So the code for the relevant part of the query is
create table example as
'~' || (replace(replace(#ParamterNumber, ',' '~'), ' '~)) || '~' as
ParameterNumber;
select * from database d
where (select ParameterNumber from example) like '%' || d.checknumber || '%';
drop table example if exists;
From that, when I enter, for example, '123, 456' as the parameter, it will return the rows where the value is 123, and rows where the value is 456. Similarly, if the parameter is just '123', it returns rows where the value is 123. If the parameter is left blank, it returns no rows. Normally, if I wanted a blank parameter to return all rows, I would have
where d.checknumber like '%' || #ParameterNumber || '%'
, but that won't allow for multiple entries. I'm not sure how to reconcile the two into the same query so that all three conditions (blank, one entry, and multiple entries) can be satisfied.
I can explain the process on how you can achieve that, but not sure If I will be able to write the query very well.
In your procedure which SSRS is using.Using Split string function (if you have) will allow for multiple selection and isnull and nullif condition will make sure that you are getting default value 'Blank' for case where you have nulls or blanks.
where isnull(nullif(d.checknumber,''),'Blank') in (select value from split_string(#ParameterNumber,',')
Then go to your report builder, and while setting up parameters for #ParameterNumber you need to give in this way.
select distinct isnull(nullif(d.checknumber,''),'Blank') ParameterNumber from Yourtablename
give where condition if you want cascading features. This will help you in doing multi select plus blank values.
You could use coalesce function in SSRS as below
select * from database d where d.checknumber like coalesce(Parameter, product)
coalesce docs link here

how to separate columns in hive

I have a file:
id,name,address
001,adam,1-A102,mont vert
002,michael,57-D,costa rica
I have to create a hive table which will contain three columns : id, name and address using comma delimited but here the address column itself contains comma in between. How are we going to handle this.
One possible solution is using RegexSerDe:
CREATE TABLE table my_table (
id string,
name string,
address string
)
ROW FORMAT SERDE 'org.apache.hadoop.hive.contrib.serde2.RegexSerDe'
WITH SERDEPROPERTIES ('input.regex'='^(.*?),(.*?),(.*?)$')
location 'put location here'
;
Replace location property with your table location and put the file(s) into that location.
First group (.*?) will match everything before first comma, second group will match everything after first comma and before second comma and third group will match everything after second comma.
Also add TBLPROPERTIES("skip.header.line.count"="1") if you need to skip header and it always exists in the file. If header can be absent, then you can filter header rows using where id !='id'
Also you can easily test Regex for extracting columns even without creating table, like this:
select regexp_replace('002,michael,57-D,costa rica','^(.*?),(.*?),(.*?)$','$1|$2|$3');
Result:
002|michael|57-D,costa rica
In this example query returns three groups, separated by |. In such way you can easily test your regular expression, check if groups are defined correctly before creating the table with it.
Answering question in the comment. You can have address with comma and one more column without comma like this:
select regexp_replace('001,adam,1-A102, mont vert,sydney','^(.*?),(.*?),(.*?),([^,]*?)$','$1|$2|$3|$4');
Returns:
001|adam|1-A102, mont vert|sydney
Checking comma is optional in Address column:
hive> select regexp_replace('001,adam,1-A102 mont vert,sydney','^(.*?),(.*?),(.*?),([^,]*?)$','$1|$2|$3|$4');
Returns:
001|adam|1-A102 mont vert|sydney
Read this article for better understanding: https://community.cloudera.com/t5/Community-Articles/Using-Regular-Expressions-to-Extract-Fields-for-Hive-Tables/ta-p/247562
[^,] means not a comma, last column can be everything except comma.
And of course add one more column to the DDL.

Is there a way to do a word search for values of one column in another column?

I have a database that contains columns that are text fields (strings, a few sentences long) and columns that are shorter strings (eg: college majors). Is there any way I can use the 'LIKE' function in SQL to search whether one of the values of the College Major column appears in another column?
I don't want to write out each of the college majors as a string since there are over 100.
Yes you can. Something like
where bigdatacolumn like '%' + computer_major + '%'
Since you said, other column contains few lines (Text column), you probably want to consider using Full Text Search instead of using LIKE operator
Use The ANY operator to compare in an array as shown in the below query.
select * from staff where department ILIKE ANY ( ARRAY['AUT%', '%COM%' ,'SP%' ] :: TEXT[] );

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, ...

How to split a column which contains a combined text and number?

I have a column in a table which consists of data like name50, somename20, other40, some65 like that.
I want to split the text part and number part and add the number part into another table with an empty column, which contains a column already with the text part. Now I have add the number part to the corresponding name part in this table.
For example in the second table I have a column called Textpart with the same text part from the first tables column (which I want to split) with all the names repeated several times randomly. And another caolumn called Numberpart which is empty.
Now I have to fill that numberpart with the corresponding numbers from the first table.
Please help me. thank you.
You can use a combination of substring and patindex.
First extract the numeric part. To get the text part just replace the previously found numeric part with an empty string.
select substring(data, patindex('%[0-9]%', data), len(data)) as numeric_part,
replace(data, substring(data, patindex('%[0-9]%', data), len(data)), '') as text_part
from tablename
To update the other table with the numeric part, use the text_part column to join.
Note that this will only work well if the numbers are towards the end.