I would like to write to the standard output in fortran without adding a line break. That is, I want to do something like this:
a='some string and '
b='some other string'
write(*,101) a
...
write(*,102) b
...
101 format(a,...)
102 format(a)
Is it possible to use some kind of format statement to supress the line break in 101, such that the code outputs "some string and some other string" on the same output line?
Note that it is important that the two write statements are separated, as the code in between is actually used to generate the second string.
You can used the advance='no' option:
a='some string and '
b='some other string'
write(*,101,advance='no') a
...
write(*,102) b
...
101 format(a)
102 format(a)
This will suppress the linebreak.
Related
I'm stuck with an SQL query to get what I need. My input is this:
COD SINOM
A 123456
B 987654, 123456, 111111
C 123456 , 234501
D 9912345699
E 99123456, 789012
F 77123456
Both fields are text type, even if they are numbers on it (we use text type because sometimes it has leading zeros). Mycriteria is 123456
I'm trying to query all COD where SINOM contains my criteria, but only as 6 chars in a single word. It does not matter if it has any leading blanks or commas or semicolons (all of them are possible), but len of string must be 6, and could be more text. And criteria can be at left, right or middle of SINOM.
My actual query is this:
SELECT Table1.cod, Table1.sinom
FROM Table1
WHERE (((Table1.sinom)="123456")) OR (((Table1.sinom) Like "*123456,*")) OR (((Table1.sinom) Like "*123456 *"))
But the output I'm getting is wrong:
The right ones would be only the first 3 rows (A,B,C). Rest of rows are wrong. My expected output would be:
9912345699, 99123456, 789012 and 77123456 are wrong because it contains 123456 but not as a single word.
Looking for an SQL query to apply this and trying to avoid the use of an VBA function if possible
Other SYNOM that would be correct would be:
998877, 123456, 029384
012310,123456
I hope this is clear, but please, do not hesitate to ask if any doubt arises.
Thanks!
I think you want something like this:
WHERE "," & REPLACE(Table1.sinom, " ", "") & ",") Like "*,123456,*"
The key is to look for the delimited strings -- but to be sure that the column has commas at the beginning and end.
One caveat here is the spaces. This removes the spaces, assuming they are merely spaces.
I might suggest something along the lines of the following:
select * from table1 where "," & table1.sinom & "," Like "*[!0-9]123456[!0-9]*"
The use of [!0-9] will account for the possibility of any delimiter surrounding the numerical values, and the concatentation with commas (which could be any non-numerical character) accounts for cases in which the number appears on its own or at the start or end of the string.
I am doing this in Impala or Hive. Basically let say I have a string like this
f-150:aa|f-150:cc|g-210:dd
Each element is separated by the pipe |. Each has prefix f-150 or whatever. I want to be able to remove the prefix and keep only element that matches specific prefix. For example, if the prefix is f-150, I want the final string after regex_replace is
aa|cc
dd is removed because g-210 is different prefix and not match, therefore the whole element is removed.
Any idea how to do this using string expression in one SQL?
Thanks
UPDATE 1
I tried this in Impala:
select regexp_extract('f-150:aa|f-150:cc|g-210:dd','(?:(?:|(\\|))f-150|keep|those):|(?:^|\\|)\\w-\\d{3}:\\w{2}',0);
But got this output:
f-150:aa
In Hive, I got NULL.
The regexyou in question could look like this:
(?:(?:|(\\|))f-150|keep|those):|(?:^|\\|)\\w-\\d{3}:\\w{2}
I have added some pseudo keywords to retain, but I am sure you get the idea:
Wholy match elements that should be dropped but only match the prefix for those that should be retained.
To keep the separator intact, match | at the beginning of an element in group 1 and put it back in the replacement with $1.
Demo
According to the documentation, your query should be written like a Java regex; likewise, this should perform like this code sample in Java.
You could match the values that you want to remove and then replace with an empty string:
f-150:|\|[^:]+:[^|]+$|[^|]+:[^|]+\|
f-150:|\\|[^:]+:[^|]+$|[^|]+:[^|]+\\|
Explanation
f-150: Match literally
| Or
\|[^:]+:[^|]+$ Match a pipe, not a colon one or more times followed by not a pipe one or more times and assert the end of the line
| Or
[^|]+:[^|]+\| Match not a pipe one or more times, a colon followed by matching not a pipe one or more times and then match a pipe
Test with multiple lines and combinations
You may have to loop through the string until the end to get the all the matching sub string. Look ahead syntax is not supported in most sql so above regexp might not be suitable for SQL syntax. For you purpose you can do something like creating a table to loop through just to mimic Oracle's level syntax and join with your table containing the string.
With loop_tab as (
Select 1 loop union all
Select 2 union all
select 3 union all
select 4 union all
select 5),
string_tab as(Select 'f-150:aa|ade|f-150:ce|akg|f-150:bb|'::varchar(40) as str)
Select regexp_substr(str,'(f\\-150\\:\\w+\\|)',1,loop)
from string_tab
join loop_tab on 1=1
Output:
regexp_substr
f-150:aa|
f-150:ce|
f-150:bb|
The regex I want to use is: ^(?=.*[,])(,?)ABC(,?)$
What I want to get out is:
^ // start
(?=.*[,]) // contains at least one comma (,)
(,?)ABC(,?) // The comma is either in the beginning or in the end of the string "ABC"
$ // end
Of course ABC is ought to be a variable based on my search term.
So if ABC = 'abc' then ",abc", "abc,", ",abc," will match but not "abc" or "abcd"
Better way to do this is also welcome.
The value in the record looks like "abc,def,ghi,ab,cde..." and I need to find out if it contains my element (i.e. 'abc'). I cannot change the data structure. We can assume that in no case the record will contain only one sub-value, so it is correct to assume that there always is a comma in the value.
If you want to know if a comma delimited string contains abc, then I think like is the easiest method in any database:
where ',' + col + ',' like '%,abc,%'
See the below example
where '1|2|||1|' like '%%|%%|%%|%%|[^3]|' /* it will return true if the string between last two pipe symbols is not like '3'*/
The above code just works fine.
In the same way I need to build a expression for the string between last two pipes not like 10 or 11. Or at least not like 10. So tried like below.
where '1|2|||8|' like '%%|%%|%%|%%|[^10]|'
The above statement return false for 1,0 and 10 instead of just 10. so the above query considers 1 as a separate string and 0 as a separate string.
I'm expecting a expression which will return false only if the string between last two pipe is 10. not for 1 or 0.
I also tried using curly braces. But it behaving differently.
where '1|2|||8|' like '%%|%%|%%|%%|[{^10}]|
Also if you can derive a expression for - string between last two pipe should not be 10 or 11.
Note: I cannot use 'OR' or 'NOT LIKE' in the query.
Any ideas?
You need to check each characters separately
Like "...Not 1 followed by not (0 or 1)"
where '1|2|||8|' like '%%|%%|%%|%%|[^1][^01]|'
I am running a job in MS SQL Server that outputs a text file with white space in between columns. What I'd like to do is specify a specific character sequence between each column as a delimiter.
For example, I'd like the output to look like:
Apple%%%red%%%fruit
banana%%%yellow%%%fruit
onion%%%White%%%veggie
In this example, %%% is the delimiter.
How can I do this?
Assuming that you are using the output file of the job step, and the output file is currently structured something like this:
---- --------
row1 somedata
row2 somedata
You could just concatenate the columns using '+' and fit the percent signs in as appropriate. So the job step definition would contain:
select column1 + '%%%' + column2 from table1;
And the output would look like:
---------------
row1%%%somedata
row2%%%somedata
This assumes that you are OK with concatenating each row of results into a single column. You will need to cast/convert non-character column values for this to work.
My guess is that you are looking for the T-SQL equivalent of Oracle's P/SQL command "set colsep" command. This command lets you alter the delimter of the output. TO make it semicolon, for example, you would call:
set colsep ";"
But in SQL Server... I see the way to do it.
Use the "bcp" utility and you can specify the delimiter and write to your file. He are instructions:
http://msdn.microsoft.com/en-us/library/ms162802.aspx
Look at the -t option to change the separator.