Avatar of lcor
lcor

asked on 

SQL Number of days


Given a date, is there a function that can calculate the number of days from that date?

Using Oracle 10g.  Can be Oracle function or plain SQL.
DatabasesOracle DatabaseSQL

Avatar of undefined
Last Comment
Sean Stuber
ASKER CERTIFIED SOLUTION
Avatar of Sean Stuber
Sean Stuber

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
Avatar of psmoreira
psmoreira

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of lcor
lcor

ASKER

psmoreira, I like your idea..this way this can be done from a select statement that needs other values as well rather then a separate select form dual.

Can I make your code as a function and use it as follows?

select NumOfDaysFrom(TO_DATE(date_from_MyTable, 'yyyy-mm-dd')), column_from_MyTable from MyTable
Avatar of psmoreira
psmoreira

Yes
>>needs other values as well rather then a separate select form dual

The others do not need the select from dual.  We just used dual to show how subtracting days works.

You can select from any table:

Select sysdate-TO_DATE(date_from_MyTable, 'yyyy-mm-dd') from your_table;

No need to over complicate it with a function.
Avatar of lcor
lcor

ASKER

Thanks, good point.

I hard-coded date_from_MyTable as '2008-05-12'  ..it works great.

I went to use the date from the table and I started to have problems.  The number of days is way too high, like 728,000 for the above date.  The format of date from table is 2008-05-12 00:00:00.  TO_DATE doesn't seem to like this format.  Any ideas anyone?
Avatar of lcor
lcor

ASKER

figured it out...needed to do a to_char with date from table....
Avatar of Sean Stuber
Sean Stuber

>> to_char with date from table.


no,  if your column in the table is actually a date, you just leave it alone  using to_char is definitely NOT the right approach.  
Think about it,  to_char converts a date into a string.  A string obviously doesn't help you do date math,  you need a date.


select  sysdate - your_date_column from your_table
Oracle Database
Oracle Database

Oracle is an object-relational database management system. It supports a large number of languages and application development frameworks. Its primary languages are SQL, PL/SQL and Java, but it also includes support for C and C++. Oracle also has its own enterprise modules and application server software.

81K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo