I'm trying to find the number of days between two date columns.
I tried to use DATEDIFF but I got an error. What more should I do
Thanks,
Are you sure you are using the correct SQL syntax for your database? Since you're using MySQL, you need to do a
SELECT DATEDIFF('2015-06-05', '2015-08-05');
and the difference is always expressed in days.
On SQL Server you need to specify the unit e.g.
SELECT DATEDIFF(day, '2015-06-05', '2015-08-05');
Related
I'm working on JupyterLab(SQL) and I want to get the difference of days between two columns.
The values of the columns are in the format YYYYMMDD but they aren't integers
How can I transform the columns to dates and then get the differences of days.
I'm not totally sure about JupyterLab, but in SQL Server you can use DATEDIFF() to calculate this. For example:
SELECT DATEDIFF(day, '2017/08/25', '2011/08/25') AS DateDiff;
See also:
https://www.w3schools.com/sql/func_sqlserver_datediff.asp
You did not mention the dbms so i am answering for oracle and postgres. You can use above answers but i like to convert them explicitly befor calculating difference.
so here is for
oracle -
TO_DATE('20170103','YYYYMMDD') - TO_DATE('20200103','YYYYMMDD')
postgres
EXTRACT(DAY FROM TO_TIMESTAMP('20160101', 'YYYYMMDD')-TO_TIMESTAMP('20150301', 'YYYYMMDD')
JupyterLab(SQL) supports SQLite, PostgreSQL, and MySQL databases:
MySQL:
SELECT DATEDIFF("20201005", "20201001");
PostgreSQL:
SELECT DATE_PART('day', AGE('20201005', '20201001'));
I need a way to substract to dates and get the result in days in sql.
I'm using PostgreSQL 8.4
When both columns you want to subtract are of date type, you can just use the - operator.
Take a look at the documentation for date functions, in particular table 9-25.
Check here the documentation of PostgreSQl...
postgresql.org/docs/8.4
I copied it from this code from this website:
age(timestamp '2001-04-10', timestamp '1957-06-13')
hope it will help you...
I have a where clause where I am trying to get a date within a certain range with parameters like so,
(AL.INSERTED_DATE BETWEEN (:begindate) AND (:enddate))
The problem is that I need to get six months before the begin date but I get an error, ORA-00904: "DATEADD": invalid identifier, when I try,
(AL.INSERTED_DATE BETWEEN DATEADD(Month,-6,(:begindate)) AND (:enddate))
Can anybody point me out to what I could be doing wrong?
You're not using SQL Server, you're using Oracle - that's why it's giving you an error in Oracle format.
http://psoug.org/definition/ADD_MONTHS.htm
ADD_MONTHS would probably be the best equivalent to what you're trying to do here -
(AL.INSERTED_DATE BETWEEN ADD_MONTHS((:begindate),-6) AND (:enddate))
Check this solution:
Oracle (10g) equivalent of DATEADD(weekday, -3, GETDATE())
I have a query in which I want to select data from a column where the data is a date. The problem is that the data is a mix of text and dates.
This bit of SQL only returns the longest text field:
SELECT MAX(field_value)
Where the date does occur, it is always in the format xx/xx/xxxx
I'm trying to select the most recent date.
I'm using MS SQL.
Can anyone help?
Try this using ISDATE and CONVERT:
SELECT MAX(CONVERT(DateTime, MaybeDate))
FROM (
SELECT MaybeDate
FROM MyTable
WHERE ISDATE(MaybeDate) = 1) T
You could also use MAX(CAST(MaybeDate AS DateTime)). I got in the (maybe bad?) habit of using CONVERT years ago and have stuck with it.
To do this without a conversion error:
select max(case when isdate(col) = 1 then cast(col as date) end) -- or use convert()
from . . .
The SQL statement does not specify the order of operations. So, even including a where clause in a subquery will not guarantee that only dates get converted. In fact, the SQL Server optimizer is "smart" enough to do the conversion when the data is brought in and then do the filtering afterwards.
The only operation that guarantees sequencing of operations is the case statement, and there are even exceptions to that.
Another solution would be using PATINDEX in WHERE clause.
SELECT PATINDEX('[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]', field_value)
Problem with this approach is you really are not sure if something is date (e.g. 99/99/9999 is not date).
And problem with IS_DATE is it depends on configuration (e.g. DATEFORMAT).
So, use an appropriate option.
i using MySQL Query for my task.
And I interested using Date and time function.
can i use DAY(), WEEK(), and YEAR() at one query?
SELECT Object
FROM table
WHERE DAY(date) BETWEEN 1 AND 7
GROUP BY WEEK(date, 1), YEAR(date)
i want do this bcoz i'm worry if sometimes my program have an error because of the date setting and not recognize some date.please give me an input.
Yes, you can use them all in a single query.
The only disadvantage I can think of is that using any of the DAY, WEEK or YEAR functions won't be able to use the index on the column the function is applied to, assuming one is present.
If you're having issues relating to date formatting, you should get familiar with:
DATE_FORMAT
STR_TO_DATE