a guide to mysql_A2-04-05.MySQL DATA TYPES-A Complete Guide To MySQL DATETIME Data Type

Summary: in this tutorial, you will learn about MySQL DATETIME data type and how to use some handy functions for manipulating DATETIME effectively.

Introduction to MySQL DATETIME data type

You use MySQL DATETIME to store a value that contains both date and time. When you query data from a DATETIME column, MySQL displays the DATETIME value in the following format:

1

YYYY-MM-DDHH:MM:SS

By default, DATETIME values range from 1000-01-01 00:00:00 to 9999-12-31 23:59:59.

A DATETIME value uses 5 bytes for storage. In addition, a DATETIME value can include a trailing fractional second up to microseconds with the format YYYY-MM-DD HH:MM:SS[.fraction] e.g., 2015-12-20 10:01:00.999999. When including the fractional second precision, DATETIME values require more storage as illustrated in the following table:

Fractional Seconds PrecisionStorage (Bytes)

0

0

1, 2

1

3, 4

2

5, 6

3

For example, 2015-12-20 10:01:00.999999requires 8 bytes, 5  bytes for 2015-12-20 10:01:00 and 3 bytes for .999999 while 2015-12-20 10:01:00.9  requires only 6 bytes, 1 byte for the fractional second precision.

Note that before MySQL 5.6.4, DATETIME values requires 8 bytes storage instead of 5 bytes.

MySQL DATETIME vs. TIMESTAMP

MySQL provides another temporal data type that is similar to the DATETIME called

The TIMESTAMP requires 4 bytes while DATETIME requires 5 bytes. Both TIMESTAMP and DATETIMErequire additional bytes for fractional seconds precision.

TIMESTAMP values range from 1970-01-01 00:00:01 UTC to 2038-01-19 03:14:07 UTC. If you want to store temporal values that are beyond 2038, you should use DATETIME instead of TIMESTAMP.

MySQL stores TIMESTAMP in UTC value. However, MySQL stores the DATETIME value as is without timezone. Let’s see the following example.

First, set the timezone of the current connection to +00:00.

1

SETtime_zone='+00:00';

Next, create a table named timestamp_n_datetime that consists of two columns: ts and dt with TIMESTAMP and DATETIME types using the following statement.

1

2

3

4

5

CREATETABLEtimestamp_n_datetime(

idINTAUTO_INCREMENTPRIMARY KEY,

tsTIMESTAMP,

dtDATETIME

);

Then, insert the current date and time into both ts and dt columns of the timestamp_n_datetime table,

1

2

INSERTINTOtimestamp_n_datetime(ts,dt)

VALUES(NOW(),NOW());

After that, query data from the timestamp_n_datetime table.

1

2

3

4

5

SELECT

ts,

dt

FROM

timestamp_n_datetime;

MySQL-DATETIME-vs-TIMESTAMP.jpg

Both values in DATETIME and TIMESTAMP columns are the same.

Finally, set the connection’s time zone to +03:00 and query data from the timestamp_n_datetime table again.

1

2

3

4

5

6

7

SETtime_zone='+03:00';

SELECT

ts,

dt

FROM

timestamp_n_datetime;

MySQL-DATETIME-vs-TIMESTAMP-timezone-changes.jpg

As you can see, the value in the TIMESTAMP column is different. This is because the TIMESTAMP column stores the date and time value in UTC when we changed the time zone, the value of the TIMESTAMPcolumn is adjusted according to the new time zone.

It means that if you use the TIMESTAMP data to store date and time values, you should take a serious consideration when you move your database to a server located in a different time zone.

MySQL DATETIME functions

The following statement sets the variable @dt to the current date and time using the

1

SET@dt=NOW();

To query the value of the @dt variable, you use the following SELECT statement:

1

SELECT@dt;

MySQL-DATETIME-NOW-function.png

MySQL DATE function

To extract the date portion from a DATETIME value, you use the DATE function as follows:

1

SELECTDATE(@dt);

MySQL-DATETIME-DATE-function-example.png

This function is very useful in case you want to query data based on a date but the data stored in the column is based on both date and time.

Let’s see the following example.

1

2

3

4

5

6

7

CREATETABLEtest_dt(

idINTAUTO_INCREMENTPRIMARY KEY,

created_atDATETIME

);

INSERTINTOtest_dt(created_at)

VALUES('2015-11-05 14:29:36');

Suppose you want to know which row created on 2015-11-05, you use the following query:

1

2

3

4

5

6

SELECT

*

FROM

test_dt

WHERE

created_at='2015-11-05';

It returns no rows.

This is because the created_at column contains not only date but also time. To correct it, you use the DATE function as follows:

1

2

3

4

5

6

SELECT

*

FROM

test_dt

WHERE

DATE(created_at)='2015-11-05';

MySQL-DATETIME-DATE-function.png

It returns one row as expected. In case the table has many rows, MySQL has to perform a full table scan to locate the rows that match the condition.

MySQL TIME function

To extract the time portion from a DATETIME value, you use the TIME function as the following statement:

1

SELECTTIME(@dt);

MySQL-DATETIME-TIME-function.png

MySQL YEAR, QUARTER, MONTH, WEEK, DAY, HOUR, MINUTE and SECOND functions

To get the year, quarter, month, week, day, hour, minute, and second from a DATETIME value, you use the functions as shown in the following statement:

1

2

3

4

5

6

7

8

9

SELECT

HOUR(@dt),

MINUTE(@dt),

SECOND(@dt),

DAY(@dt),

WEEK(@dt),

MONTH(@dt),

QUARTER(@dt),

YEAR(@dt);

MySQL-DATETIME-datetime-functions.png

MySQL DATE_FORMAT function

To format a DATETIME value, you use the DATETIME value based on the %H:%i:%s - %W %M %Y format:

1

SELECTDATE_FORMAT(@dt,'%H:%i:%s - %W %M %Y');

MySQL-DATETIME-DATE_FORMAT-function.png

MySQL DATE_ADD function

To add an intervalto a DATETIME value, you use DATE_ADD function as follows:

1

2

3

4

5

6

7

8

SELECT@dtstart,

DATE_ADD(@dt,INTERVAL1SECOND)'1 second later',

DATE_ADD(@dt,INTERVAL1MINUTE)'1 minute later',

DATE_ADD(@dt,INTERVAL1HOUR)'1 hour later',

DATE_ADD(@dt,INTERVAL1DAY)'1 day later',

DATE_ADD(@dt,INTERVAL1WEEK)'1 week later',

DATE_ADD(@dt,INTERVAL1MONTH)'1 month later',

DATE_ADD(@dt,INTERVAL1YEAR)'1 year later';

MySQL-DATETIME-DATE_ADD-function.png

MySQL DATE_SUB function

To subtract an interval from a DATETIME value, you use DATE_SUB function as follows:

1

2

3

4

5

6

7

8

SELECT@dtstart,

DATE_SUB(@dt,INTERVAL1SECOND)'1 second before',

DATE_SUB(@dt,INTERVAL1MINUTE)'1 minute before',

DATE_SUB(@dt,INTERVAL1HOUR)'1 hour before',

DATE_SUB(@dt,INTERVAL1DAY)'1 day before',

DATE_SUB(@dt,INTERVAL1WEEK)'1 week before',

DATE_SUB(@dt,INTERVAL1MONTH)'1 month before',

DATE_SUB(@dt,INTERVAL1YEAR)'1 year before';

MySQL-DATETIME-DATE_SUB-function.png

MySQL DATE_DIFF function

To calculate a difference in days between two DATETIME values, you use the DATEDIFF function only considers the date part of a DATETIME value in the calculation.

See the following example.

First, create a table named datediff_test that has one column whose data type is DATETIME.

1

2

3

CREATETABLEdatediff_test(

dtDATETIME

);

Second, insert some rows into the datediff_test table.

1

2

3

4

5

6

7

8

INSERTINTOdatediff_test(dt)

VALUES('2010-04-30 07:27:39'),

('2010-05-17 22:52:21'),

('2010-05-18 01:19:10'),

('2010-05-22 14:17:16'),

('2010-05-26 03:26:56'),

('2010-06-10 04:44:38'),

('2010-06-13 13:55:53');

Third, use the DATEDIFF function to compare the current date and time with the value in each row of the datediff_test table.

1

2

3

4

5

SELECT

dt,

DATEDIFF(NOW(),dt)

FROM

datediff_test;

MySQL-DATETIME-DATEDIFF-Example.png

In this tutorial, you have learned about MySQL DATETIME data type and some useful DATETIME functions.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SELECT PIS.SHOW_FLT_DETAIL AS SHOW_FLT_DETAIL -- new , PIS.SHOW_AWB_DETAIL AS SHOW_AWB_DETAIL -- new , PIS.DISPLAY_AIRLINE_CODE AS CARRIER_CODE , DECODE(PIS.REVERT_FLOW,'N',PIS.FLOW_TYPE,DECODE(PIS.FLOW_TYPE,'I','E','I')) AS FLOW_TYPE , PIS.SHIP_TO_LOCATION AS SHIP_TO_LOCATION , PIS.INVOICE_SEQUENCE AS INVOICE_SEQUENCE , PFT.FLIGHT_DATE AS FLIGHT_DATE , PFT.FLIGHT_CARRIER_CODE AS FLIGHT_CARRIER_CODE , PFT.FLIGHT_SERIAL_NUMBER AS FLIGHT_SERIAL_NUMBER , PFT.FLOW_TYPE AS AIRCRAFT_FLOW , FAST.AIRCRAFT_SERVICE_TYPE AS AIRCRAFT_SERVICE_TYPE , PPT.AWB_NUMBER AS AWB_NUMBER , PPT.WEIGHT AS WEIGHT , PPT.CARGO_HANDLING_OPERATOR AS CARGO_HANDLING_OPERATOR , PPT.SHIPMENT_PACKING_TYPE AS SHIPMENT_PACKING_TYPE , PPT.SHIPMENT_FLOW_TYPE AS SHIPMENT_FLOW_TYPE , PPT.SHIPMENT_BUILD_TYPE AS SHIPMENT_BUILD_TYPE , PPT.SHIPMENT_CARGO_TYPE AS SHIPMENT_CARGO_TYPE , PPT.REVENUE_TYPE AS REVENUE_TYPE , PFT.JV_FLIGHT_CARRIER_CODE AS JV_FLIGHT_CARRIER_CODE , PPT.PORT_TONNAGE_UID AS PORT_TONNAGE_UID , PPT.AWB_UID AS AWB_UID , PIS.INVOICE_SEPARATION_UID AS INVOICE_SEPARATION_UID , PFT.FLIGHT_TONNAGE_UID AS FLIGHT_TONNAGE_UID FROM PN_FLT_TONNAGES PFT , FZ_AIRLINES FA , PN_TONNAGE_FLT_PORTS PTFP , PN_PORT_TONNAGES PPT , FF_AIRCRAFT_SERVICE_TYPES FAST , SR_PN_INVOICE_SEPARATIONS PIS --new , SR_PN_INVOICE_SEP_DETAILS PISD--new , SR_PN_INV_SEP_PORT_TONNAGES PISPT --new WHERE PFT.FLIGHT_OPERATION_DATE >= trunc( CASE :rundate WHEN TO_DATE('01/01/1900', 'DD/MM/YYYY') THEN ADD_MONTHS(SYSDATE,-1) ELSE ADD_MONTHS(:rundate,-1) END, 'MON') AND PFT.FLIGHT_OPERATION_DATE < trunc( CASE :rundate WHEN TO_DATE('01/01/1900', 'DD/MM/YYYY') THEN TRUNC(SYSDATE) ELSE TRUNC(:rundate) END, 'MON') AND PFT.TYPE IN ('C', 'F') AND PFT.RECORD_TYPE = 'M' AND (PFT.TERMINAL_OPERATOR NOT IN ('X', 'A') OR (PFT.TERMINAL_OPERATOR <> 'X' AND FA.CARRIER_CODE IN (SELECT * FROM SPECIAL_HANDLING_AIRLINE) AND PPT.REVENUE_TYPE IN (SELECT * FROM SPECIAL_REVENUE_TYPE) AND PPT.SHIPMENT_FLOW_TYPE IN (SELECT * FROM SPECIAL_SHIPMENT_FLOW_TYPE) AND PFT.FLIGHT_OPERATION_DATE >= (select EFF_DATE from SPECIAL_HANDLING_EFF_DATE) )) AND PFT.DELETING_DATETIME IS NULL AND FA.AIRLINE_UID = PFT.AIRLINE_UID AND FA.DELETING_DATETIME IS NULL AND PTFP.FLIGHT_TONNAGE_UID = PFT.FLIGHT_TONNAGE_UID AND PTFP.RECORD_TYPE = 'M' AND PTFP.DELETING_DATETIME IS NULL AND PPT.TONNAGE_FLIGHT_PORT_UID (+)= PTFP.TONNAGE_FLIGHT_PORT_UID AND PPT.RECORD_TYPE (+)= 'M' AND PPT.DISCREPANCY_TYPE (+)= 'NONE' AND PPT.ADJUSTMENT_INC_FLAG (+)= 'Y' AND PPT.DELETING_DATETIME (+) IS NULL AND FAST.AIRCRAFT_SERVICE_TYPE_UID = PFT.AIRCRAFT_SERVICE_TYPE_UID AND FAST.DELETING_DATETIME IS NULL AND PIS.TEMPORAL_NAME = TO_CHAR((CASE :rundate --new WHEN TO_DATE('01/01/1900', 'DD/MM/YYYY') THEN TRUNC(SYSDATE) ELSE TRUNC(:rundate) END ), 'YYYYMM') || '00' AND PIS.INVOICE_SEPARATION_UID = PISD.INVOICE_SEPARATION_UID --new AND PISD.INVOICE_SEP_DETAIL_UID = PISPT.INVOICE_SEP_DETAIL_UID --new AND PISPT.PORT_TONNAGE_UID = PPT.PORT_TONNAGE_UID --new AND PIS.PRINT_SUPPORTING_DOC = 'Y';上面是oracle的写法,请转成spark SQL的写法。
06-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值