I have multiple strings:
address_1 = '226'
address_2 = 'Virginia Ave'
address_city = 'Trenton'
address_state = 'NJ'
address_country = 'US'
address_postal_code = '08610'
I am trying to combine them into a single string, separated from one another with comma, as follows:
"226 Virginia Ave, Trenton, NJ, 08610, US"
How can I combine my variables in a string, separate them using a comma, and in case any of the variable is missing, then do not display the extra comma?
I did this:
(address_1.to_s + ' ' + address_2.to_s + ' ' + address_city.to_s + ' ' + address_state.to_s + ' ' + address_postal_code.to_s + ' ' + address_country.to_s).squish
This gives output like this:
"226 Virginia Ave Trenton NJ 08610 US"
This happens because I am adding space + ' ' +. If I do + ', ' +, in case any of the address_ is nil or empty, it still displays the extra ,, and the address ends up looking like this:
"226 Virginia Ave, , , 08610, US"
address_1 = '226'
address_2 = 'Virginia Ave'
address_city = 'Trenton'
address_state = 'NJ'
address_country = 'US'
address_postal_code = '08610'
["#{address_1} #{address_2}", address_city, address_state, address_country, address_postal_code].map{|line| line.to_s.strip}.select{|line| !line.empty?}.join(', ')
This puts all the address elements in an array, maps to change all lines to a stripped string, and then selects lines that are not empty, and then joins using a comma and space.
The first element is address_1 and address 2 combined so as to avoid a comma after house number.
Try this using array compact. This may solve your problem.
address_arr = [address_1, address_2, address_city, address_state, address_country, address_postal_code]
no_empty_address = address_arr.reject{ |c| c.empty? }
no_empty_address.compact.join(', ')
Related
In order to create PlaceKey for addresses to link some of my tables, I need to split an address column in SnowFlake.
I am not familiar with JavaScript, but I tried Javascript UDF in SnowFlake. Then I don't know how to deal with the addresses like '123_45ThSt'.
The output of my function is like '123_45 Th St'. I am stuck here.
The expected output is '123 45Th St'.
Hope someone could help me out. Much appreciated!
Below is another example and my SnowFlake SQL code:
Original address column: 12345NE17ThSt
The expected column: 12345 NE 17Th St
My function's output: 12345 NE17 ST
My function:
CREATE OR REPLACE FUNCTION Split_On_Upper_Case(s string)
RETURNS string
LANGUAGE JAVASCRIPT
AS '
function Split_On_Upper_Case(str){
str=str.split(/(?=[A-Z])/).join(" ")
return str
}
// Now call the function
return Split_On_Upper_Case(S);
'
;
Assuming the format of street address, which includes number + word (ends with lower case or number) + word (start with upper case), I have below solution:
CREATE OR REPLACE FUNCTION Split_On_Upper_Case(s string)
RETURNS string
LANGUAGE JAVASCRIPT
AS $$
regexp = /([0-9]+)(NE|SE|NW|SW)?(.*[0-9a-z]{1})([A-Z][a-zA-Z0-9]+)/g;
splits = regexp.exec(S.replace(/_/g, " "));
if (splits && splits.length == 5) {
return
splits[1].trim() + " " +
(splits[2] ? splits[2].trim() + " ": "" ) +
splits[3].trim() + " " +
splits[4].trim();
}
return "not found" // or whatever you want to do
$$;
Then try to run the function:
select Split_On_Upper_Case('12345NE17ThSt');
-- 12345 NE 17Th St
select Split_On_Upper_Case('123_45ThSt');
-- 123 45Th St
select Split_On_Upper_Case('35TestSt');
-- 35 Test St
It returns expected output, but if you have more sample inputs, they can help to validate.
I want to move among the sheets within the same workbook, my sheet names are like 'Order 1', 'Order 2', 'Order 3'........so on. I want to take the value from loop along with 'Order' string like 'Order [i]' and want to paste some formula that also takes some value from the loop like ={Link!B[[i]+2]}
I tried with this following code but can't be succeeded.
function Order() {
var spreadsheet = SpreadsheetApp.getActive();
for (var i = 1; i <= 10; i++) {
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Order [i]'));
spreadsheet.getRange('B3').activate();
spreadsheet.getCurrentCell().setValue('={Link!B[[i]+2]}');;
}
};
When you specify 'Order [i]', your script looks for a sheet called "Order [i]". Instead, you should write 'Order ' + i.
Similarly, '={Link!B[[i]+2]}' will return "={Link!B[[i]+2]}", just as you wrote it. Instead, write '={Link!B' + (i+2) + '}'.
Also, you don't need the .activate() or .setActiveSheet() calls. You can simply get the range and then immediately set the value. You're getting the sheet already with the .getSheetByName() method. In fact, you link all of it together.
function Order() {
var spreadsheet = SpreadsheetApp.getActive();
for (var i = 1; i <= 10; i++) {
spreadsheet.getSheetByName('Order ' + i)
.getRange('B3')
.setValue('={Link!B' + (i+2) + '}');
}
};
The string 'Order [i]' will always just be 'Order [i]', as everything is wrapped in quotes so the computer is interpreting it literally. You want to use something like 'Order ' + i, which will evaluate what i is and then append it to 'Order'.
In the second example, you might want something like '={Link!' + B[i+2] + '}'
I need to concatenate the following statement in SQL.
I am joining 2 tables, the MEDINFO table and the IMMUNIZE table.
There should be one record for each of the shot code dates as long as it is not null or 01/01/1901.
The output must look like this:
"Admin-" + medinfo_field_31 + ", Manuf-" + medinfo_field_32 + ", Lot-" + medinfo_field_33 + ", Exp-" + medinfo_field_34 + ", Site-" + medifno_field_35 + ", Dose-" + medinfo_field_36
Here is the criteria for the SQL statement I must create:
IF immunize_shot_code = 'FLU',
then concatenate medinfo_field_12 and medinfo_field_19 thru medinfo_field_24
IF immunize_shot_code = 'MENI',
then concatenate medinof_field_11 and medinfo_field_25 thru medinof_field_30
IF immunize_shot_code = 'TETA',
then concatenate medinfo_field_13 thru medinfo_field_18 and medinfo_field_31 thru medinfo_field_36"
Thanks for any ideas.
This would look something like the followning:
SELECT
(CASE WHEN Immunize_shot_code='Flu'
THEN medinfo_field_12+', '+med_info_field_19+ ', '+med_info_field_20+', '+med_info_field_21+ ', '+med_info_field_22+', '+med_info_field_23+', 'med_info_field_24
WHEN immunize_shot_code='MENI'
THEN [CONCATINATE AS PER ABOVE]
.....
.....
END) as ImmuCode
FROM TABLE_1
Like some of them mentioned above, you could use CASE statement something like this:
select
case when i.immunize_shot_Code = 'FLU'
then concat(m.medinfo_field_12,',',m.medinfo_field_19,',',m.medinfo_field_24...)
when i.immunize_Shot_Code = 'MENI'
then concat(m.medinfo_field_11,',',m.medinfo_field_11)
when i.immunize_Shot_code = 'TETA'
then concat(m.medinfo_field_13,',',m.medinfo_field18,',')
end as concat_value
from medinfo m
join immunize i on m.id = i.medinfo_id --change this to match your join
where (shot_code_date is not null or shot_code_date <> '01-01-1901');
Here is the section of code, I am using ' ' blah ' ' to escape single quotes but I guess its not working:
declare
my_func varchar2(20) :='test_func';
begin
execute immediate 'insert into TABLE_TEST (OUTPUT) select ' || my_func || ' from dual where TABLE_TEST.FUNCTION_NAME like ' 'VALIDATION1_%' ' ';
end;
I am getting the following error:
PLS-00103: Encountered the symbol "VALIDATION1_%" when expecting one of the following:
& = - + ; < / > at in is mod remainder not rem return
returning <an exponent (**)> <> or != or ~= >= <= <> and or
like like2 like4 likec between into using || bulk member
submultiset
The symbol "* was inserted before "VALIDATION1_%" to continue.
It looks like you are trying to escape the single quote with another single quote (which is good), but there is an extra space in between the two. That has to go.
Change
' from dual where TABLE_TEST.FUNCTION_NAME like ' 'VALIDATION1_%' ' '
to
' from dual where TABLE_TEST.FUNCTION_NAME like ''VALIDATION1_%'' '
In cases like this it's much simpler to use the new q syntax for literal strings, e.g.:
execute immediate 'insert into TABLE_TEST (OUTPUT) select ' || my_func ||
q[' from dual where TABLE_TEST.FUNCTION_NAME like 'VALIDATION1_%' ]';
I am performing pattern matching on #ProductDescription_Glossary in SQL Server 2005 to replace complete words.
#GlossaryKeyword variable contains word to be matched and replaced.
The following code replaces #GlossaryKeyword found at the beginning, at the end and in the end of #ProductDescription_Glossary, but this code cannot handle replacement successfully if #ProductDescription_Glossary contains parentheses at start or end of word
Case 1: this case is working properly - Heather is not replaced with tooltip link with word Heatherd
#GlossaryKeyword = Heather
#ProductDescription_Glossary = Heathered
Case 2: this case fails - In this case Heather is replaced, my requirement is that, heather do not get replaced, as in case 1 so provide me with required pattern.
#GlossaryKeyword = Heather
#ProductDescription_Glossary = (Heathered
Thanks in advance.
Note: #GlossaryKeyword has alpha numeric, hyphen and / character only i,e (0-9, A-Z, a-z, -, /)
#ProductDescription_Glossary contains HTML tags, which are handled by default (may be due to collation settings on my server)
Code:
if PATINDEX ('%[^a-z]' + #GlossaryKeyword + '[^a-z]%','.' + #ProductDescription_Glossary + '.') > 0
BEGIN
SET #ProductDescription_Glossary = REPLACE(#ProductDescription_Glossary,#GlossaryKeyword, '<a target="_blank" id="q_' + CAST (#GlossaryID AS VARCHAR(10)) + '" class="anchor_regular_Mehroon" href="javascript: void(0);">' + #GlossaryKeyword + '</a>')
SET #GlossaryToolTip = #GlossaryToolTip + '<div id="a_q_' + CAST (#GlossaryID AS VARCHAR(10)) + '" class="toolTip_glossary" style="display:none;">' + #GlossaryKeywordDescription + '</div>'
END
Just as ( ) to the not
select PATINDEX('%[^a-z(]'+ 'Heather' + '[^a-z)]%', '(Heathered red, purple, royal and navy)')
And it is not regex.
And you want to exclude ( - not include.