Computer Science

SQL Datetime Value

A SQL Datetime Value is a data type used in SQL databases to store date and time information. It includes both the date and time components, and can be used for a variety of purposes such as tracking events or scheduling tasks. The format for a SQL Datetime Value is typically YYYY-MM-DD HH:MM:SS.

Written by Perlego with AI-assistance

5 Key excerpts on "SQL Datetime Value"

  • Book cover image for: Joe Celko's SQL for Smarties
    eBook - ePub

    Joe Celko's SQL for Smarties

    Advanced SQL Programming

    SQL only supports what are called nonsequenced operations. Standard SQL has a very complete description of its temporal data types. There are rules for converting from numeric and character strings into these data types and there is a schema table for global time-zone information that is used to make sure that temporal data types are synchronized. It is so complete and elaborate that smaller SQLs have not implemented it yet. As an international standard, Standard SQL has to handle time for the whole world, and most of us work with only local time. If you have ever tried to figure out the time in a foreign city to place a telephone call, you have some idea of what is involved.
    The common terms and conventions related to time are also confusing. We talk about “an hour” and use the term to mean a particular point within the cycle of a day (“The train arrives at 13:00 hrs”) or to mean an interval of time not connected to another unit of measurement (“The train takes three hours to get there”). The number of days in a month is not uniform; the number of days in a year is not uniform; weeks are not easily related to months; and so on.
    Standard SQL has two basic kinds of temporal data types. The datetimes (DATE , TIME , and TIMESTAMP ) represent points in the time line, and the interval data types and INTERVAL s (DAY , HOUR , MINUTE , and SECOND with decimal fraction) are durations of time. Standard SQL also has a full set of operators for these data types. But you will still find vendor-specific syntax in most existing SQL implementations today.

    11.2.1 Internal Representations

    The syntax and power of date, timestamp, and time features vary so much from product to product that it is impossible to give anything but general advice. This chapter will assume that you have simple date arithmetic in your SQL, but you might find that some library functions would let you do a better job than what you see here. Please continue to check your manuals until the Standard SQL standards are implemented.
  • Book cover image for: SQL Interview Questions
    eBook - ePub

    SQL Interview Questions

    A complete question bank to crack your ANN SQL interview with real-time examples

    hh:mm:ss:nnnn . The date ranges between 0001-01-01 and 9999-12-31, and the time ranges between 00-00-00 and 23-59-59.
    • YYYY is the year and it ranges from 0001 to 9999.
    • MM is the month and it ranges from 1 to 12 (from January to December).
    • DD is the day and it ranges from 1 to 31.
    • hh is the hour unit and it ranges between 00 and 23.
    • mm is the minute unit and it ranges between 00 and 59.
    • ss is the seconds and it ranges between 00 and 59.
    • nn is the nano-seconds and it ranges between 00 and 999.
    Basically, time is stored as string, where the max value is 19-23 characters (YYYY-MM-DD hh:mm: ss) The storage capacity for time datatype is fixed, which is 8 bytes, and if you leave this field empty, then the default value is 1900-01-01 00:00:00.
    When you want to store both date and time value in a single column, then you can use this datatype.
    Real-time example: If you want to store student's birthday with date and time, then you can define a column with datetime datatype.
    If you want more options in seconds precision the you can use datetimeoffset datatype and to support different time zones, you can use datetimeoffset .

    Miscellaneous SQL datatypes

    The previous sections of datatypes covered most of the type of datatypes used to store in SQL, but apart from that we also have a few more datatypes that can be used to store different values. Go through the following datatypes for more details:
    • xml: This datatype was introduced in version SQL 2008, and as the name suggests, this datatype is used to store xml instance in column. The data size of the XML should not be more than 2 GB. Here XML datatype has some rule while storing data:
      • There should be one or more nodes
      • There must be only one root element
      • You can create optional schema if needed
      • You can store contents or document as XML
      When you want to store XML data in the form of document or contents then you can use this datatype.
  • Book cover image for: Joe Celko's SQL for Smarties
    eBook - ePub

    Joe Celko's SQL for Smarties

    Advanced SQL Programming

    Standard SQL has a very complete description of its temporal data types. There are rules for converting from numeric and character strings into these data types, and there is a schema table for global time-zone information that is used to make sure temporal data types are synchronized. It is so complete and elaborate that nobody has implemented it yet—and it will take them years to do so! Because it is an international standard, Standard SQL has to handle time for the whole world, and most of us work with only local time. If you have ever tried to figure out the time in a foreign city before placing a telephone call, you have some idea of what is involved.
    The common terms and conventions related to time are also confusing. We talk about “an hour” and use the term to mean a particular point within the cycle of a day (“The train arrives at 13:00”) or to mean an interval of time not connected to another unit of measurement (“The train takes three hours to get there”); the number of days in a month is not uniform; the number of days in a year is not uniform; weeks are not related to months; and so on.
    All SQL implementations have a DATE data type; most have a separate TIME and a TIMESTAMP data type. These values are drawn from the system clock and are therefore local to the host machine. They are based on what is now called the Common Era calendar, which many people would still call the Gregorian or Christian calendar.
    Standard SQL has a set of date and time (DATE, TIME, and TIMESTAMP) and INTERVAL (DAY, HOUR, MINUTE, and SECOND, with decimal fraction) data types. Both of these groups are temporal data types, but datetimes represent points in the time line, while the interval data types are durations of time. Standard SQL also has a full set of operators for these data types. The full syntax and functionality have not yet been implemented in any SQL product, but you can use some of the vendor extensions to get around a lot of problems in most existing SQL implementations today.
  • Book cover image for: Joe Celko's Data, Measurements and Standards in SQL
    There should also be a function that returns the current date from the system clock. This function has a different name with each vendor: TODAY, SYSDATE, and getdate() are dialect for the correct CURRENT_DATE or CURRENT_TIME_STAMP. There may also be a function to return the day of the week from a date, which is sometimes called DOW() or WEEKDAY(). Standard SQL provides for CURRENT_DATE, CURRENT_TIME [(<time precision>)] and CURRENT_TIMESTAMP [(<timestamp precision>)] functions, which are self-explanatory.

    41.4. The Nature of Temporal Data Models

    Temporal data is pervasive. It has been estimated that 1 of every 50 lines of database application code involves a date or time value. Data warehouses are by definition time-varying: Ralph Kimball states that every data warehouse has a time dimension. Often the time-oriented nature of the data is what lends it value.
    Integers, decimal numbers, strings, Boolean values, and most other data that we put into a computer are discrete. Time and real numbers are is a continuum. In a continuum, a value is not always known exactly. Consider the number pi (π); you can approximate it with a real number (3.141592653…) or a rational number (22/7), but you cannot express it exactly.
    When we give a date, say “2009-12-31,” it is really a duration that starts at exactly “2009-12-31 00:00:00” but it ends just before “2010-01-01 00:00:00.” This is called a half-open interval in mathematics.
    We are stuck with expressing the end of that day to whatever precision we have in my machine—say “2009-12-31 23:59:59.999999999” if we have nanoseconds in the hardware. Technically, we cannot use “2009-12-31 24:00:00” since that time does not exist in the ISO standards and in many SQL products it would “round” to “2010-01-01 00:00:00” anyway.
    MySQL uses a proprietary notation for periods of years and year-months, so they can be expressed in a single column. They fill in the day or year field with “00” so that “2009-01-00” is the entire month of January 2009 and likewise “2009-00-00” is the entire year of 2009. Most other SQL products have to use {start_time, end_time} pairs that declare the end_time to be NULL-able so that an ongoing state of affairs can be modeled. You use the expression COALESCE (end_time, CURRENT_TIMESTAMP) in VIEWS or queries for the current situation, or COALESCE (end_time, ≪dead line timestamp≫) when there is a required ending time.
  • Book cover image for: SQL: 1999
    eBook - PDF

    SQL: 1999

    Understanding Relational Language Components

    • Jim Melton, Alan R. Simon(Authors)
    • 2001(Publication Date)
    • Morgan Kaufmann
      (Publisher)
    Unfortunately, SQL: 1999's date capability cannot handle B.C.E. or B.C.(before the Common Era or before Christ) dates due to a lack of general agreement over issues such as how to handle the year O, how far back to go (the pyramids? dinosaurs? Big Bang?), and the like. We believe that many products will support such dates, but they may do so in a variety of ways. 2.4 SQL Data Types 39 The length of a DATE is said to be 10 positions; in this way, SQL:1999 does not appear to prescribe---or proscribe--implementations that use character strings, packed decimal, or any other form internally. You specify a date data type by simply using DATE. 9 TIME:This variation stores the hour, minute, and second values of a time. The hour value is exactly 2 digits and can represent the hours O0 through 23. The minutes value is also exactly 2 digits and can represent O0 through 59. The seconds value is again 2 digits, but a fractional value is optional. The seconds value is restricted to O0 through 61.999 .... Why 61 instead of the expected 59? Simply because of the phenomenon known as leap seconds: occasionally, the earth's official timekeepers will add one or two seconds to a minute to keep clocks synchronized with sidereal time. Support for leap seconds is implementation-defined. The maximum number of fractional digits is defined by your implementa- tion, but the standard requires that it support at least 6 digits. If you don't specify a number when you are defining your data types, you'll get no frac- tional digits. The length of a TIME value is 8 positions; if there are any fractional digits, then the length is 9 plus the number of fractional digits. (The extra position is to account for the period that separates the integer seconds value from the fractional digits.) You specify a time data type by either specifying TIME or by specifying TIME(p). The value of p must not be negative, of course, and the maximum value is determined by your implementation.
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.