sql语句怎么获取系统时间

sql语句怎么获取系统时间,第1张

sql语句怎么获取系统时间

sql读取系统日期和时间的方法如下:

--获取当前日期(如:yyyymmdd)

select CONVERT (nvarchar(12),GETDATE(),112)

--获取当前日期(如:yyyymmdd hh:MM:ss)

select GETDATE()

--获取当前日期(如:yyyy-mm-dd)

Select Datename(year,GetDate())+'-'+Datename(month,GetDate())+'-'+Datename(day,GetDate())

--获取当前日期(如:yyyy/mm/dd)

select DATENAME(YEAR,GETDATE())+'/'+DATENAME(MONTH,GETDATE())+'/'+DATENAME(DAY,GETDATE())

--获取几种日期

select DATENAME(YEAR,GETDATE()) --年份(YYYY)

select DATENAME(YY,GETDATE())

select DATENAME(MM,GETDATE()) --月份

select DATENAME(DD,GETDATE()) --日期

select dateName(hh,getdate()) --获取小时

select DATENAME(MI,GETDATE()) --获取分钟

select DATENAME(SECOND,GETDATE()) --获取秒

select DATENAME(WEEK,GETDATE()) --获取当前星期(周)是这一年中的第几个星期(周)

select DATENAME(WEEKDAY,GETDATE()) --星期几

#include<timeh>

int main()

{

time_t timep;

struct tm p;

time (&timep);

p=gmtime(&timep);

printf("%d\n",p->tm_sec); /获取当前秒/

printf("%d\n",p->tm_min); /获取当前分/

printf("%d\n",8+p->tm_hour);/获取当前时,这里获取西方的时间,刚好相差八个小时/

printf("%d\n",p->tm_mday);/获取当前月份日数,范围是1-31/

printf("%d\n",1+p->tm_mon);/获取当前月份,范围是0-11,所以要加1/

printf("%d\n",1900+p->tm_year);/获取当前年份,从1900开始,所以要加1900/

printf("%d\n",p->tm_yday); /从今年1月1日算起至今的天数,范围为0-365/

}

C语言是一门通用计算机编程语言,广泛应用于底层开发。C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。

尽管C语言提供了许多低级处理的功能,但仍然保持着良好跨平台的特性,以一个标准规格写出的C语言程序可在许多电脑平台上进行编译,甚至包含一些嵌入式处理器(单片机或称MCU)以及超级电脑等作业平台。

二十世纪八十年代,为了避免各开发厂商用的C语言语法产生差异,由美国国家标准局为C语言制定了一套完整的美国国家标准语法,称为ANSI C,作为C语言最初的标准。目前2011年12月8日,国际标准化组织(ISO)和国际电工委员会(IEC)发布的C11标准是C语言的第三个官方标准,也是C语言的最新标准,该标准更好的支持了汉字函数名和汉字标识符,一定程度上实现了汉字编程。

C语言是一门面向过程的计算机编程语言,与C++,Java等面向对象的编程语言有所不同。

其编译器主要有Clang、GCC、WIN-TC、SUBLIME、MSVC、Turbo C等。

参考资料:

C语言_百度百科

1、首先打开ASP的运行环境 ,这里使用的是Windows自带的IIS管理工具。

2、在使用的地方写入以下代码,运行该项目即可。

3、如图所示系统时间就可以显示出来了。

4、这个方法是使用时间的每一个字段组合成的时间,这个适用于获取某个时间字段。

5、经过组合后,如图所示,时间也可以按照完整的格式显示出来。

6、这里是获取年月份和时分秒和星期的方法,组合一起也可以显示完整的时间。

7、时间如图所示分割的显示了。

可以了解一下MySQL的时间戳(Timestamp)数据类型:

默认时间戳(Timestamp)类型的取值范围为'1970-01-01 00:00:01' UTC至'2038-01-19 03:14:07' UTC,数据精确到秒级别,该取值范围包含约22亿个数值,因此在MySQL内部使用4个字节INT类型来存放时间戳数据:

1、在存储时间戳数据时,先将本地时区时间转换为UTC时区时间,再将UTC时区时间转换为INT格式的毫秒值(使用UNIX_TIMESTAMP函数),然后存放到数据库中。

2、在读取时间戳数据时,先将INT格式的毫秒值转换为UTC时区时间(使用FROM_UNIXTIME函数),然后再转换为本地时区时间,最后返回给客户端。

(Timestamp)时间戳列可以有四张组合定义,其含义分别为:

1、当字段定义为timestamp,表示该字段在插入和更新时都不会自动设置为当前时间。

2、当字段定义为timestamp DEFAULT CURRENT_TIMESTAMP,表示该字段仅在插入且未指定值时被赋予当前时间,再更新时且未指定值时不做修改。

3、当字段定义为timestamp ON UPDATE CURRENT_TIMESTAMP,表示该字段在插入且未指定值时被赋值为"0000-00-00 00:00:00",在更新且未指定值时更新为当前时间。

4、当字段定义为timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,表示该字段在插入或更新时未指定值,则被赋值为当前时间。

方法一:通过查找表数据文件方式

这种方法通过查找表的数据文件的方式从而确定表的创建时间,但是这种方法并不能准备查询表的创建

时间,而且有时候,这种方法得到的信息还有可能是错误的,下面大致演示下。

--11 创建表并插入数据

francs=> create table test_ctime (id int4 primary key ,name varchar(32));

NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test_ctime_pkey" for table "test_ctime"

CREATE TABLE

francs=> insert into test_ctime select generate_series(1,10000),'create_time test';

INSERT 0 10000

francs=> \d test_ctime;

Table "francstest_ctime"

Column | Type | Modifiers

--------+-----------------------+-----------

id | integer | not null

name | character varying(32) |

Indexes:

"test_ctime_pkey" PRIMARY KEY, btree (id)

francs=> \dt+ test_ctime;

List of relations

Schema | Name | Type | Owner | Size | Description

--------+------------+-------+--------+--------+-------------

francs | test_ctime | table | francs | 536 kB |

(1 row)

备注:表创建好了,接下来演示如何定位表的物理文件。

--12 定位表所在的表空间

francs=> select relname,relfilenode,reltablespace from pg_class where relname='test_ctime';

relname | relfilenode | reltablespace

------------+-------------+---------------

test_ctime | 24650 | 0

(1 row)

备注:在 PostgreSQL 的逻辑结构体系中,表位于数据库中,同时表位于表空间上,面表空间对应系统上一个

文件目录,每个表由一个或者多个文件组成; 根据上面的结果,表 test_ctime 的 reltablespace

值为 0,表示位于所属数据库的默认表空间,注意 relfilenode 值为 24650。

--13 查询数据库 francs 的默认表空间

francs=> select oid,datname,dattablespace from pg_database where datname='francs';

oid | datname | dattablespace

-------+---------+---------------

16386 | francs | 16385

备注:上面查出数据库 francs 的默认表空间的 oid 为 16385。

--14 查找 oid 为 16385 的表空间

francs=> select oid, from pg_tablespace where oid=16385;

oid | spcname | spcowner | spcacl | spcoptions

-------+------------+----------+-----------------------------------------+------------

16385 | tbs_francs | 10 | {postgres=C/postgres,francs=C/postgres} |

(1 row)

备注:查了半天才查到表 test_ctime 的默认表空间为 tbs_francs,这里之所以饶这么大圈,是为

了展示 postgresql 中的一些逻辑结构关系,如果自己对环境比较熟悉,可以直接定位到

哪个表空间。

--15 查询表空间 tbs_francs 对应的物理目录

francs=> \db

List of tablespaces

Name | Owner | Location

------------+----------+------------------------------------------

pg_default | postgres |

pg_global | postgres |

tbs_francs | postgres | /database/1922/pgdata1/pg_tbs/tbs_francs

(3 rows)

备注:表空间 tbs_francs 的数据目录为 /database/1922/pgdata1/pg_tbs/tbs_francs。

--16 进入数据目录

[postgres@redhat6 16386]$ cd /database/1922/pgdata1/pg_tbs/tbs_francs

[postgres@redhat6 tbs_francs]$ ll

total 40K

drwx------ 4 postgres postgres 40K May 22 10:35 PG_92_201204301

[postgres@redhat6 tbs_francs]$ cd PG_92_201204301/

[postgres@redhat6 PG_92_201204301]$ ll

total 16K

drwx------ 2 postgres postgres 12K Jun 26 19:03 16386

drwx------ 2 postgres postgres 40K May 22 10:37 pgsql_tmp

备注:根据前面的步骤 13 查询的信息知道 16386 为数据库 francs 的 oid。 再根据步骤 12 的信息知道

表 test_ctime 的 relfilenode 值为 24650

--17 查找表 test_ctime 的数据文件

[postgres@redhat6 16386]$ ll 24650

-rw------- 1 postgres postgres 512K Jun 26 18:57 24650

备注:根据数据文件 24650 知道表的创建时间为 2012-06-26 18:57。但这种方法并不准确,因为

表上的 *** 作可能导致表重新生成文件,接着演示。

--18 cluster 表

francs=> cluster verbose test_ctime using test_ctime_pkey;

INFO: clustering "francstest_ctime" using index scan on "test_ctime_pkey"

INFO: "test_ctime": found 0 removable, 10000 nonremovable row versions in 64 pages

DETAIL: 0 dead row versions cannot be removed yet

CPU 000s/003u sec elapsed 008 sec

CLUSTER

francs=> select relname,relfilenode,reltablespace from pg_class where relname='test_ctime';

relname | relfilenode | reltablespace

------------+-------------+---------------

test_ctime | 24655 | 0

(1 row)

备注:表 test_ctime 经过 cluster *** 作后,重新生成了数据文件,文件号由原来的 24650 变成了 24655

--19 系统上再次查询表数据文件

[postgres@redhat6 16386]$ ll 24650

-rw------- 1 postgres postgres 0 Jun 26 19:19 24650

[postgres@redhat6 16386]$ ll 24655

-rw------- 1 postgres postgres 512K Jun 26 19:19 24655

备注:显然新文件的时间 24655 并不是表 test_ctime 的初始创建时间。

--110 vacuum full 表

francs=> vacuum full test_ctime;

VACUUM

francs=> select relname,relfilenode,reltablespace from pg_class where relname='test_ctime';

relname | relfilenode | reltablespace

------------+-------------+---------------

test_ctime | 24659 | 0

(1 row)

备注: vacuum full *** 作后,同样产生了新文件,新文件号为 24659

--111 系统上再次查询表数据文件

[postgres@redhat6 16386]$ ll 24659

-rw------- 1 postgres postgres 512K Jun 26 19:22 24659

SYSDATE函数可以得到目前系统的时间,所以答案是C例如:

select sysdate from dual;

SYSDATE函数常用的格式举例如下:

--加法

select sysdate,add_months(sysdate,12) from dual; --加1年

select sysdate,add_months(sysdate,1) from dual; --加1月

select sysdate,to_char(sysdate+7,'yyyy-mm-dd HH24:MI:SS') from dual; --加1星期

select sysdate,to_char(sysdate+1,'yyyy-mm-dd HH24:MI:SS') from dual; --加1天

select sysdate,to_char(sysdate+1/24,'yyyy-mm-dd HH24:MI:SS') from dual; --加1小时

select sysdate,to_char(sysdate+1/24/60,'yyyy-mm-dd HH24:MI:SS') from dual; --加1分钟

select sysdate,to_char(sysdate+1/24/60/60,'yyyy-mm-dd HH24:MI:SS') from dual; --加1秒

--减法

select sysdate,add_months(sysdate,-12) from dual; --减1年

select sysdate,add_months(sysdate,-1) from dual; --减1月

select sysdate,to_char(sysdate-7,'yyyy-mm-dd HH24:MI:SS') from dual; --减1星期

select sysdate,to_char(sysdate-1,'yyyy-mm-dd HH24:MI:SS') from dual; --减1天

select sysdate,to_char(sysdate-1/24,'yyyy-mm-dd HH24:MI:SS') from dual; --减1小时

select sysdate,to_char(sysdate-1/24/60,'yyyy-mm-dd HH24:MI:SS') from dual; --减1分钟

select sysdate,to_char(sysdate-1/24/60/60,'yyyy-mm-dd HH24:MI:SS') from dual; --减1秒

--举例:

--1、取得当前日期是本月的第几周

select to_char(sysdate,'YYYYMMDD W HH24:MI:SS') from dual;

select to_char(sysdate,'W') from dual;

--2、取得当前日期是一个星期中的第几天,星期日为第一天

select sysdate,to_char(sysdate,'D') from dual;

--类似:

select to_char(sysdate,'yyyy') from dual; --年

select to_char(sysdate,'Q' from dual; --季

select to_char(sysdate,'mm') from dual; --月

select to_char(sysdate,'dd') from dual; --日

--ddd 年中的第几天

--WW 年中的第几个星期

--W 该月中第几个星期

--D 周中的星期几

--hh 小时(12)

--hh24 小时(24)

--Mi 分

--ss 秒

--3、取当前日期是星期几,中文显示

select to_char(sysdate,'day') from dual;

--4、如果一个表在一个date类型的字段上面建立了索引,如何使用

alter session set NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'

--5、得到当前的日期

select sysdate from dual;

--6、得到当天凌晨0点0分0秒的日期

select trunc(sysdate) from dual;

-- 得到这天的最后一秒

select trunc(sysdate) + 099999 from dual;

-- 得到小时的具体数值

select trunc(sysdate) + 1/24 from dual;

select trunc(sysdate) + 7/24 from dual;

--7、得到明天凌晨0点0分0秒的日期

select trunc(sysdate+1) from dual;

select trunc(sysdate)+1 from dual;

--8、本月一日的日期

select trunc(sysdate,'mm') from dual;

--9、得到下月一日的日期

select trunc(add_months(sysdate,1),'mm') from dual;

--10、返回当前月的最后一天

select last_day(sysdate) from dual;

————————————————

版权声明:本文为CSDN博主「象在舞」的原创文章,遵循 CC 40 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接:>

大家也许对PHP时间戳已经有所了解,那么我们如何应用它来获取具体的日期呢?我们今天来为大家介绍一下PHP时间戳获取当前时期的具体方式。 实现功能:获取某个日期的时间戳,或获取某个时间的PHP时间戳。 strtotime能将任何英文文本的日期时间描

以上就是关于sql语句怎么获取系统时间全部的内容,包括:sql语句怎么获取系统时间、C语言中 如何获取系统时间、asp 如何获得系统当前时间 (包括分)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/9753033.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-01
下一篇 2023-05-01

发表评论

登录后才能评论

评论列表(0条)

保存