Is it possible to use a variable in the assignfile command?
Eg.
f : Textfile ;
sFile : string ; {contains 'MyFile.txt' as content}
...
cFileDir = 'C:\Users\User\Desktop\Data Engine\Data\Country' ;
...
Assignfile(f, cFileDir + '\' + sFile) ;
...
I appreciate your help very much. if it's unclear I'll edit the question to add more details.
Is it possible to use a variable in the AssignFile command?
Yes.
The second parameter of AssignFile has type string.
The expression cFileDir + '\' + sFile has type string.
FWIW, AssignFile is known as a function rather than a command. Getting on top of terminology like this will help you learn the language more effectively.
Related
Dear All Master,
How to convert or replace the string "-" to bracket "()" in vba ?.
I can only do it manually with find and replace. The original record there were three thousand.
please recommend the solution.
FILENAME
DESIRED FILENAME
4715-(0).jpg
4715(0).jpg
TC45-1.jpg
TC45(1).jpg
TC51-1b.jpg
TC51(1b).jpg
TC52-B-1.jpg
TC52(B1).jpg
WSO.19-A.jpg
WSO.19(A).jpg
N.WAB25-AF.jpg
N.WAB25(AF).jpg
WA.4-K1.jpg
WA.4(K1).jpg
PD.133-AFa.jpg
PD.133(Afa).jpg
KP02-10.jpg
KP02(10).jpg
01-4-1-B.jpg
01-4-1(B).jpg
01-4-01-1.jpg
01-4-01(1).jpg
You could have a go with a regular expression. Something like:
-\(?(\w+)\)?(\.\w+)$
See an online demo
-\(? - Match an hyphen and an optional literal opening paranthesis;
(\w+) - A 1st capture group to match 1+ word-characters;
\)? - An optional closing paranthesis;
(\.\w+) - A 2nd capture group just to assert that previous matches are done before the extension. Here we match a dot and 1+ word-characters;
$ - End-line anchor to assert all previous matching is done at end of string.
You could chuck this into a VBA UDF and either call it from inside another sub or directly from th worksheet:
Public Function RegexReplace(s As String) As String
Static RE As Object: If RE Is Nothing Then Set RE = CreateObject("vbscript.regexp")
RE.Pattern = "-\(?(\w+)\)?(\.\w+)$"
RegexReplace = RE.Replace(s, "($1)$2")
End Function
I'm looking for some assistance in debugging a REGEXP_REPLACE() statement in Snowflake.
I wanted to replace |(pipe) between double quoted string only with #.
Example:
"Foreign Corporate| Name| Registration"|"99999"|"Valuation Research"
Required Result:
"Foreign Corporate# Name# Registration"|"99999"|"Valuation Research"
I have tried regex101.com with (?!(([^"]"){2})[^"]*$)[|] and substitution\1#, works, but doesn't work in Snowflake.
The regexp functions in Snowflake do not lookahead and lookbehind. If you want to use regular expressions with lookahead and lookbehind functions, you can do so in a JavaScript UDF.
Note that the regular expression here finds all the pipes including those inside double quotes. I was able to find a regular expression that finds pipes outside double quotes, which is why this UDF splits by those findings and rejoins the string. If you can find a regular expression that finds the pipes inside rather than outside the double quotes, you can simplify the UDF. However, splitting it allows other possibilities such as removing wrapping quotes if you want to do that.
set my_string = '"Foreign Corporate| Name| Registration"|"99999"|"Valuation Research"';
create or replace function REPLACE_QUOTED_PIPES(STR string)
returns string
language javascript
as
$$
const search = `(?!\\B"[^"]*)\\|(?![^"]*"\\B)`;
const searchRegExp = new RegExp(search, 'g');
var splits = STR.split(searchRegExp);
var out = "";
var del = "|";
for(var i = 0; i < splits.length; i++) {
if (i == splits.length -1) del = "";
out += splits[i].replace(/\|/g, '#') + del;
}
return out;
$$;
select REPLACE_QUOTED_PIPES($my_string);
Different approach, just using REPLACE
Replace "|" with a string that will never appear in your data. I've used ### in my example
Replace the remaining pipes with #
Replace the dummy string, ###, back to the original value "|"
e.g.
replace(replace(replace(sample_text,'"|"','###'),'|','#'),'###','"|"')
SQL statement to show each step:
select
sample_text
,replace(sample_text,'"|"','###') r1
,replace(replace(sample_text,'"|"','###'),'|','#') r2
,replace(replace(replace(sample_text,'"|"','###'),'|','#'),'###','"|"') r3
from test_solution;
Is there 'concat' function in GreenPlum? I can use concat function in postgresql and it works well, but when i use it in Greenplum, I got an error.
select concat('a', 'b');
ERROR: function concat(unknown, unknown) does not exist at character 8
HINT: No function matches the given name and argument types. You may need to add explicit type casts.
LINE 1: select concat('a', 'b');
^
Is there some other functions can instead of 'concat' function in GreenPlum? And I have tried to create a function to instead of it, but got some syntax errors also.
CREATE OR REPLACE FUNCTION my_concat(VARIADIC arr VARCHAR[] ) RETURNS VARCHAR AS $$ SELECT array_to_string(arr, ''); $$ LANGUAGE SQL;
ERROR: syntax error at or near "VARCHAR" at character 51
LINE 1: CREATE OR REPLACE FUNCTION my_concat(VARIADIC arr VARCHAR[] ...
^
Anyone can help? Thanks very much!
Like most databases, Greenplum uses "||" to concatenate two strings together.
SELECT 'Green' || 'plum';
Result:
Greenplum
its a versional issue , you have use || in place where ever u using contact function.
Greenplum doesn't have the concat function yet. May be you can modify your code to use "||" instead of concat.
Well,
First I agree that you should replace your code to use the correct SQL syntax '||' for concatenation.
If you really want to create a function to emulate the concat, you could do something like:
create or replace function myschema.concat(arg1 text, arg2 text)
returns text as
$body$
declare
v_arg1 text;
v_arg2 text;
begin
v_arg1 := arg1;
v_arg2 := arg2;
return v_arg1 || v_arg2;
end
$body$
language plpgsql volatile;
Then, the query will work:
select myschema.concat('test1', 'test2');
>>test1test2
Hope you are looking for the below query.
gpadmin=# CREATE OR REPLACE FUNCTION my_concat( character varying[] ) RETURNS VARCHAR AS $$ SELECT array_to_string($1, ''); $$ LANGUAGE SQL;
gpadmin=# select my_concat(ARRAY['Green','plum']);
my_concat
Greenplum
I wanted to use a named parameter placeholder inside the LIKE operator pattern so that the argument string is properly escaped.
Here's my modified code where I am using the at-param placeholder:
var stmt = new air.SQLStatement();
stmt.text = "SELECT * FROM comments WHERE title LIKE '%#search%';";
stmt.parameters["#search"] = "argument string";
stmt.execute();
Doing so yields an SQLError with the following details
message: Error #3315: SQL Error.
details: '#search' parameter name(s) found in parameters property but not in the SQL specified
As suggested by Mike Petty, I tried:
stmt.text = 'SELECT * FROM comments WHERE title LIKE "#%search%";';
Which yields to the same SQL Error details.
Documentation has this:
expr ::= (column-name | expr) LIKE pattern
pattern ::= '[ string | % | _ ]'
My suspicion is that it is skipped due to the qoutes, any ideas on how to make it work?
Found a solution for this, basically instead of doing it like this:
var stmt = new air.SQLStatement();
stmt.text = "SELECT * FROM comments WHERE title LIKE '%#search%';";
stmt.parameters["#search"] = "argument string";
stmt.execute();
You have to put a placeholder for the entire LIKE operator pattern and bind the pattern as a parameter.
var stmt = new air.SQLStatement();
stmt.text = "SELECT * FROM comments WHERE title LIKE #search;";
stmt.parameters["#search"] = "%" + userInput + "%";
stmt.execute();
Remove your :string from your string.
Like works just fine as:
SELECT * FROM comments WHERE comments.title LIKE '%string%';
It's hard to tell from your question, but is your current statement either throwing a SQLError, just doesn't compile, or just doesn't return any results?
If I had to guess I'd say you have a few issues here:
You shouldn't have to qualify the column in the where clause since you only have 1 table to select from
The parameter string is technically a reserved word by AIR / Actionscript (although the case is different) - still a bit confusing.
Even though the documentation and code allow you to use either the colon, or at-symbol, my preference is the # since it's a bit easier to see ;)
Don't forget to add an itemClass definition of the Class you expect for rows - this helps to avoid anonymous objects.
This should be fairly straight forward:
var stmt:SQLStatement = new SQLStatement();
stmt.itemClass = Comment;
stmt.text = 'SELECT * FROM comments WHERE title LIKE "#%search%";';
stmt.parameters['#search'] = userInput.text;
stmt.addEventListener(SQLEvent.RESULT, onResults);
stmt.execute();
I have a database running on an MS SQL Server. My application communicates via JDBC and ODBC with it. Now I try to use prepared statements.
When I insert a numeric (Long) parameter everything works fine. When I insert a string
parameter it does not work. There is no error message, but an empty result set.
WHERE column LIKE ('%' + ? + '%') --inserted "test" -> empty result set
WHERE column LIKE ? --inserted "%test%" -> empty result set
WHERE column = ? --inserted "test" -> works
But I need the LIKE functionality. When I insert the same string directly into the query string (not as a prepared statement parameter) it runs fine.
WHERE column LIKE '%test%'
It looks a little bit like double quoting for me, but I never used quotes inside a string. I use preparedStatement.setString(int index, String x) for insertion.
What is causing this problem?
How can I fix it?
Thanks in advance.
What are you inserting at '?'
If you are inserting
test
Then this will result in
WHERE column LIKE ('%' + test + '%')
which will fail. If you are inserting
"test"
Then this will result in
WHERE column LIKE ('%' + "test" + '%')
Which will fail.
You need to insert
'test'
Then this will result in
WHERE column LIKE ('%' + 'test' + '%')
And this should work.
I don't know why = "test" works, it should not unless you have a column called test.
I am using SUN's JdbcOdbcBridge. As far as I read yet, you should avoid to use it. Maybe there is a better implementation out there.
For now, I wrote the folling method. It inserts string-type parameters into the statement with string operations before the statement is compiled.
You should build a map of the parameters with the parameter index as the key and the value as the parameter itself.
private static String insertStringParameters(String statement, Map<Integer, Object> parameters) {
for (Integer parameterIndex : parameters.keySet()) {
Object parameter = parameters.get(parameterIndex);
if (parameter instanceof String) {
String parameterString = "'" + (String) parameter + "'";
int occurence = 0;
int stringIndex = 0;
while(occurence < parameterIndex){
stringIndex = statement.indexOf("?", stringIndex) + 1;
occurence++;
}
statement = statement.substring(0, stringIndex - 1) + parameterString + statement.substring(stringIndex);
}
}
return statement;
}