Time resolved data type mysql

1, MySQL five date and time types

MySQl There are several types of data indicating the date and time. Which indicates the year YEAR, DATE is the date, TIME represents the time, DATETIME and TIMESTAMP indicates the date and practice. Comparison thereof as follows:

TEAR, number of bytes is 1, the range of "1901--2155"

DATE, number of bytes is 4, the range of "1000-01-01--9999-12-31"

The TIME, number of bytes is 3, the range of "-838: 59: 59--838: 59: 59"

DATETIME, 8 bytes, in the range of "1000-01-0100: 00: 00--9999-12-31 23:59:59"

TIMESTAMP, number of bytes is 4, the range of "19700101080001--20380119111407"

When the value is outside the valid value range is inserted, the system being given, and a zero value is inserted into the database.

 

2, YEAR type

YEAR type to be copied in three ways.

The first string is inserted directly into 4 or 4 digits.

The second string 2 is inserted, is inserted in this case, if '00' ~ '69 ', corresponding to the insert of 2000 to 2069; if the insert' 70 '~' 99 ', corresponding to the insert 1970 to 1999. If it is '0', '00' is inserted in the second case the same and insert effects are expressed in 2000.

The third is to insert two numbers, it with a second (insert two strings) except that only: If you insert a digit 0, it means that 0000, not 2000. So when given the assignment YEAR types, we must distinguish between 0 and '0', although the difference between the two quotation marks, but the actual effect is indeed a difference of 2000.

3, TIME type

TIME type indicated as "hours: minutes: seconds", although hours generally range from 0 to 23, but in order to show some specific time interval, MySQL TIME hours expand the scope to be made, and a negative support.

TIME type of assignment, the standard format is 'HH: MM: SS', but not necessarily in this format.

If the insert is 'D HH: MM: SS' format is similar to the insert '(D * 24 + HH): MM: SS'. Such as inserting '23:50:50 2', corresponding to the inserted '71: 50: 50 '.

If you insert 'HH: MM' or 'SS' format, the effect is not represented other bit value assigned to a zero value. Such as insert '30', corresponding to the inserted '00: 00: 30 '; if inserted '11: 25', corresponding to the inserted '11: 25: 00 '.

Further may be inserted 'D HH' and 'D HH: MM', the effect according to the above example it may be inferred.

In MySQl, for 'HHMMSS' format, the system can automatically be converted to a standard format.

If we want to insert current system time, you can insert CURRENT_TIME or NOW ().

TIME type only 3 bytes, if only data storage time, it is most appropriate.

4, DATE type

MySQL is YYYY-MM-DD format to display the value of DATE type, insert the data, the data format can be maintained. In addition, MySQL also supports the syntax is not strict, separator "-" can be replaced with "@", and many rich. "."

When inserting data, may also be used "YY-MM-DD" format, YY converted to the corresponding rule YEAR year and similar types.

If we want to insert current system time, you can insert CURRENT_DATE or NOW ().

5, DATETIME type

Standard format "YYYY-MM-DD HH: MM: SS", similar to the specific evaluation method with various types of the above process.

6, TIMESTAMP type

TIMESTAMP value range is relatively small, no large DATETIME the range, the input value must therefore ensure that the range of the TIMESTAMP. It is also similar to insert other insertion date and time data types.

So what type TIMESTAMP to insert the current time? The first can be used CURRENT_TIMESTAMP; second, input NULL, the system automatically enters the current TIMESTAMP; third, no input, the system will automatically enter the current TIMESTAMP.

Another very special thing: TIMESTAMP value is associated with the time zone.




MySQL Date type: Comp date format, storage space is occupied, the date range. 
Date date format type storage range 
------------ --------- --------------------- ----------------------------------------- 
datetime 8 bytes YYYY-MM-DD HH : the MM: the SS 1000-01-01 00:00:00 ~ 9999-12-31 23:59:59 
timestamp. 4 bytes the MM-DD-YYYY HH: the MM: the SS ~ 2038 1970-01-01 00:00:01 
the MM-YYYY. 3 bytes DATE DD-1000-01-01 ~ 9999-12-31 
year. 1 1901 ~ 2155 bytes YYYY

When you create a table in MySQL, comparison tables above, it is easy to choose a suitable own data types. But in the end is to choose datetime or timestamp, it might be a little daunting. Both types have advantages Date Time: date datetime relatively large range; timestamp occupied storage space is relatively small, only half of datetime.

Further, there is a column of type timestamp characteristics: By default, the data insert, update, timestamp column automatically to the current time (CURRENT_TIMESTAMP) is filled / updated. "Automatic" means you leave it, MySQL will be for you to deal with. 

Code for the construction of the table:

create table t8 (
`id1` timestamp NOT NULL default CURRENT_TIMESTAMP,
`id2` datetime default NULL
);


In general, I tend to use datetime date type.

The comparison between the two:

1. timestamp range easily supported smaller than timedate. And prone to circumstances beyond

Comparison of the Effects 2.timestamp time zone timezone and the impact of SQL MODE and server versions of MYSQL subject.


MySQL time Type: time format, occupies storage space, time range. 
Time Type Storage Time Format time 
------------ --------- --------------------- ----------------------------------------- 
Time 3 bytes HH: MM: SS - 838: 59: 59 to 838: 59: 59

time time actually have such a large range, in particular time can be negative, a bit strange. Later, I read the MySQL manual knew it was time to meet the two dates subtract it by design.

select timediff('2000:01:31 23:59:59', '2000:01:01 00:00:00'); -- 743:59:59 
select timediff('2000:01:01 00:00:00', '2000:01:31 23:59:59'); -- -743:59:59 
select timediff('23:59:59', '12:00:00'); -- 11:59:59

Note that, the only two parameters timediff datetime / timestamp, time type, and the two types of parameters to be the same. That is: datetime / timestamp and datetime / timestamp comparison; comparison time and time.

Although the date and time of the type rich in MySQL, but unfortunately, currently (2008-08-08) The date and time types can only support up to second grade, does not support milliseconds, microseconds. No generation function milliseconds.


"MySQL: MySQL date data type, MySQL time type using summary" Applies to MySQL 5.X and above.

A, MySQL function to get the current date and time 
1.1 to get the current date + time (date + time) function: now ()

mysql> select now();

+---------------------+ 
| now() | 
+---------------------+ 
| 2008-08-08 22:20:46 | 
+---------------------+

In addition to now () function to get the current date and time outside, MySQL there are the following function:

current_timestamp() 
,current_timestamp 
,localtime() 
,localtime 
,localtimestamp -- (v4.0.6) 
,localtimestamp() -- (v4.0.6)

The date and time functions are identical to the now (). Given the now () function is a short and easy to remember, it is recommended to always use now () to replace the functions listed above.

1.2 to obtain the current date + time (date + time) function: sysdate ()

sysdate () function is similar with the date and time now (), except that: now () at the beginning of a time when it was executed, sysdate () to get dynamic value when the function execution. Look at the following example to understand:

mysql> select now(), sleep(3), now();

+---------------------+----------+---------------------+ 
| now() | sleep(3) | now() | 
+---------------------+----------+---------------------+ 
| 2008-08-08 22:28:21 | 0 | 2008-08-08 22:28:21 | 
+---------------------+----------+---------------------+

mysql> select sysdate(), sleep(3), sysdate();

+---------------------+----------+---------------------+ 
| sysdate() | sleep(3) | sysdate() | 
+---------------------+----------+---------------------+ 
| 2008-08-08 22:28:41 | 0 | 2008-08-08 22:28:44 | 
+---------------------+----------+---------------------+

It can be seen that, although the middle SLEEP 3 seconds, but now () function of two time values ​​are the same; SYSDATE () function of two time values ​​obtained difference of 3 seconds. Is MySQL Manual describes SYSDATE () is: Return the time at which the function executes.

sysdate () the date and time function, rarely used under normal circumstances.

 


2. Get the current date (date) function: curdate ()

mysql> select curdate();

+ ------------ + 
| CURDATE () | 
+ ------------ + 
| 2008-08-08 | 
+ ------------ +

Wherein, following function is equivalent to two dates curdate ():

current_date() 
,current_date

3. Get the current time (time) function: curtime ()

mysql> select curtime();

+ ----------- + 
| CURTIME () | 
+ ----------- + 
| 22:41:30 | 
+ ----------- +

Wherein the following two time function is equivalent to curtime ():

current_time() 
,current_time

4. Time function obtains the current UTC date: utc_date (), utc_time (), utc_timestamp ()

mysql> select utc_timestamp(), utc_date(), utc_time(), now()

+---------------------+------------+------------+---------------------+ 
| utc_timestamp() | utc_date() | utc_time() | now() | 
+---------------------+------------+------------+---------------------+ 
| 2008-08-08 14:47:11 | 2008-08-08 | 14:47:11 | 2008-08-08 22:47:11 | 
+---------------------+------------+------------+---------------------+

Because our country is located in East eight time zones, so local time = UTC time + 8 hours. UTC time when the business involves a number of countries and regions, very useful.

 

 


Five, MySQL timestamp (Timestamp) function 
1. MySQL to get the current time stamp function: current_timestamp, current_timestamp ()

mysql> select current_timestamp, current_timestamp();

+---------------------+---------------------+ 
| current_timestamp | current_timestamp() | 
+---------------------+---------------------+ 
| 2008-08-09 23:22:24 | 2008-08-09 23:22:24 | 
+---------------------+---------------------+

2. MySQL (Unix timestamp, date) conversion functions:

unix_timestamp(), 
unix_timestamp(date), 
from_unixtime(unix_timestamp), 
from_unixtime(unix_timestamp,format)

Here is an example:

select unix_timestamp (); - 1218290027 === UNIX time obtained current time value

 

DETAILED time to timestamp

select unix_timestamp('2008-08-08'); -- 1218124800 
select unix_timestamp('2008-08-08 12:30:00'); -- 1218169800


The timestamp is converted into a specific time 
SELECT FROM_UNIXTIME (1,218,290,027); - 'Post Comment 2008-08-09 21:53:47' 
SELECT FROM_UNIXTIME (1,218,124,800); - '2008-08-08 00:00:00' 
SELECT FROM_UNIXTIME ( 1218169800); - '2008-08-08 12:30:00'

select from_unixtime(1218169800, '%Y %D %M %h:%i:%s %x'); -- '2008 8th August 12:30:00 2008'

 

 

 

3. MySQL timestamp (timestamp) conversion, increasing, decreasing function:

timestamp(date) -- date to timestamp 
timestamp(dt,time) -- dt + time 
timestampadd(unit,interval,datetime_expr) -- 
timestampdiff(unit,datetime_expr1,datetime_expr2) --

Below is an example parts:

select timestamp('2008-08-08'); -- 2008-08-08 00:00:00 
select timestamp('2008-08-08 08:00:00', '01:01:01'); -- 2008-08-08 09:01:01 
select timestamp('2008-08-08 08:00:00', '10 01:01:01'); -- 2008-08-18 09:01:01

select timestampadd(day, 1, '2008-08-08 08:00:00'); -- 2008-08-09 08:00:00 
select date_add('2008-08-08 08:00:00', interval 1 day); -- 2008-08-09 08:00:00

MySQL timestampadd () function is similar to date_add ().

select timestampdiff(year,'2002-05-01','2001-01-01'); -- -1 
select timestampdiff(day ,'2002-05-01','2001-01-01'); -- -485 
select timestampdiff(hour,'2008-08-08 12:00:00','2008-08-08 00:00:00'); -- -12

select datediff('2008-08-08 12:00:00', '2008-08-01 00:00:00'); -- 7

MySQL timestampdiff () function on () much stronger than datediff function () can only calculate the number of days difference between two dates (date) of datediff.

 

 

 

====================================================================================

 

 


Two, MySQL the Extract Date Time (selecting) function. 
1. Select the date and time of each part of: the date, time, year, quarter, month, day, hour, minute, second, microsecond

set @dt = '2008-09-10 07:15:30.123456';

select date(@dt); -- 2008-09-10 
select time(@dt); -- 07:15:30.123456 
select year(@dt); -- 2008 
select quarter(@dt); -- 3 
select month(@dt); -- 9 
select week(@dt); -- 36 
select day(@dt); -- 10 
select hour(@dt); -- 7 
select minute(@dt); -- 15 
select second(@dt); -- 30 
select microsecond(@dt); -- 123456

2. MySQL Extract () function, you can achieve similar functionality above:

set @dt = '2008-09-10 07:15:30.123456';

select extract(year from @dt); -- 2008 
select extract(quarter from @dt); -- 3 
select extract(month from @dt); -- 9 
select extract(week from @dt); -- 36 
select extract(day from @dt); -- 10 
select extract(hour from @dt); -- 7 
select extract(minute from @dt); -- 15 
select extract(second from @dt); -- 30 
select extract(microsecond from @dt); -- 123456

select extract(year_month from @dt); -- 200809 
select extract(day_hour from @dt); -- 1007 
select extract(day_minute from @dt); -- 100715 
select extract(day_second from @dt); -- 10071530 
select extract(day_microsecond from @dt); -- 10071530123456 
select extract(hour_minute from @dt); -- 715 
select extract(hour_second from @dt); -- 71530 
select extract(hour_microsecond from @dt); -- 71530123456 
select extract(minute_second from @dt); -- 1530 
select extract(minute_microsecond from @dt); -- 1530123456 
select extract(second_microsecond from @dt); -- 30123456

MySQL Extract () function does not function in addition to date (), time (), and other functions should be a whole. And further having a select 'day_microsecond' functions. Note that this is not the only selected day and microsecond, but has been selected from the day portion of the date to the microsecond part. Doughty it!

MySQL Extract () function only bad thing is this: You need to knock a few keyboard.

3. MySQL dayof ... function: dayofweek (), dayofmonth (), dayofyear ()

Return the date parameters in the week, the month, year, location.

set @dt = '2008-08-08';

select dayofweek(@dt); -- 6 
select dayofmonth(@dt); -- 8 
select dayofyear(@dt); -- 221

Date '2008-08-08' is the 6th day of the week (1 = Sunday, 2 = Monday, ..., 7 = Saturday); 8 days of the month; 221 days a year.

4. MySQL week... 函数:week(), weekofyear(), dayofweek(), weekday(), yearweek()

set @dt = '2008-08-08';

select week(@dt); -- 31 
select week(@dt,3); -- 32 
select weekofyear(@dt); -- 32

select dayofweek(@dt); -- 6 
select weekday(@dt); -- 4

select yearweek(@dt); -- 200831

MySQL week () function, you can have two parameters, specifically look at the manual. weekofyear () and week (), are calculated "someday" is located in the first few weeks of the year. weekofyear (@dt) equivalent to a week (@ dt, 3).

MySQL weekday () function and dayofweek () is similar to the return position is "day" of the week. The difference is that the reference standard, weekday: (0 = Monday, 1 = Tuesday, ..., 6 = Sunday); dayofweek: (1 = Sunday, 2 = Monday, ..., 7 = Saturday)

MySQL yearweek () function returns the year (2008) + week position (31).

5. MySQL function returns the week and month names: dayname (), monthname ()

set @dt = '2008-08-08';

select dayname(@dt); -- Friday 
select monthname(@dt); -- August

Thinking about how to return the name of Chinese do?

6. MySQL last_day () function: returns the last day of the month.

select last_day('2008-02-01'); -- 2008-02-29 
select last_day('2008-08-08'); -- 2008-08-31

MySQL last_day () function is useful, for example, I think in the current month how many days, it can be calculated:

mysql> select now(), day(last_day(now())) as days;

+---------------------+------+ 
| now() | days | 
+---------------------+------+ 
| 2008-08-09 11:45:45 | 31 | 
+---------------------+------+

Three, MySQL datetime calculation function 
1. MySQL increase a date interval: date_add ()

set @dt = now();

select date_add(@dt, interval 1 day); -- add 1 day 
select date_add(@dt, interval 1 hour); -- add 1 hour 
select date_add(@dt, interval 1 minute); -- ... 
select date_add(@dt, interval 1 second); 
select date_add(@dt, interval 1 microsecond); 
select date_add(@dt, interval 1 week); 
select date_add(@dt, interval 1 month); 
select date_add(@dt, interval 1 quarter); 
select date_add(@dt, interval 1 year);

select date_add(@dt, interval -1 day); -- sub 1 day

MySQL adddate (), addtime () function can be replaced with date_add (). The following is DATE_ADD () implement AddTime () function example:

mysql> set @dt = '2008-08-09 12:12:33';

mysql> 
mysql> select date_add(@dt, interval '01:15:30' hour_second);

+------------------------------------------------+ 
| date_add(@dt, interval '01:15:30' hour_second) | 
+------------------------------------------------+ 
| 2008-08-09 13:28:03 | 
+------------------------------------------------+

mysql> select date_add(@dt, interval '1 01:15:30' day_second);

+-------------------------------------------------+ 
| date_add(@dt, interval '1 01:15:30' day_second) | 
+-------------------------------------------------+ 
| 2008-08-10 13:28:03 | 
+-------------------------------------------------+

DATE_ADD () function, respectively, increased @dt "1 hour 15 minutes 30 seconds" and "1 day, 1 hour 15 minutes 30 seconds." Recommendation: Always use date_add () function instead of the date and time adddate (), addtime ().

2. MySQL to date minus a time interval: date_sub ()

mysql> select date_sub('1998-01-01 00:00:00', interval '1 1:1:1' day_second);

+----------------------------------------------------------------+ 
| date_sub('1998-01-01 00:00:00', interval '1 1:1:1' day_second) | 
+----------------------------------------------------------------+ 
| 1997-12-30 22:58:59 | 
+----------------------------------------------------------------+

Consistent with MySQL date_sub () the date and time functions date_add () usage, not repeat them. Further, MySQL there are two functions subdate (), subtime (), recommended by DATE_SUB () instead.

Alternative 3. MySQL date functions: period_add (P, N), period_diff (P1, P2)

Function parameters "P" format is "YYYYMM" or "YYMM", the second parameter "N" denotes adding or subtracting N month (month).

MySQL period_add (P, N): Date plus / minus N month.

mysql> select period_add(200808,2), period_add(20080808,-2)

+----------------------+-------------------------+ 
| period_add(200808,2) | period_add(20080808,-2) | 
+----------------------+-------------------------+ 
| 200810 | 20080806 | 
+----------------------+-------------------------+

MySQL period_diff (P1, P2): Date P1-P2, N returns months.

mysql> select period_diff(200808, 200801);

+-----------------------------+ 
| period_diff(200808, 200801) | 
+-----------------------------+ 
| 7 | 
+-----------------------------+

In MySQL, these functions date, under normal circumstances rarely used.

4. MySQL date, time subtraction function: datediff (date1, date2), timediff (time1, time2)

MySQL datediff (date1, date2): subtract two dates date1 - date2, returns the number of days.

select datediff('2008-08-08', '2008-08-01'); -- 7 
select datediff('2008-08-01', '2008-08-08'); -- -7

MySQL timediff (time1, time2): subtract two dates time1 - time2, return time difference.

select timediff('2008-08-08 08:08:08', '2008-08-08 00:00:00'); -- 08:08:08 
select timediff('08:08:08', '00:00:00'); -- 08:08:08

Note: timediff (time1, time2) two parameters must be the same type of the function.

4, MySQL of date conversion function, conversion routines 
1. MySQL (time in seconds) transfer function: time_to_sec (time), sec_to_time ( seconds)

select time_to_sec('01:00:05'); -- 3605 
select sec_to_time(3605); -- '01:00:05'

2. MySQL (date, number of days) conversion functions: to_days (date), from_days (days)

select to_days('0000-00-00'); -- 0 
select to_days('2008-08-08'); -- 733627

select from_days(0); -- '0000-00-00' 
select from_days(733627); -- '2008-08-08'

3. MySQL Str to Date (string into the date) function: str_to_date (str, format)

select str_to_date('08/09/2008', '%m/%d/%Y'); -- 2008-08-09 
select str_to_date('08/09/08' , '%m/%d/%y'); -- 2008-08-09 
select str_to_date('08.09.2008', '%m.%d.%Y'); -- 2008-08-09 
select str_to_date('08:09:30', '%h:%i:%s'); -- 08:09:30 
select str_to_date('08.09.2008 08:09:30', '%m.%d.%Y %h:%i:%s'); -- 2008-08-09 08:09:30

Can be seen, str_to_date (str, format) conversion function, can put some chaotic string into the date format. Further, it can also be converted to time. "Format" can be found in the MySQL manual.

4. MySQL Date / Time to Str (date / time is converted to string): date_format (date, format), time_format (time, format)

mysql> select date_format('2008-08-08 22:23:00', '%W %M %Y');

+------------------------------------------------+ 
| date_format('2008-08-08 22:23:00', '%W %M %Y') | 
+------------------------------------------------+ 
| Friday August 2008 | 
+------------------------------------------------+

mysql> select date_format('2008-08-08 22:23:01', '%Y%m%d%H%i%s');

+----------------------------------------------------+ 
| date_format('2008-08-08 22:23:01', '%Y%m%d%H%i%s') | 
+----------------------------------------------------+ 
| 20080808222301 | 
+----------------------------------------------------+

mysql> select time_format('22:23:01', '%H.%i.%s');

+-------------------------------------+ 
| time_format('22:23:01', '%H.%i.%s') | 
+-------------------------------------+ 
| 22.23.01 | 
+-------------------------------------+

MySQL date, time conversion function: date_format (date, format), time_format (time, format) able to put a date / time character string converted into various formats. It is str_to_date (str, format) an inverse transfer function.

5. MySQL national regional time formatting functions: get_format ()

MySQL get_format () syntax:

get_format(date|time|datetime, 'eur'|'usa'|'jis'|'iso'|'internal'

All examples MySQL get_format () usage:

select get_format(date,'usa') ; -- '%m.%d.%Y' 
select get_format(date,'jis') ; -- '%Y-%m-%d' 
select get_format(date,'iso') ; -- '%Y-%m-%d' 
select get_format(date,'eur') ; -- '%d.%m.%Y' 
select get_format(date,'internal') ; -- '%Y%m%d' 
select get_format(datetime,'usa') ; -- '%Y-%m-%d %H.%i.%s' 
select get_format(datetime,'jis') ; -- '%Y-%m-%d %H:%i:%s' 
select get_format(datetime,'iso') ; -- '%Y-%m-%d %H:%i:%s' 
select get_format(datetime,'eur') ; -- '%Y-%m-%d %H.%i.%s' 
select get_format(datetime,'internal') ; -- '%Y%m%d%H%i%s' 
select get_format(time,'usa') ; -- '%h:%i:%s %p' 
select get_format(time,'jis') ; -- '%H:%i:%s' 
select get_format(time,'iso') ; -- '%H:%i:%s' 
select get_format(time,'eur') ; -- '%H.%i.%s' 
select get_format(time,'internal') ; -- '%H%i%s'

MySQL get_format () function uses the opportunity in practice less.

6. MySQL patchwork date, time functions: makdedate (year, dayofyear), maketime (hour, minute, second)

select makedate(2001,31); -- '2001-01-31' 
select makedate(2001,32); -- '2001-02-01'

select maketime(12,15,30); -- '12:15:30'

 


Six, MySQL time zone (timezone) conversion function 
convert_tz (dt, from_tz, to_tz)

select convert_tz('2008-08-08 12:00:00', '+08:00', '+00:00'); -- 2008-08-08 04:00:00

Time zone conversion can also be achieved by date_add, date_sub, timestampadd.

select date_add('2008-08-08 12:00:00', interval -8 hour); -- 2008-08-08 04:00:00 
select date_sub('2008-08-08 12:00:00', interval 8 hour); -- 2008-08-08 04:00:00 
select timestampadd(hour, -8, '2008-08-08 12:00:00'); -- 2008-08-08 04:00:00

Guess you like

Origin www.cnblogs.com/jiangshiguo/p/12082256.html