A legacy app sends a sql insert query with a date using the spanish format (dd-mm-yyyy)
I need to change that to english format (mm-dd-yyyy) using a trigger before insert.
How I do this? My database is SQL Server 2000.
Instead of using a trigger, consider just changing the Language property of the Login used by the application to one with the proper locale.
Example:
SET LANGUAGE ENGLISH
-- Query:
SELECT CONVERT(DATETIME, '9/6/91')
-- Result:
-- 1991-09-06 00:00:00.000
GO
SET LANGUAGE SPANISH
-- Query:
SELECT CONVERT(DATETIME, '9/6/91')
-- Result:
-- 1991-06-09 00:00:00.000
Edit: For SQL Server 2000, see the BOL Page for configuration. The LANGUAGE option can also be set in ODBC.
Related
I have a postgres timescale database running in docker. For the purposes of api testing I want SELECT NOW() to return lets say 2010-12-01 23:00:44.851242 +00:00. Basically every time I start up the container I want it to think the current date is some time in December 2010.
How can I achieve this? I cant seem to find any command to set current time in postgres. Do I need to change the system time in the docker container before the database shows up? Is that even something I can do?
You can achieve this by creating a custom now() function in a separate schema and then adjusting the search_path to prefer that function over the builtin now function:
CREATE SCHEMA test;
CREATE OR REPLACE FUNCTION test.now() RETURNS timestamptz LANGUAGE SQL AS $$ SELECT '2000-01-01 0:00'::timestamptz; $$;
SET search_path TO test,pg_catalog,public;
-- make search_path change permanent for a specific user
ALTER USER <testuser> SET search_path TO test,pg_catalog,public;
SELECT now();
now
------------------------
2000-01-01 00:00:00+01
(1 row)
Time: 1.826 ms
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.
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.
I'm currently writing some installer script that fires SQL files against different database types depending on the system's configuration (the webapplication supports multiple database server like MySQL, MSSQL and PostgreSQL).
One of those types is PostgreSQL. I'm not fluent with it and I would like to know if it's possible to make a statement into a define/populate SQL file that makes an SQL query conditional to a specific PostgreSQL server version.
How to make an SQL statement conditionally in plain PGSQL so that it is only executed in version 9? The command is:
ALTER DATABASE dbname SET bytea_output='escape';
The version check is to compare the version with 9.
Postgres does have version() function, however there is no major_vesion(). Assuming that output string always includes version number as number(s).number(s).number(s) you could write your own wrapper as:
CREATE OR REPLACE FUNCTION major_version() RETURNS smallint
AS $BODY$
SELECT substring(version() from $$(\d+)\.\d+\.\d+$$)::smallint;
$BODY$ LANGUAGE SQL;
Example:
=> Select major_version();
major_version
---------------
9
(1 row)
However real issue here is that AFAIK you can't execute your commands conditionally in "pure" SQL and best what you can do is to write some stored function like this:
CREATE OR REPLACE FUNCTION conditionalInvoke() RETURNS void
AS $BODY$
BEGIN
IF major_version() = 9 THEN
ALTER DATABASE postgres SET bytea_output='escape';
END IF;
RETURN;
END;
$BODY$ LANGUAGE plpgsql;
I think that you should rather use some scripting language and generate appropriate SQL with it.
Or you could just use
select setting from pg_settings where name = 'server_version'
Or
select setting from pg_settings where name = 'server_version_num'
If you need major version only
select Substr(setting, 1, 1) from pg_settings where name = 'server_version_num'
or
select Substr(setting, 1, strpos(setting, '.')-1) from pg_settings where name = 'server_version'
if you want it to be compatible with two digit versions.
Maybe you could make things dependent on the output of
select version();
(probably you'll have to trim and substring that a bit)
BTW (some) DDL statements may not be issued from within functions; maybe you'll have to escape to shell-programming and here-documents.
In Oracle, I can SELECT sysdate FROM dual to get the current system date/time.
How can I do the same accessing a FileMaker database via the JDBC driver?
Note: "Don't use FileMaker" is unfortunately not an option.
Create an unstored calc field that calls Get ( CurrentHostTimeStamp ) and then include that field as one of your selected fields.
(Make sure you configure the field's options so that FileMaker does not store the calculation results - unless you want the date/time of when the field was added...)
I don't know FileMaker, but maybe the standard (ANSI SQL) expressions CURRENT_DATE or CURRENT_TIMESTAMP work?
So something like
SELECT CURRENT_TIMESTAMP
FROM whatever
Edit:
Another thing to try could be the standard JDBC escape function:
SELECT {fn CURTIME()}
FROM whatever