简体   繁体   中英

Store date range in a single column in Oracle SQL

Here trip 1 involves 2 activity_code in a single day and also concludes in a single day and most other activities are just single day but i have one trip that span over more than one day.

What could be the best possible way to store date range for that column that span more than one days.

Splitting the column into multiple begin date and end date just doesn't make sense as there would be many blank columns?

trip_id(pk,fk)  Activity_code(pk,fk)          date
1                     a1                     1st October 2015
1                     a2                     1st October 2015
2                     a3                     2nd -5th October 2015

Keep in mind that i need to search the activity_code on basis of month. such as list all the activity code that occur in October ?

Is it possible to insert a range of date in a single column or any other design solution ?

Is there any datatype that can represent the date range in single value ?

PS: oracle 11g e

Store the date ranges as FirstDate / LastDate or FirstDate / Duration .

This allows you to store the values in the native format for dates. Storing dates as strings is a bad, bad idea, because strings don't have all the built-in functionality provided for native date types.

Don't worry about the additional storage for a second date or duration. In fact, the two columns together are probably smaller than storing the value as a string.

Splitting the date into start date and end date would be ideal. Storing dates as strings is not recommended. If you store your dates as strings then there is a possibility of malformed data being stored in the column since a VARCHAR2 column will allow any value. You will have to build strong validations in your script while inserting the data which is unnecessary.

Secondly, you will not be able to perform simple operations like calculating the duration/length of the trip easily if both the start_date and end_date are stored in the same column. If they are stored in different columns it would be as simple as

SELECT trip_id, activity_code, end_date - start_date FROM trips;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM