Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Wendy Calderon
Wendy Calderon

Posted on

     

SQL: Timestamps

TheTIMESTAMP data type holds values that contain both date and time, fixed at 19 characters. The format of aTIMESTAMP is 'YYYY-MM-DD HH:MM:SS'

InDBMSs such as MySQL,DATETIME is a supported data type that, just likeTIMESTAMP, is used to hold date and time values, but they're not entirely the same for the following reasons:

  • Size:DATETIME requires a few more bytes for data storing thanTIMESTAMP.
  • As specified in the MySQL documentation, MySQL convertsTIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval, which does not occur for other types such asDATETIME. This is particularly convenient when working with different time zones.
  • Different supported ranges: '1000-01-01 00:00:00' to '9999-12-31 23:59:59' forDATETIME, '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC forTIMESTAMP.

Retrieving data fromTIMESTAMP:
In order to fetch information from aTIMESTAMP object, we can use theextract() function, for which we specify the field fromTIMESTAMP we want. Examples of this field would be:

Timestamp fields examples

Let's consider the following table to work on some examples:

Table for timestamp examples

All payments were made in november, so let's grab these bills along with the days they were paid:

Timestamp query

We add the AS statement to name the resulting column from using EXTRACT.

Timestamp query results

If our bills table was larger, including payments to these bills all throughout the year, we could implement extract() and an aggregate function (SUM) to show, for example, the total amount of money paid in bills by month, with a query similar to this one:

Another timestamp query

The larger our databases, the more ways available to retrieve info!
Have fun!

Top comments(4)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
douglasfugazi profile image
Douglas Fugazi
Free thinker
  • Location
    Medellín
  • Education
    MSc Technology Management
  • Work
    QA Engineer
  • Joined

Super nice explanation. Thanks for sharing Wendy.

CollapseExpand
 
wendisha profile image
Wendy Calderon
#WomanInTech with a passion for exploring, learning and utilizing technology for the common good. Software Engineer at @followLantern
  • Location
    NY
  • Joined

Glad you liked it! 😊

CollapseExpand
 
maeve70 profile image
LJ
Sociologist at heart, data nerd. Always learning. I want to use my powers for good :)
  • Location
    NYC
  • Education
    MA-Sociology
  • Joined

Thank you. This makes more sense than any documentation I've read elsewhere. :)

CollapseExpand
 
wendisha profile image
Wendy Calderon
#WomanInTech with a passion for exploring, learning and utilizing technology for the common good. Software Engineer at @followLantern
  • Location
    NY
  • Joined

Sorry for the laaaate reply! Glad you found it useful!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

#WomanInTech with a passion for exploring, learning and utilizing technology for the common good. Software Engineer at @followLantern
  • Location
    NY
  • Joined

More fromWendy Calderon

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp