I'm looking to find a way to add a day to a date in both Postgres and SQL Server so I don't have to add an if condition checking which database the server is running
DATEADD(day, 1, STOP_DATE)
doesn't work in PostgreSQL &
STOP_DATE + 1
doesnt work in sql server
Overall, it is not a good idea to try to write SQL code using syntax that is common on both SQL Server and Postgres. You are severely limiting yourself and will sooner or later come across a query that runs too slowly because it doesn't use syntax specific to one of the DBMS.
For example, with your approach you are artificially refusing to use lateral joins, because their syntax is different in Postgres (LATERAL JOIN) and SQL Server (CROSS/OUTER APPLY).
Back to your question.
You can add an integer value to a date value in Postgres and to datetime value in SQL Server.
SQL Server
CREATE TABLE T(d datetime);
INSERT INTO T VALUES ('2020-01-01');
SELECT
d, d+1 AS NextDay
FROM T
http://sqlfiddle.com/#!18/d519d9/1
This will not work with date or datetime2 data types in SQL Server, only datetime.
Postgres
CREATE TABLE T(d date);
INSERT INTO T VALUES ('2020-01-01');
SELECT
d, d+1 AS NextDay
FROM T
http://sqlfiddle.com/#!17/b9670/2
I don't know if it will work with other data types.
Define a function in PostgreSQL that works like the sql server function.
Edit:
can't pass day
Create a function with the same name on each database system that adds a day accordingly.
Related
I have a SQLite database with a simple table in the format of:
ID,DateText,Name
1,2020-09-01T18:57:17Z,John
2,2022-12-01T04:00:09Z,Laurel
...
The DateText column is declared as TEXT on the "create table" statement. Using only SQL, I need to:
Create a new column with the DateText data.
Obtain the "oldest" date
Obtain the "newest" date
Note that I need to resolve this with a SQL query. I cannot read into a programming language, parse, and update table--I need to do everything on SQL. For example, SQL Server DateTime with timezone is the opposite, but they are using node.js, something I cannot do.
You can get the oldest and newest using min() and max():
SELECT ID, min(DateTime), Name FROM YourTable; -- Oldest
SELECT ID, max(DateTime), Name FROM YourTable; -- Newest
The nice thing about ISO-8601 date and time format strings is that they sort lexicographically without having to do anything special with them.
These queries would give an error on most SQL database engines because of the mix of non-grouped columns with an aggregate function, but SQLite explicitly will return the row that goes with the minimum or maximum column value. And of course if you don't want the other columns, just leave them out of the SELECT.
I have a Clients table with the inscriptiondate which is a date. I want to create a function that I give the value inscriptiondate(from my table) and do the value-currdate(). Is it possible?
I'm using SQL server, But I would also like to know how to do in Mysql if its different.
In MySQL you would typically do:
select datediff(curdate(), inscriptiondate) as days_between
In MS SQL, you would typically do:
select datediff(day, inscriptiondate, getdate())
I need to include a date comparison in my query where I compare a date string to a DATETIME column but i need it to work on both ORACLE and SQL Server and not have two separate queries.
Are there any date comparissons which will work on both oracle and sql?
ORACLE:
Select * from MyTable where myDate > DATE '2013-04-10'
SQL Server:
Select * from MyTable where myDate > convert(DATETIME,'2013-04-10', 126)
This portability issue only applies with literals.
I do not believe there is a fully portable way to do this because SQL Server does not support the ANSI literal DATE keyword and all the CAST, CONVERT, TO_DATE, and date functions are not identical on all platforms.
Note that your second query can also be written as Select * from MyTable where myDate > '20130410'.
It would be nice if there was support in SQL Server for the ANSI DATE literal feature (DB/2 and Teradata both have this).
I could not find a Connect item on this, nor anything about why SQL Server doesn't support the ANSI DATE, TIME and TIMESTAMP literal keywords.
I'm wondering in your scenario, whether it would be possible to use a parameter instead?
The solution in Jack's comment Common SQL to compare dates in SQL Server and ORACLE will make this code portable, but you will have to have a non-portable scalar function. This might be a viable option for you - note that in SQL Server, you will need to prefix a scalar function with its schema - this might introduce another wrinkle between the Oracle code and SQL code if you can't make the schemas the same name (note in Oracle, the prefix could be the name of a package instead of a schema if you put the function inside a package)
Is defining your own user-defined functions allowed? You could create a function which abstracts the DBMS specific code and returns a literal 'True' or 'False' result.
[MSSQL User Defined Functions: ]http://msdn.microsoft.com/en-ca/library/ms186755.aspx
[Oracle User Defined Functions]http://ocs.oracle.com/cd/B19306_01/server.102/b14200
Oracle apparently does not allow user defined functions to return a Boolean type, so a string (or numeric) result is required.
I have a SQL Server CE table with a date column. Any given date could have one or more records. My client would like the input form to default to the next date (starting from and including the current date) that doesn't yet have a record. I'm having trouble wrapping my head around a query to accomplish this. Googling I've found a couple snippets, but they all use stored procedures or user defined functions which aren't available in SQL Server CE.
Is there way to do this without creating a loop in the code with multiple database calls?
I can't be sure that SQLCE allows all this syntax, but in T-SQL query like next would work:
select dateadd(d, 1, min(t1.mydatefield))
from mytable t1
left join mytable t2 on datediff(d, t1.mydatefield, t2.mydatefield)=1
where t1.mydatefield>=getdate() and t2.mydatefield is null
How to Insert Only Date and Only time in the table(column datatype is DateTime) using SQL Server 2005.
As far as I know, Date and Time are always stored together. The intern representation of it is a number, that represents timeticks that are passed since a certain moment.
You can of course filter the date info and the time info out of it and store it as for example a string.
Or you can make a userdefined type as shown here : http://weblogs.sqlteam.com/jeffs/archive/2007/10/31/sql-server-2005-date-time-only-data-types.aspx