Firebird and ChatGPT...

131 views
Skip to first unread message

Adrián Avila

unread,
Aug 26, 2023, 9:21:04 PM8/26/23
to firebird-support
I asked to ChatGPT to create a function that formats a number with 2 decimals.

Here is the result, and yes, it works.

SET TERM ^ ;

CREATE OR ALTER FUNCTION FormatPositiveNumber(numPart VARCHAR(50)) RETURNS VARCHAR(50)
AS
DECLARE VARIABLE formatted VARCHAR(50);
DECLARE VARIABLE i INTEGER;
BEGIN
    formatted = '';
    i = 0;
   
    WHILE (i < CHAR_LENGTH(numPart)) DO
    BEGIN
        formatted = formatted || SUBSTRING(numPart FROM i + 1 FOR 1);
       
        IF (MOD(i + 1, 3) = 0 AND i <> CHAR_LENGTH(numPart) - 1) THEN
            formatted = formatted || ',';
       
        i = i + 1;
    END

    RETURN REVERSE(formatted);
END^

CREATE OR ALTER FUNCTION FormatNumber(num DECIMAL(18, 3)) RETURNS VARCHAR(50)
AS
DECLARE VARIABLE numberString VARCHAR(50);
DECLARE VARIABLE formattedString VARCHAR(50);
DECLARE VARIABLE integerPart VARCHAR(50);
DECLARE VARIABLE decimalPart VARCHAR(50);
DECLARE VARIABLE reversedInteger VARCHAR(50);
DECLARE VARIABLE formattedInteger VARCHAR(50);
DECLARE VARIABLE formattedDecimal VARCHAR(50);
DECLARE VARIABLE i INTEGER;
BEGIN
    num = ROUND(2);
    numberString = CAST(num AS VARCHAR(50));
    integerPart = SUBSTRING(numberString FROM 1 FOR POSITION('.' IN numberString) - 1);
    decimalPart = SUBSTRING(numberString FROM POSITION('.' IN numberString) + 1);
   
    IF (integerPart STARTING WITH '-') THEN
    BEGIN
        reversedInteger = REVERSE(SUBSTRING(integerPart FROM 2));
        formattedInteger = '-' || FormatPositiveNumber(reversedInteger);
    END
    ELSE
    BEGIN
        reversedInteger = REVERSE(integerPart);
        formattedInteger = FormatPositiveNumber(reversedInteger);
    END
   
    formattedDecimal = CASE WHEN CHAR_LENGTH(decimalPart) > 0 THEN '.' || LEFT(decimalPart || '00', 2) ELSE '' END;

    RETURN formattedInteger || formattedDecimal;
END^


SET TERM ; ^

Karol Bieniaszewski

unread,
Aug 27, 2023, 5:17:33 AM8/27/23
to firebird...@googlegroups.com

Do you mean it compiles not works?

There is one error

 

num = ROUND(2);

 

but simple fixable

 

num = ROUND(num, 2);

 

 

Regards,

Karol Bieniaszewski

--
You received this message because you are subscribed to the Google Groups "firebird-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebird-suppo...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/firebird-support/4cf2db17-f4fb-4f90-b6f2-eca6ed505ab6n%40googlegroups.com.

 

Mário Reis

unread,
Aug 31, 2023, 8:20:18 PM8/31/23
to firebird-support
It won't work at all. num is decimal round( needs  a numerical expression...
ROUND (<number> [, <scale>]) <number> ::= a numerical expression <scale> ::= an integer specifying the number of decimal places toward which should be rounded, e.g.: 2 for rounding to the nearest multiple of 0.01 1 for rounding to the nearest multiple of 0.1 0 for rounding to the nearest whole number -1 for rounding to the nearest multiple of 10 -2 for rounding to the nearest multiple of 100

liviuslivius

unread,
Sep 1, 2023, 3:26:43 AM9/1/23
to firebird...@googlegroups.com
Can you explain more what do you mean here?

Regards,
Karol Bieniaszewski


-------- Oryginalna wiadomość --------
Od: Mário Reis <mariod...@gmail.com>
Data: 31.08.2023 19:41 (GMT+01:00)
Do: firebird-support <firebird...@googlegroups.com>
Temat: Re: [firebird-support] Firebird and ChatGPT...

Mark Rotteveel

unread,
Sep 1, 2023, 4:36:42 AM9/1/23
to firebird...@googlegroups.com
On 31-08-2023 19:41, Mário Reis wrote:
> It won't work at all. num is decimal round( needs a numerical expression...

A decimal variable *is* a numerical expression.

Mark
--
Mark Rotteveel

Mário Reis

unread,
Sep 1, 2023, 10:17:55 AM9/1/23
to firebird-support
Sorry! Yes it work's know. I had to drop down the:
DECLARE EXTERNAL FUNCTION "ROUND"
  INTEGER BY DESCRIPTOR,
  INTEGER BY DESCRIPTOR
RETURNS PARAMETER 2
ENTRY_POINT 'fbround' MODULE_NAME 'fbudf';
Then it is working.

Dimitry Sibiryakov

unread,
Sep 1, 2023, 10:21:44 AM9/1/23
to firebird...@googlegroups.com
Mário Reis wrote 01.09.2023 16:17:
> Sorry! Yes it work's know. I had to drop down the:
> DECLARE EXTERNAL FUNCTION "ROUND"
>   INTEGER BY DESCRIPTOR,
>   INTEGER BY DESCRIPTOR
> RETURNS PARAMETER 2
> ENTRY_POINT 'fbround' MODULE_NAME 'fbudf';
> Then it is working.

Unfortunately. This code deserves to be posted to thedaylywtf.com.

--
WBR, SD.

Mark Rotteveel

unread,
Sep 1, 2023, 10:22:19 AM9/1/23
to firebird...@googlegroups.com
On 01-09-2023 16:17, Mário Reis wrote:
> Sorry! Yes it work's know. I had to drop down the:
> DECLARE EXTERNAL FUNCTION "ROUND"
>   INTEGER BY DESCRIPTOR,
>   INTEGER BY DESCRIPTOR
> RETURNS PARAMETER 2
> ENTRY_POINT 'fbround' MODULE_NAME 'fbudf';
> Then it is working.

What ancient Firebird version are you using that it doesn't have the
ROUND built-in function? It was introduced in Firebird 2.1 in 2008 as a
built-in function, removing the need to use a UDF.

Mark
--
Mark Rotteveel

Mário Reis

unread,
Sep 4, 2023, 12:00:30 PM9/4/23
to firebird-support
Well! Sorry but sometimes it does not ! 
I did the same. Asked to create checkdigit of IBAN. I beleave  it must be near but doesn't even compile!
The ChatGpt Generated souce:
function CalculateIBANDigit(const IBAN: string): Integer;
var
  IBANWithoutCheckDigit: string;  // IBAN Without CheckDigit!?!?!? I doubt it (*)
  NumericIBAN: string;
  i: Integer;
  CurrentChar: Char;
begin

  // Remove o dígito de verificação do IBAN
  IBANWithoutCheckDigit := Copy(IBAN, 1, Length(IBAN) - 1);

  // Move os quatro primeiros caracteres para o final do IBAN
  IBANWithoutCheckDigit := IBANWithoutCheckDigit + Copy(IBAN, 1, 4);

  // Substitui as letras do IBAN por números (A=10, B=11, etc.)
  NumericIBAN := '';
  for i := 1 to Length(IBANWithoutCheckDigit) do
  begin
    CurrentChar := IBANWithoutCheckDigit[i];
    if CharInSet(CurrentChar, ['A'..'Z']) then
      NumericIBAN := NumericIBAN + IntToStr(Ord(CurrentChar) - 55)
    else
      NumericIBAN := NumericIBAN + CurrentChar;
  end;

  // Calcula o módulo 97 do número obtido
  Result := StrToInt(NumericIBAN) mod 97;        // Is not a Valid Integer?! 
end;

function ValidateIBAN(const IBAN: string): Boolean;
begin
  Result := CalculateIBANDigit(IBAN) = 1;
end;
(*) https://www.swissdelphicenter.ch/en/showcode.php?id=1470 this work's pefect

Dimitry Sibiryakov

unread,
Sep 4, 2023, 12:03:12 PM9/4/23
to firebird...@googlegroups.com
Mário Reis wrote 04.09.2023 18:00:
> I did the same. Asked to create checkdigit of IBAN. I beleave  it must be near
> but doesn't even compile!
> The ChatGpt Generated souce:

It is not even Firebird's PSQL.

--
WBR, SD.

Karol Bieniaszewski

unread,
Sep 4, 2023, 1:17:32 PM9/4/23
to firebird...@googlegroups.com

You probably asked wrongly. Fix your prompt for Firebird SQL and also to do not use mod on full result number as it is „wrong” algorithm.

Even int128 cannot handle such division 😉

 

Regards,

Karol Bieniaszewski

--

You received this message because you are subscribed to the Google Groups "firebird-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebird-suppo...@googlegroups.com.

Karol Bieniaszewski

unread,
Sep 4, 2023, 1:53:20 PM9/4/23
to firebird...@googlegroups.com

Sorry i was wrong, int 128 should be ok here…

 

Regards,

Karol Bieniaszewski

 

Od: Karol Bieniaszewski
Wysłano: poniedziałek, 4 września 2023 19:17
Do: firebird...@googlegroups.com
Temat: ODP: [firebird-support] Re: Firebird and ChatGPT...

 

You probably asked wrongly. Fix your prompt for Firebird SQL and also to do not use mod on full result number as it is „wrong” algorithm.

Even int128 cannot handle such division 😉

 

Regards,

Karol Bieniaszewski

 

Od: Mário Reis
Wysłano: poniedziałek, 4 września 2023 18:00
Do: firebird-support
Temat: [firebird-support] Re: Firebird and ChatGPT...

 

Well! Sorry but sometimes it does not ! 

--

Mark Rotteveel

unread,
Sep 5, 2023, 3:21:18 AM9/5/23
to firebird...@googlegroups.com
On 04-09-2023 18:00, Mário Reis wrote:
> Well! Sorry but sometimes it does not !
> I did the same. Asked to create checkdigit of IBAN. I beleave  it must
> be near but doesn't even compile!
> The ChatGpt Generated souce:

ChatGPT does not know how to program, it just generates text using a
statistical language model based on the inputs it was trained on. You
cannot trust anything it generates.

Mark
--
Mark Rotteveel

Karol Bieniaszewski

unread,
Sep 6, 2023, 3:28:41 AM9/6/23
to firebird...@googlegroups.com

It is only half true, especially GTP version 4 is much more 😉

 

Regards,

Karol Bieniaszewski

--

You received this message because you are subscribed to the Google Groups "firebird-support" group.

To unsubscribe from this group and stop receiving emails from it, send an email to firebird-suppo...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages