Python 3 - MySQL Database Access - The Python standard for database interfaces is the Python DB-API. Most Python database interfaces adhere to this standard. SQLEditor is a user-oriented and streamlined macOS utility that makes it simple and straightforward to edit or design an SQL database using a Mac. The SQLEditor app allows you to quickly get started working on your own databases with the help of a vast array of design tools that can be used to quickly add. RazorSQL is an SQL query tool, database browser, SQL editor, and database administration tool for Windows, macOS, Mac OS X, Linux, and Solaris. RazorSQL has been tested on over 40 databases, can connect to databases via either JDBC or ODBC, and includes support for the following databases.
DATE and TIME values in PostgreSQL have a whole special set of functions and operators for their proper use. So many queries deal with DATE and TIME information that it’s important to get to know the date tools. Below we’ll cover and practice the main functions you’ll likely need. If you want to get detailed you can checkout the full list of PostgreSQL functions here.
DATE/TIME Datatypes
There are 4 main ways to store date values in a PostgreSQL database:
Data Type | Description | Example | Output |
---|---|---|---|
TIMESTAMP | date and time | TIMESTAMP '2020-09-08 23:36:34' | 2020-09-08T23:36:34 |
DATE | date (no time) | DATE '2020-09-08 23:36:34' | 2020-09-08 |
TIME | time (no day) | TIME '2020-09-08 23:36:34' | 23:36:34 |
INTERVAL | interval between two date/times | INTERVAL '1 day 2 hours 10 seconds' | 1 day, 2:00:10 |
We’ll go over more about each of these.
Date String Formatting
Dates in a database aren’t stored as strings, but we input and fetch data from it as if it were a string with the following format for the information:
where the letters stand for Year, Month, Day, Hour, Minutes and Seconds. Let’s say for example that we want to record that we got a new user on September 08, 2020 at exactly 23:36. To represent that exact date and time we would use the format:
2020-09-08 23:36:00
TODO: this format is also supported: January 8 04:05:06 1999 PST
To get some familiarity try creating and SELECTing a few TIMESTAMPS below. I was born on May 1st, 1983 at exactly 4:00am. Can you fetch that timestamp?
We’re just going to jump in here. We need to use a different table as none of the previous ones we’ve been using have had date fields in them. Another table available to us in chinook is employees. Let’s get familiar with what columns are in this table by looking at the first few rows. Note that there are several columns so you may have to scroll right to see all of the data:
Each employee has two TIMESTAMP columns, one for their birth_date and one for their hire_date. You can use all of the ORDERing, GROUPing and other functions we learned for other columns on DATE columns as well. Try getting a list of the 4 youngest employees in the company.
Formatting Dates to Strings
Often you don’t want to show the full raw TIMESTAMP, but rather a nicely formatted, potentially truncated version. For example, let’s say we want to get a list of the employees names and the year that they were hired. To do so we’ll need to parse the hired_date to just pull out the year. We can do so with the TO_CHAR function which works as follows
where [date type] is a column or value of any of the above listed date/time data types, and [pattern] is a string indicating how to format the output date. The main symbols you’ll want to use to create your format patterns are here
Pattern | Description | Example | Output |
---|---|---|---|
HH | Hour (01-12) | TO_CHAR(TIME '4:15 pm', 'HH') | 04 |
HH24 | Hour (01-24) | TO_CHAR(TIME '4:15 pm', 'HH24') | 16 |
MI | Minute | TO_CHAR(TIME '4:15 pm', 'MI') | 15 |
SS | Seconds | TO_CHAR(TIME '4:15:23 pm', 'SS') | 23 |
am | displays whether time is am or pm | TO_CHAR(TIME '4:15 pm', 'am') | am |
YY | last 2 digits of the Year | TO_CHAR(DATE '2020-09-08', 'YY') | 20 |
YYYY | 4 digits of the Year | TO_CHAR(DATE '2020-09-08', 'YY') | 2020 |
MM | Month # of the year. | TO_CHAR(DATE '2020-09-08', 'MM') | 09 |
Month | written Month of the year capitalized | TO_CHAR(DATE '2020-09-08', 'Month') | September |
Mon | abbreviated of Month of year | TO_CHAR(DATE '2020-09-08', 'Mon') | Sep |
DD | Day # of the month | TO_CHAR(DATE '2020-09-08', 'DD') | 08 |
Day | written Day of the week | TO_CHAR(DATE '2020-09-08', 'Day') | Tuesday |
Dy | abbreviated Day of the week | TO_CHAR(DATE '2020-09-08', 'Dy') | Tue |
WW | Week # of the year | TO_CHAR(DATE '2020-09-08', 'WW') | 37 |
Q | Quarter of the year | TO_CHAR(DATE '2020-09-08', 'Q') | 3 |
TZ | TimeZone | TO_CHAR(DATE '2020-09-08', 'TZ') | UTC |
Sql Editor 3 6 2013
The above patterns can be string together to get the format you eventually want. Some common outputs are:
and
Sql Editor 3 6 2010
and
You don’t have to memorize these (it’s hard to!). It’s just good to get familiar with how it works and then reference back to it when you need it in the future.
Number formatting
There are a couple of extra tools you can use on patterns that output numbers.
Formatter | Description | Example | Output |
---|---|---|---|
FM | Fill Mode will remove any 0’s at the front of a 2 digit number. | TO_CHAR(DATE '2020-09-05', 'FMDD') | 5 |
th | adds the ordinal suffixes like st, nd or th to the end of a number | TO_CHAR(DATE '2020-09-05', 'FMDD') | 05th |
And of course you can combine the two to get
String Formatting
For string outputs, most of the patterns above support different casing output based on the case you use for the pattern. Some examples using different casings of “Day”:
Sql Editor 3 6 2007
And you can see the following common date format in UPPERCASE, Capitalized and lowercase formats:
Note that the case for numeric values doesn’t change. Still use DD for the day # of the month and YYYY for year.
We’re going to move on in the tutorial but if you’d like more details checkout the full list of PostgreSQL date formatting functions .
Current DATE and TIME Functions
PostgreSQL supports a number of special values, or functions to help bet the current DATE, TIMESTAMP or TIME. The most used ones are
and they are used by just putting them in the query
GROUPing BY DATE
In analytic queries, it’s very common to group things by dates. Isoftphone 4 1202 download free. For example you may want to see new users by year, month, week or day. To do so, you’ll want to use the TO_CHAR function to convert the dates into a truncated string before you GROUP BY it. You don’t want to simply GROUP BY the raw date as those are accurate down to the millisecond so grouping by the unaltered date would be like making GROUPs for each millisecond.
The following examples are using the hire_date field from the employees table and show a lot of common formats you can use for these groups. These are what we use at Chartio for our date group formatting standards.
Group Period | Example SQL | Example Output |
Second | TO_CHAR(hire_date, 'YYYY-MM-DD'T'HH24':'MI:SS') | 2018-03-04T00:00:00 |
Minute | TO_CHAR(hire_date, 'YYYY-MM-DD'T'HH24':'MI') | 2018-08-14T00:00 |
Hour | TO_CHAR(hire_date, 'YYYY-MM-DD'T'HH24') | 2018-01-02T00 |
Day | TO_CHAR(hire_date, 'YYYY-MM-DD') | 2003-10-17 |
Week | TO_CHAR(hire_date, 'IYYY'-W'IW') | 2002-W33 |
Month | TO_CHAR(hire_date, 'YYYY-MM') | 2002-05 |
Quarter | TO_CHAR(hire_date, 'YYYY'-Q'Q') | 2003-Q2 |
Year | TO_CHAR(hire_date, 'Y'YYYY') | Y2012 |
Hour of Day | TO_CHAR(hire_date, 'HH24') | 14 |
Day of Week | TO_CHAR(hire_date, 'FMDay') | Thursday |
Day of Month | TO_CHAR(hire_date, 'DD') | 17 |
Day of Year | TO_CHAR(hire_date, 'DDD') | 125 |
Month of Year | TO_CHAR(hire_date, 'FMMonth') | October |
Feel free to try out any of the above formats on the query below:
There are only 8 employees in our database so we’re not dealing with too many groups there. You can get a little more granular with the invoices table and it’s invoice_date column with 250 rows.
The above query returns the number of invoices created per year. Can you modify it to get a SUM of the total amount invoiced by month?
Sql Editor 3 6 20
Written by: Dave Fowler
Reviewed by: Matt David Tumbleweed express download free.
Reviewed by: Matt David Tumbleweed express download free.