mysql 日期和時間以及轉換時間戳記函數

來源:互聯網
上載者:User

   使用execl轉換時間戳記的公式為:

  代碼:

  =(xxxxxxxxxx+8*3600)/86400+70*365+19

  使用mysql教程語句解釋時間戳記文法舉例:

  代碼:

  select from_unixtime(1234567890, '%y-%m-%d %h:%i:%s')

  附:

  在mysql中,一個時間欄位的儲存類型是int(11),怎麼轉化成字元類型,比方儲存為13270655222,需要轉化為yyyy -mm-dd的形式

  使用 from_unixtime函數,具體如下:

  代碼:

  from_unixtime(unix_timestamp,format)

  返回表示 unix 時間標記的一個字串,根據format字串格式化。format可以包含與date_format()函數列出的條目同樣的修飾符。

  根據format字串格式化date值。下列修飾符可以被用在format字串中: %m 月名字(january……december)

  %w 星期名字(sunday……saturday)

  %d 有英語首碼的月份的日期(1st, 2nd, 3rd, 等等。)

  %y 年, 數字, 4 位

  %y 年, 數字, 2 位

  %a 縮寫的星期名字(sun……sat)

  %d 月份中的天數, 數字(00……31)

  %e 月份中的天數, 數字(0……31)

  %m 月, 數字(01……12)

  %c 月, 數字(1……12)

  %b 縮寫的月份名字(jan……dec)

  %j 一年中的天數(001……366)

  %h 小時(00……23)

  %k 小時(0……23)

  %h 小時(01……12)

  %i 小時(01……12)

  %l 小時(1……12)

  %i 分鐘, 數字(00……59)

  %r 時間,12 小時(hh:mm:ss [ap]m)

  %t 時間,24 小時(hh:mm:ss)

  %s 秒(00……59)

  %s 秒(00……59)

  %p am或pm

  %w 一個星期中的天數(0=sunday ……6=saturday )

  %u 星期(0……52), 這裡星期天是星期的第一天

  %u 星期(0……52), 這裡星期一是星期的第一天

  %% 一個文字“%”。

  相關函數

  mysql> select something from tbl_name

  where to_days(now()) – to_days(date_col) <= 30;

  dayofweek(date)

  返回 date 的星期索引(1 = sunday, 2 = monday, ... 7 = saturday)。索引值符合 odbc 的標準。

  mysql> select dayofweek('1998-02-03′);

  -> 3

  weekday(date)

  返回 date 的星期索引(0 = monday, 1 = tuesday, … 6 = sunday):

  mysql> select weekday('1998-02-03 22:23:00′);

  -> 1

  mysql> select weekday('1997-11-05′);

  -> 2

  dayofmonth(date)

  返回 date 是一月中的第幾天,範圍為 1 到 31:

  mysql> select dayofmonth('1998-02-03′);

  -> 3

  dayofyear(date)

  返回 date 是一年中的第幾天,範圍為 1 到 366:

  mysql> select dayofyear('1998-02-03′);

  -> 34

  month(date)

  返回 date 中的月份,範圍為 1 到 12:

  mysql> select month('1998-02-03′);

  -> 2

  dayname(date)

  返回 date 的星期名:

  mysql> select dayname(”1998-02-05′);

  -> ‘thursday'

  monthname(date)

  返回 date 的月份名:

  mysql> select monthname(”1998-02-05′);

  -> ‘february'

  quarter(date)

  返回 date 在一年中的季度,範圍為 1 到 4:

  mysql> select quarter('98-04-01′);

  -> 2

  week(date)

  week(date,first)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.