Hi
This is not a bug. Field must have value in declared type. If you provide different type it must be converted first before is posted to the engine.
Consider in any programing language with typed variables. How can you store string into integer variabe?
Same is with Timestamp.
If you pass string it must be converted first before pass into engine which require field in declared type.
In the trigger you can only manipulate value in field type range.
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/5c871d3c-e2ac-45cc-ac4e-30b5bda58ed3n%40googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/firebird-support/4DnR7R15dGzlgNms%40smtp.poczta.onet.pl.
Hi!
You won’t be able to alter varchar to timestamp.
I suggest you create a view that contains all the required fields in the table plus one varchar for the timestamp value as string.
Create a before insert trigger on view and you can write your business logic inside this trigger and there you can fill the table’s fields correctly.
András
To view this discussion on the web, visit https://groups.google.com/d/msgid/firebird-support/CACnB7Ryk__eSoCD9dYTSyOreftyTxJpG4%2B%3DkgPqJqer2GZcCvQ%40mail.gmail.com.
Hello Akira,
You may try this approach as well :
CREATE FUNCTION F_TIMESTAMP_OR_NULL(
VAL VARCHAR(1000))
RETURNS TIMESTAMP
AS
DECLARE ANYVAR TIMESTAMP;
BEGIN
BEGIN
BEGIN
ANYVAR = CAST(VAL AS TIMESTAMP);
END
WHEN ANY DO
BEGIN
RETURN NULL;
END
END
RETURN ANYVAR;
END;
CREATE TABLE TestTable (C TIMESTAMP);
SET TERM ^ ;
INSERT INTO TestTable (C) VALUES ( F_TIMESTAMP_OR_NULL(''));
INSERT INTO TestTable (C) VALUES ( F_TIMESTAMP_OR_NULL( CAST ('1/1/1970' AS TIMESTAMP)));
INSERT INTO TestTable (C) VALUES ( F_TIMESTAMP_OR_NULL( 'SOME_STRING'));
INSERT INTO TestTable (C) VALUES ( CURRENT_TIMESTAMP );
SELECT * FROM TestTable;
Regards,
Vlad
From: firebird...@googlegroups.com <firebird...@googlegroups.com> On Behalf Of Akira Kawaguchi
Sent: Saturday, February 27, 2021 9:14 AM
To: firebird...@googlegroups.com
To view this discussion on the web, visit https://groups.google.com/d/msgid/firebird-support/CACnB7Ryk__eSoCD9dYTSyOreftyTxJpG4%2B%3DkgPqJqer2GZcCvQ%40mail.gmail.com.
--
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/d525ba17-1d03-d78b-e9bc-a93d88f04c66%40lawinegevaar.nl.
Good concept but function should not catch any convert error to null only from empty string
CREATE FUNCTION F_TIMESTAMP_OR_NULL(
VAL VARCHAR(1000))
RETURNS TIMESTAMP
AS
DECLARE ANYVAR TIMESTAMP;
BEGIN
IF (VAL=’’) THEN
ANYVAR = NULL; ELSE
ANYVAR = CAST(VAL AS TIMESTAMP);
RETURN ANYVAR;
END;
regards,
Karol Bieniaszewski
To view this discussion on the web, visit https://groups.google.com/d/msgid/firebird-support/000001d70cde%24b81e5e70%24285b1b50%24%40softwiz.ro.
Of course. In this particular case is correct.
It can be written like this also:
CREATE OR ALTER FUNCTION F_TIMESTAMP_OR_NULL( VAL VARCHAR(24))
RETURNS TIMESTAMP
AS
BEGIN
RETURN CAST( NULLIF( VAL, '') AS TIMESTAMP );
END
In this case:
INSERT INTO TestTable (C) VALUES ( F_TIMESTAMP_OR_NULL('X')); --NOT PASSES
INSERT INTO TestTable (C) VALUES ( F_TIMESTAMP_OR_NULL('')); -- PASSES FALLBACK TO NULL
INSERT INTO TestTable (C) VALUES ( F_TIMESTAMP_OR_NULL( CAST ('1/1/1970' AS TIMESTAMP))); --PASSES
INSERT INTO TestTable (C) VALUES ( F_TIMESTAMP_OR_NULL( CURRENT_TIMESTAMP ) ); -- PASSES
Regards,
Vlad
To view this discussion on the web, visit https://groups.google.com/d/msgid/firebird-support/4DnjZM4qYzzmKW%40smtp.poczta.onet.pl.
Or just NULLIF, no function at all …
From: firebird...@googlegroups.com <firebird...@googlegroups.com> On Behalf Of Karol Bieniaszewski
Sent: Saturday, February 27, 2021 12:35 PM
To: firebird...@googlegroups.com
To view this discussion on the web, visit https://groups.google.com/d/msgid/firebird-support/4DnjZM4qYzzmKW%40smtp.poczta.onet.pl.
The easiest way would be then:
--- NOT SO SAFE ONE LINER ---
INSERT INTO TestTable (C) VALUES( NULLIF( CAST( YOUR_INSERT_VALUE AS VARCHAR(24)) , '' ));
---E.G.
INSERT INTO TestTable (C) VALUES( NULLIF( CAST( '' AS VARCHAR(24)) , '' )); --ok
INSERT INTO TestTable (C) VALUES( NULLIF( CAST( '1/1/1970 00:00:00' AS VARCHAR(24)) , '' )); --ok
INSERT INTO TestTable (C) VALUES( NULLIF( CAST( CURRENT_TIMESTAMP AS VARCHAR(24)) , '' )); --ok
INSERT INTO TestTable (C) VALUES( NULLIF( CAST( 'X' AS VARCHAR(24)) , '' )); -- error
IMO I would opt for the function as there would be a better control for any transformation. But depends of course on each use case.
Regards,
Vlad
To view this discussion on the web, visit https://groups.google.com/d/msgid/firebird-support/000501d70cfe%249258ab30%24b70a0190%24%40softwiz.ro.
To view this discussion on the web, visit https://groups.google.com/d/msgid/firebird-support/000e01d70d03%24fa915350%24efb3f9f0%24%40softwiz.ro.
Hi!
Here is my suggested solution. I think you misunderstood a little bit. The trick is TIMES_AS_VARCHAR field.
CREATE TABLE TEST_TABLE (
ID INTEGER,
TIMES TIMESTAMP
);
CREATE OR ALTER VIEW TEST_VIEW(
ID,
TIMES,
TIMES_AS_VARCHAR)
AS
select id, times, cast(null as varchar(40)) from test_table;
CREATE OR ALTER TRIGGER TEST_VIEW_BI0 FOR TEST_VIEW
ACTIVE BEFORE INSERT POSITION 0
as
declare variable ts type of column test_table.times;
begin
begin
ts = cast(new.times_as_varchar as timestamp);
when any do
begin
ts = null;
end
end
insert into test_table(id, times) values (new.id, :ts);
end
Test it:
insert into test_view(id, times_as_varchar) values (1, null);
insert into test_view(id, times_as_varchar) values (2, '');
insert into test_view(id, times_as_varchar) values (3, '2021.02.28');
insert into test_view(id, times_as_varchar) values (4, '2021.02.28 19:42:00');
commit;
select * from test_table order by id
Result:
ID TIMES
1 <NULL>
2 <NULL>
3 28.02.2021 00:00
4 28.02.2021 19:42
select * from test_view order by id
ID TIMES TIMES_AS_VARCHAR
1 <NULL> <NULL>
2 <NULL> <NULL>
3 28.02.2021 00:00 <NULL>
4 28.02.2021 19:42 <NULL>
András
To view this discussion on the web, visit https://groups.google.com/d/msgid/firebird-support/CACnB7RzoMKoFkJ9NwVnCsSHTqBKV%2Bx7xvyYuvjKHXFt7v4DL3w%40mail.gmail.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/firebird-support/f40e64ace36f430aa1705c8efdbbeaa3%40mail3.mve.hu.
Hi!
To avoid unnecessary disk writing. (If that matters.)
András
CÉGÜNK A LIBRA CSOPORT TAGJA |
OMACHT ANDRÁS
|
To view this discussion on the web, visit https://groups.google.com/d/msgid/firebird-support/CAKC2LgzdwcXnmFUFUTC8siRByCO06A3nL4Ym%2BVwF-toQaYKmKQ%40mail.gmail.com.
--
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/427098e6-380f-78c6-bcad-49ebbc6ae0a1%40ibphoenix.com.
--
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/18eec187-c11f-0d3d-bb06-308f1d0fcc21%40ibphoenix.com.