In PostgreSQL, the INTERVAL type is used to store a span of time. It is versatile and allows for the representation of time differences in terms of years, months, days, hours, minutes, and seconds.
The INTERVAL type can be used to add or subtract time from DATE or TIMESTAMP values. It can be defined using the INTERVAL keyword followed by a string indicating the time span.
Here is a basic example of using the INTERVAL type in a query:
SELECT NOW() + INTERVAL '1 year 2 months 3 days';
The INTERVAL value can be customized to fit different requirements for date manipulation.
Console Output:
2024-12-28 14:45:00
The INTERVAL type can be used to perform arithmetic operations with DATE values, allowing for easy manipulation of date-based data.
Add 10 days to a specific date using the INTERVAL type:
SELECT DATE '2023-01-01' + INTERVAL '10 days';
The result will be a new date that reflects the addition of the specified interval.
Console Output:
2023-01-11
INTERVAL can also be used to subtract time spans from TIMESTAMP values, which is useful for calculating past dates and times.
Subtract 3 months from a specific timestamp:
SELECT TIMESTAMP '2023-06-15 10:00:00' - INTERVAL '3 months';
The result will be a new timestamp reflecting the subtraction of the specified interval.
Console Output:
2023-03-15 10:00:00
PostgreSQL allows combining multiple INTERVALs to create complex time spans, which can be useful for advanced date calculations.
Combine 1 year, 2 months, and 10 days into a single INTERVAL:
SELECT INTERVAL '1 year' + INTERVAL '2 months' + INTERVAL '10 days';
The combined INTERVAL can then be used in further date or timestamp calculations.
Console Output:
1 year 2 mons 10 days
The EXTRACT function can be used with INTERVAL to retrieve specific components like years, months, or days from a time span.
Extract the number of days from an INTERVAL:
SELECT EXTRACT(DAY FROM INTERVAL '1 year 2 months 10 days');
The EXTRACT function can be applied to other components such as months or years.
Console Output:
10
PostgreSQL supports arithmetic operations directly on INTERVALs, allowing for addition, subtraction, multiplication, and division.
Multiply an INTERVAL by a factor to scale the time span:
SELECT INTERVAL '10 days' * 2;
The resulting INTERVAL will be scaled accordingly, in this case doubling the number of days.
Console Output:
20 days
In PostgreSQL, INTERVALs can be cast to other types such as strings, allowing for flexible data manipulation and formatting.
Cast an INTERVAL to a string to format the output:
SELECT CAST(INTERVAL '1 year 2 months' AS VARCHAR);
The resulting string can be used in applications where text formatting is required.
Console Output:
1 year 2 mons
PostgreSQL provides various formatting options for INTERVALs to customize their display according to user preferences.
Format an INTERVAL to display in a custom format:
SELECT TO_CHAR(INTERVAL '1 year 2 months 3 days', 'YYYY "years" MM "months" DD "days"');
The output can be tailored to meet specific display requirements, enhancing readability.
Console Output:
1 years 02 months 03 days
INTERVALs can be passed as arguments to functions, enabling dynamic date and time calculations based on input parameters.
Create a function that takes an INTERVAL and adds it to the current date:
CREATE FUNCTION add_interval_to_now(interval_value INTERVAL) RETURNS TIMESTAMP AS $$
BEGIN
RETURN NOW() + interval_value;
END;
$$ LANGUAGE plpgsql;
SELECT add_interval_to_now(INTERVAL '5 days');
The function dynamically calculates a future date by adding the INTERVAL to the current date.
Console Output:
2023-10-23 14:45:00
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies