How to format currency in Filemaker ExecuteSQL istruction? - filemaker

there is a way to format currencies in the result of an ExecuteSQL?
Sql keyword "FORMAT" doesn't work.
the calculation is:
ExecuteSQL ( "
SELECT expenses.user, ' - € ', SUM(expenses.value)
FROM expenses
GROUP BY expenses.user"
;" " ; ¶ ) ;
my output is
Ray - € 10000.1
John - € 44926.97
Tim - € 315.88
I need to get
Ray - € 10.000,10
John - € 44.926,97
Tim - € 315,88

It is (sort of) possible to format a Number field, but not a sum - because an aggregate function cannot be used as an argument to other functions.
However, you could process the query result and format the amounts using Filemaker's native functions. Or (probably simpler) use a summary field instead of ExecuteSQL().

Related

TSQL query to extract a value between to char where a specific set of characters is there

I have a problem I can't seem to figure out. I am trying to extract capacity from a product description. It is always between two values, "," and "oz." however there could be other commas included in the description that are not part of what I'm trying to extract. Example value is , 15 oz., or , 2 oz.,
I'm trying to find values that have the oz in them and are between two commas and I have been completely unsuccessfully. I've tried many things, but here is the latest that I have tried today and I'm just getting an error.
SELECT SUBSTRING(
FullDescription,
CHARINDEX(',', FullDescription),
CHARINDEX('oz.',FullDescription)
- CHARINDEX(',', FullDescription)
+ Len('oz.')
)
from CatalogManagement.Product
Since the backwards pattern ,.zo is more recognisable, I'd go with the REVERSE function
Sample values:
"something, something more, 18oz., complete"
"shorter, 12oz., remainder"
"there is no capacity, in this, value"
"a bit more, 14oz, and some followups, maybe"
SELECT REVERSE(
SUBSTRING (
REVERSE(FullDescription),
CHARINDEX(',.zo', REVERSE(FullDescription)) + 1,
CHARINDEX(',', REVERSE(FullDescription), CHARINDEX(',.zo', REVERSE(FullDescription)) + 1) - CHARINDEX(',.zo', REVERSE(FullDescription)) - 1
)
)
FROM CatalogManagement.Product
WHERE FullDescription LIKE '%oz.,%'
You might use XML-splitting together with a XQuery predicate:
DECLARE #tbl TABLE(ID INT IDENTITY, YourString VARCHAR(MAX));
INSERT INTO #tbl VALUES('Here is one with an amount, 1 oz., some more text')
,('Here is one with no amount, some more text')
,('a, 10 oz.')
,('b, 20oz., no blank between oz and the number')
,('30oz., starts with the pattern, no leading comma');
SELECT t.*
,A.oz.value('.','nvarchar(max)') oz
FROM #tbl t
CROSS APPLY(SELECT CAST('<x>' + REPLACE((SELECT t.YourString AS [*] FOR XML PATH('')),',','</x><x>') + '</x>' AS XML)
.query('/x[contains(text()[1],"oz.")]')) A(oz);
The idea in short:
We use some string methods to replace commas with XML tags and to cast your string to XML. each fragment is placed within a decent <x> element.
We use a predicate to return just the fragments containing "oz.".
You can filter easily with
WHERE LEN(A.oz.value('.','nvarchar(max)'))>0

Date format with Quickpart Database in Word

I try to change the date format in a quickpart database function.
The format is in American (mm/d/yyyy) but i want to change in the French format (dd.MM.yyyy).
This is my code :
DATABASE \d "C:\Users\taagede1\Dropbox\Samaritains\Soldes et
indemnités\2018\Total soldes.xlsx" \c
"Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data
Source=C:\Users\taagede1\Dropbox\Samaritains\Soldes et
indemnités\2018\Total soldes.xlsx;Mode=Read;Extended
Properties=\"HDR=YES;IMEX=1;\";Jet OLEDB:System database=\"\";Jet
OLEDB:Registry Path=\"\";Jet OLEDB:Engine Type=37;Jet OLEDB:Database
Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global
Bulk Transactions=1;Jet OLEDB:New Database Password=\"\";Jet
OLEDB:Create System Database=False;Jet OLEDB:Encrypt
Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet
OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False;Jet
OLEDB:Support Complex Data=False;Jet OLEDB:Bypass UserInfo
Validation=False;Jet OLEDB:Limited DB Caching=False;Jet OLEDB:Bypass
ChoiceField Validation=False" \s "SELECT Quoi, Date , Heure
Début, Heure Fin, Total FROM Engagements$ WHERE ((NomPrenom =
'AubortLoic') AND (Payé IS NULL )) ORDER BY Date" \l "26" \b "191"
\h
This is the result:
I have tried to add this:
{ DATABASE [\# "dd.MM.yyyy"] \* MERGEFORMAT }
But i have a very ugly result (all buggy)
The OLEDB driver for Excel (and Access - it's the same one) supports a limited number of functions that can be used on the data via the Select query, among them Format. It's similar, but not identical to the VBA function of the same name.
In my test the following Select phrase worked (extracted from the Database field code for better visibility):
\s "SELECT Quoi, Format([Date], 'dd.MM.yyyy') AS FrDate, Heure
Début, Heure Fin, Total FROM Engagements$ WHERE ((NomPrenom = 'AubortLoic') AND (Payé IS NULL )) ORDER BY Date
Note that the date format is in single, not double quotes. You can use anything for the alias (the column header), except another field name. So it can't be Date if that's the field name in the data source. It could be Le Date, but in this case, due to the spaces, it would have to be in square brackets: [Le Date].

Padding Fields With White Space

I have the following piece of code in my SELECT statement -
SELECT convert(varchar (24),ra.Reference)
If a result is - R0_2, so 4 characters, how do you go about padding the trailing space (to the right) with the remaining 20 characters to make up 24?
Similar in that if I have a figure of say 18.00 what I want is to add a # to the front, which I know I can achieve with a CONCAT.
However this field I want to be 16 characters and any leading space to be filled with white space, so this example would look like -
'xxxxxxxxxx#18.00' (where x is a blank space)
Thank you for any advice.
One trick you can use is to just concatenate to the string an amount of padding which is guaranteed to fill the missing spaces. For the case of a string 24 characters long, in your first example, we can concatenate 24 spaces to the end of that string. Then, take the first 24 characters from the left, and the resulting string should be right padded by spaces. Similar logic applies to the other case.
First query:
SELECT LEFT(CONVERT(varchar(24), ra.Reference) + ' ', 24)
FROM yourTable
Second query:
SELECT RIGHT(' ' + '#' + CONVERT(varchar(16), ra.TotalValue), 16)
FROM yourTable
You could also use REPLICATE to accurately pad based on the length of text for each cell to ensure it's always 24 characters:
DECLARE #Test1 VARCHAR(24) = 'Test',
#Test2 VARCHAR(24) = 'Longer String'
SELECT CONCAT(#Test1, REPLICATE(N' ', 24 - LEN(#Test1))),
CONCAT(#Test2, REPLICATE(N' ', 24 - LEN(#Test2)))
And for the #....
DECLARE #Number DECIMAL(4,2) = 18.00
SELECT CONCAT(REPLICATE(' ', 15 - LEN(CONVERT(VARCHAR(16), #Number))), '#',#Number)
I used 15 here despite it being 16 characters to account for the addition of the #

How to replace captured group with evaluated expression (adding an integer value to capture group)

I need to convert some strings with this format:
B12F34
to something like that:
Building 12 - Floor 34
but I have to add a value, say 10, to the second capture group so the new string would be as:
Building 12 - Floor 44
I can use this postgres sentence to get almost everything done, but I don't know how to add the value to the second capture group.
SELECT regexp_replace('B12F34', 'B(\d+)F(\d+)', 'Building \1 - Floor \2', 'g');
I have been searching for a way to add a value to \2 but all I have found is that I can use 'E'-modifier, and then \1 and \2 need to be \\1 and \\2:
SELECT regexp_replace('B12F34', 'B(\d+)F(\d+)', E'Building \\1 - Floor \\2', 'g')
I need some sentence like this one:
SELECT regexp_replace('B12F34', 'B(\d+)F(\d+)', E'Building \\1 - Floor \\2+10', 'g')
to get ........ Floor 44 instead of ........ Floor 34+10
You can not do this in regexp alone because regexp does not support math on captured groups even if they are all numeric characters. So you have to get the group that represents the floor number, do the math and splice it back in:
SELECT regexp_replace('B12F34', 'B(\d+)F(\d+)', 'Building \1 - Floor ') ||
((regexp_matches('B12F34', '[0-9]+$'))[1]::int + 10)::text;
Not very efficient because of the two regexp calls. Another option is to just get the two numbers in a sub-query and assemble the string in the main query:
SELECT format('Building %L - Floor %L', m.num[1], (m.num[2])::int + 10)
FROM (
SELECT regexp_matches('B12F34', '[0-9]+', 'g') AS num) m;

How to represent spaces in Perl's DBI properly

I have a record in an Informix table. The table columns look like this:
acct_no integer,
suffix char(1),
meter_num char(20),
date_read datetime year to second not null ,
counter smallint,
reading integer not null ,
typeofread char(1),
estimated char(1),
time_billed datetime year to second
Using Informix's dbaccess tool:
select *
from ws_mtrread
where acct_no = 113091000
and suffix = " "
order by date_read desc;
this result (newest shown) is returned and works whether or not I use one or two spaces for suffix.
acct_no 113091000
suffix
meter_num 34153205
date_read 2013-09-09 23:31:15
counter 0
reading 1240
typeofread g
estimated
time_billed 2013-10-22 11:48:21
However, this Perl DBI query
my $sql_statement =
"select * ".
"from ws_mtrread ".
"where acct_no = ? ".
"and suffix = ? ".
"order by date_read desc ; ";
does not work. I can fetch the row without specifying $suffix, so I know the row exists.
I believe this is an error on my part in representing the suffix. In this example suffix is equal to a string of two spaces.
How do I represent spaces correctly, so the query works? Here is the rest of the code I used to fetch the row.
my $test_acct_no = 113091000;
my $suffix = " ";
my $pt_sel_hdl = $DBHdl->prepare($sql_statement);
$pt_sel_hdl->execute($test_acct_no, $DBHdl->quote($suffix));
my $ws_mtr_read_rec_ref = $pt_sel_hdl->fetchrow_hashref;
After the call, $ws_mtr_read_rec_ref is undefined.
Don't use DBI's quote method here:
$pt_sel_hdl->execute($test_acct_no, $DBHdl->quote($suffix));
When you use ? placeholders in your SQL, the database driver will correctly parameterize the query arguments that you pass to execute. You are probably creating a query that is searching for the literal string " " (including the quotes) when you want to search for (just two spaces.)
So this should be all you need:
$pt_sel_hdl->execute($test_acct_no, $suffix);