bitsCN.com
SQLServer时间日期函数详解,SQLServer,时间日期,1. 当前系统日期、时间 select getdate()2. dateadd 在向指定日期加上一段时间的基础上,返回新的 datetime 值 例如:向日期加上2天 select dateadd(day,2,'2004-10-15') --返回:2004-10-17 00:00:00.0003. datediff 返回跨两个指定日期的日期和时间边界数。 select datediff(day,'2004-09-01','2004-09-18') --返回:17 select datediff(day,'2004-09-18','2004-09-01') --返回:-174. datepart 返回代表指定日期的指定日期部分的整数。 SELECT DATEPART(month, '2004-10-15') --返回 105. datename 返回代表指定日期的指定日期部分的字符串 SELECT datename(weekday, '2004-10-15') --返回:星期五6. day(), month(),year() --可以与datepart对照一下select 当前日期=convert(varchar(10),getdate(),120),当前时间=convert(varchar(8),getdate(),114)select datename(dw,'2004-10-15')select 本年第多少周=datename(week,'2004-10-15') ,今天是周几=datename(weekday,'2004-10-15')函数 参数/功能GetDate( ) 返回系统目前的日期与时间DateDiff (interval,date1,date2) 以interval 指定的方式,返回date2 与date1两个日期之间的差值date2-date1DateAdd (interval,number,date) 以interval指定的方式,加上number之后的日期DatePart (interval,date) 返回日期date中,interval指定部分所对应的整数值DateName (interval,date) 返回日期date中,interval指定部分所对应的字符串名称参数 interval的设定值如下:值 缩 写(Sql Server) (Access 和 ASP) 说明Year Yy yyyy 年 1753 ~ 9999Quarter Qq q 季 1 ~ 4Month Mm m 月1 ~ 12Day of year Dy y 一年的日数,一年中的第几日 1-366Day Dd d 日,1-31Weekday Dw w 一周的日数,一周中的第几日 1-7Week Wk ww 周,一年中的第几周 0 ~ 51Hour Hh h 时0 ~ 23Minute Mi n 分钟0 ~ 59Second Ss s 秒 0 ~ 59Millisecond Ms - 毫秒 0 ~ 999access 和 asp 中用date()和now()取得系统日期时间;其中DateDiff,DateAdd,DatePart也同是能用于Access和asp中,这些函数的用法也类似举例:1.GetDate() 用于sql server :select GetDate()2.DateDiff('s','2005-07-20','2005-7-25 22:56:32')返回值为 514592 秒DateDiff('d','2005-07-20','2005-7-25 22:56:32')返回值为 5 天3.DatePart('w','2005-7-25 22:56:32')返回值为 2 即星期一(周日为1,周六为7)DatePart('d','2005-7-25 22:56:32')返回值为 25即25号DatePart('y','2005-7-25 22:56:32')返回值为 206即这一年中第206天DatePart('yyyy','2005-7-25 22:56:32')返回值为 2005即2005年具体的语法:日期函数用来操作DATETIME 和SMALLDATETIME 类型的数据,执行算术运算。与其它函数一样,可以在Select 语句的Select 和Where 子句以及表达式中使用日期函数。其使用方法如下:日期函数参数,其中参数个数应不同的函数而不同。·DAY()DAY() 函数语法如下:DAY (<date_expression>)DAY() 函数返回date_expression 中的日期值。·MONTH()MONTH() 函数语法如下:MONTH (<date_expression>)MONTH() 函数返回date_expression 中的月份值。与DAY() 函数不同的是,MONTH() 函数的参数为整数时,一律返回整数值1,即SQL Server 认为其是1900 年1 月。·YEAR()YEAR() 函数语法如下:YEAR (<date_expression>)YEAR() 函数返回date_expression 中的年份值。提醒:在使用日期函数时,其日期值应在1753年到9999年之间,这是SQL Server系统所能识别的日期范围,否则会出现错误。·DATEADD()DATEADD() 函数语法如下:DATEADD (<datepart>, <number>, <date>)DATEADD() 函数返回指定日期date 加上指定的额外日期间隔number 产生的新日期。参数“datepart” 在日期函数中经常被使用,它用来指定构成日期类型数据的各组件,如年、季、月、日、星期等。其取值如表4-9 所示:·DATEDIFF()DATEDIFF() 函数语法如下:DATEDIFF() (<datepart>, <date1>, <date2>)DATEDIFF() 函数返回两个指定日期在datepart 方面的不同之处,即date2 超过date1的差距值,其结果值是一个带有正负号的整数值。针对不同的datepart, DATEDIFF()函数所允许的最大差距值不一样,如:datepart 为second 时,DATEDIFF() 函数所允许的最大差距值为68: 年datepart 为millisecond 时,DATEDIFF() 函数所允许的最大差距值为24 天20 小时30 分23 秒647 毫秒。·DATENAME()DATENAME() 函数语法如下:DATENAME (<datepart>, <date>DATENAME() 函数以字符串的形式返回日期的指定部分此部分。由datepart 来指定。·DATEPART()DATEPART() 函数语法如下:DATEPART (<datepart>, <date>)DATEPART() 函数以整数值的形式返回日期的指定部分。此部分由datepart 来指定。DATEPART (dd, date) 等同于DAY (date)DATEPART (mm, date) 等同于MONTH (date)DATEPART (yy, date) 等同于YEAR (date)·GETDATE()GETDATE() 函数语法如下:GETDATE()GETDATE() 函数以DATETIME 的缺省格式返回系统当前的日期和时间,它常作为其它函数或命令的参数使用。在开发数据库应用中,经常会遇到处理时间的问题,如查询指定时间的记录等。下面就这些常见的问题,结合自己的一些经验,和大家探讨一下这类问题。 首先介绍一下,SQL Server里处理时间的几个主要函数的用法:getdate()函数:取得系统当前的日期和时间。返回值为datetime类型的。用法:getdate()例子:select getdate() as dte,dateadd(day,-1,getdate()) as nowdat输出结果:dte nowdat1999-11-21 19:13:10.083 1999-11-20 19:13:10.083(1 row(s) affected)datepart()函数:以整数的形式返回时间的指定部分。用法:datepart(datepart,date)参数说明:datepart时要返回的时间的部分,常用取值year、month、day、hour、minute。date是所指定的时间。例子:SELECT DATEPART(month, GETDATE()) AS 'Month Number'输出结果:Month Number11(1 row(s) affected)dateadd()函数:通过给指定的时间的指定部分加上一个整数值以返回一个新时间值。用法:dateadd(datepart,number,date)参数说明:datepart(同上)date(同上)number要增加的值,整型,可正可负,正值返回date之后的时间值,负值返回date之前的时间值例子:select getdate() as todayselect dateadd(day,-1,getdate())select dateadd(day,1,getdate())输出:today1999-11-21 19:42:41.410(1 row(s) affected)yesterday1999-11-20 19:42:41.410(1 row(s) affected)tomorrow1999-11-22 19:42:41.410(1 row(s) affected)datediff()函数:返回两个时间以指定时间部分来计算的差值。返回整数值。如1991-6-12和1991-6-21之间以天来算相差9天,1998-6-12和1999-6-23按年算相差1年,1999-12-1和1999-3-12按月算相差9个月用法:datediff(darepart,date1,date2)参数说明:datepart(同上)date1、date2(同上date)例子:select datediff(month,'1991-6-12','1992-6-21') as a</date></datepart></date></datepart></date2></date1></datepart></date></number></datepart></date_expression></date_expression></date_expression>
bitsCN.com

This article explores optimizing MySQL memory usage in Docker. It discusses monitoring techniques (Docker stats, Performance Schema, external tools) and configuration strategies. These include Docker memory limits, swapping, and cgroups, alongside

This article addresses MySQL's "unable to open shared library" error. The issue stems from MySQL's inability to locate necessary shared libraries (.so/.dll files). Solutions involve verifying library installation via the system's package m

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

This article compares installing MySQL on Linux directly versus using Podman containers, with/without phpMyAdmin. It details installation steps for each method, emphasizing Podman's advantages in isolation, portability, and reproducibility, but also

This article provides a comprehensive overview of SQLite, a self-contained, serverless relational database. It details SQLite's advantages (simplicity, portability, ease of use) and disadvantages (concurrency limitations, scalability challenges). C

This guide demonstrates installing and managing multiple MySQL versions on macOS using Homebrew. It emphasizes using Homebrew to isolate installations, preventing conflicts. The article details installation, starting/stopping services, and best pra

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
