I am new to Firebird. I come from a SQL Server background.
To get current date I use following query in SQL Server
SELECT GETDATE()
I am looking for something similar in Firebird
select 'Now' from rdb$database
-- returns 'Now'
select cast('Now' as date) from rdb$database
-- returns e.g. 2008-08-13
select cast('now' as time) from rdb$database
-- returns e.g. 14:20:19.6170
select cast('NOW' as timestamp) from rdb$database
-- returns e.g. 2008-08-13 14:20:19.6170
Shorthand syntax for the last three statements:
select date 'Now' from rdb$database
select time 'now' from rdb$database
select timestamp 'NOW' from rdb$database
In addition to the string-based solution in the answer of stackoverflow, Firebird supports the SQL standard functions CURRENT_DATE, CURRENT_TIME, and CURRENT_TIMESTAMP (and with Firebird 2.5.9 and Firebird 3.0.5 in anticipation of introduction of time zones in Firebird 4: LOCALTIME and LOCALTIMESTAMP). The Firebird documentation sometimes refers to them as 'context variables'.
There is a difference between 'now' and the CURRENT_xxx functions: the CURRENT_xxx when used in PSQL code will be stable (same value) for the duration of execution of the outermost routine, while 'now' will be evaluated individually.
Related
Is there a way to change the &_DATE variable in SQL Developer?
Whenever I run the following code, it just shows todays date:
DEFINE RUN_DATE = &_DATE
I'd like to find a way to format the query so it finds the last day in the month.
Also, if possible, to change the format to show as YYMMDD?
SQL> select '&_DATE' from dual;
old 1: select '&_DATE' from dual
new 1: select '23-JUN-20' from dual
'23-JUN-2
---------
23-JUN-20
SQL> alter session set NLS_DATE_FORMAT='YYYYMMDD';
Session altered.
SQL> select '&_DATE' from dual;
old 1: select '&_DATE' from dual
new 1: select '20200623' from dual
'2020062
--------
20200623
SQL>
_DATE is defined to store result of the SYSDATE function in the database. It's output is based on the NLS_DATE_FORMAT setting for your session.
Or in SQL Developer -
But, you want the last day of the month...we'll, we have the LAST_DAY() function for you!
select LAST_DAY('&_DATE')
from DUAL;
Returns '20200630' because we submitted _DATE which returns June 23 2020, so the function returns the last day of June 2020.
In the official documentation, both functions have the same description:
Current date and time (start of current transaction)
Is there a difference between the two functions, and if not, why do both exist? Thanks.
now and transaction_timestamp are equivalent to the SQL standard current_timestamp. All report the start time of the transaction.
In terms of transactions, there are two timestamps to think of. the start of the transaction & the time each individual statement is executed. Conceptually, there is also the end time of the transaction which one can get by running a select statement_timestamp() trx_end_timestamp at the very end, just before the commit / rollback.
If you run the following in psql [copy the whole line & paste into psql shell]
BEGIN; SELECT PG_SLEEP(5); SELECT NOW(), CURRENT_TIMESTAMP, TRANSACTION_TIMESTAMP(), STATEMENT_TIMESTAMP(); COMMIT;
I got this output:
now | current_timestamp | transaction_timestamp | statement_timestamp
-------------------------------+-------------------------------+-------------------------------+-------------------------------
2019-04-23 11:15:18.676855-04 | 2019-04-23 11:15:18.676855-04 | 2019-04-23 11:15:18.676855-04 | 2019-04-23 11:15:23.713275-04
(1 row)
You can see clearly that NOW, CURRENT_TIMESTAMP, TRANSACTION_TIMESTAMP are equivalent, and STATEMENT_TIMESTAMP has a 5 second offset because we slept for 5 seconds.
BTW, CURRENT_TIMESTAMP is in the sql standard. The others are postgresql specific, though other databases may also implement them
The answer is in the doc your mention:
now() is a traditional PostgreSQL equivalent to
transaction_timestamp().
So, they are the same, and they are here for historical / backward compatibility, and some could argue for the simplicity of the function name.
This question already has answers here:
in postgres, can you set the default formatting for a timestamp, by session or globally?
(3 answers)
Closed 6 years ago.
INSERT into Group (Name,CreatedDate) VALUES ('Test',UTC_TIMESTAMP(), 1);
This is the query I have used for mysql to insert current date time.
When I am using this in postgresql, I am getting below error.
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
********** Error **********
ERROR: function utc_timestamp() does not exist
SQL state: 42883
I have tried like below using now(), however it is inserting like "2016-07-07 17:01:18.410677". I need to insert in 'yyyymmdd hh:mi:ss tt' format.
INSERT into Group (Name,CreatedDate) VALUES ('Test',UTC_TIMESTAMP(), 1);
How to insert current date time in insert query of postgresql in above format ?
timestamp (or date or time columns) do NOT have "a format".
Any formatting you see is applied by the SQL client you are using.
To insert the current time use current_timestamp as documented in the manual:
INSERT into "Group" (name,createddate)
VALUES ('Test', current_timestamp);
To display that value in a different format change the configuration of your SQL client or format the value when SELECTing the data:
select name, to_char(createddate, 'yyyymmdd hh:mi:ss tt') as created_date
from "Group"
For psql (the default command line client) you can configure the display format through the configuration parameter DateStyle: https://www.postgresql.org/docs/current/static/runtime-config-client.html#GUC-DATESTYLE
For current datetime, you can use now() function in postgresql insert query.
You can also refer following link.
insert statement in postgres for data type timestamp without time zone NOT NULL,.
You can of course format the result of current_timestamp().
Please have a look at the various formatting functions in the official documentation.
Do you have any idea on how to have a getdate() function in EnterpriseDB PostgreSQL? I upgraded to EDB-PSQL, and when I try to restore old data from the free PSQL, it returns error on some tables since there is no getdate().
I believe this should automatically be created upon creating new database? But it didn't. :( Only a now() function.
Can I create the function instead? Help!
If getdate() is like now() (as with SQL Server) you can simply say
create function public.getdate() returns timestamptz
stable language sql as 'select now()';
I am using Perl's DBD::ODBC to connect to an Oracle database. However, an issue arises when I try to execute a select query using a date in the where clause. It seems this issue occurs because of the database's date format being DD-MON-RR (see DBD::ODBC::FAQ). Since I cannot change the database's settings, can anyone suggest a workaround?
The database's default date format only matters if you depend on it, which you should not in general. You can:
1) Specify the format of the date in your query:
select *
from news
where news_date = to_date ('01-DEC-2009','DD-MON-RRRR');
2) Use the ANSI standard for date literals:
select *
from news
where news_date = DATE '2009-12-01';
One option is to use the TO_DATE() function (or the ANSI 'DATE' keyword) to convert the format in every query:
WHERE date_field > TO_DATE('2009-11-01', 'YYYY-MM-DD');
-- or
WHERE date_field > DATE '2009-11-01'
If you have to do this a lot, a better option would be to set the format for the session:
$dbh->do("ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'");
$dbh->do("ALTER SESSION SET NLS_TIMESTAMP_FORMAT='YYYY-MM-DD HH24:MI:SSxFF'");
$dbh->do("ALTER SESSION SET NLS_TIMESTAMP_TZ_FORMAT='YYYY-MM-DD HH24:MI:SSxFF'");
Then:
my $sth = $dbh->prepare(<<EOT);
SELECT date_field
FROM some_table
WHERE date_field > '2009-11-01'
EOT
Don't rely on implicit datatype conversion. You can always specify the date format in the where clause:
WHERE your_column = to_date(:your_parameter, 'yyyy/mm/dd')