search
HomeDatabaseMysql Tutorial数据库中substring的使用方法CONVERT(varchar(12),getdate(),112

Sqlserver 中经常要操作一些时间类型的字段转换,我又不太记得住,所以搜集了以下的一些SqlserverConvertDateTime相关的资料发表在自己的小站里,方便自己以后要用的时候寻找,望对大家也有帮助. 将sqlserver中table表的[datetime]字段值2007-11-0716:41:

Sqlserver中经常要操作一些时间类型的字段转换,我又不太记得住,所以搜集了以下的一些SqlserverConvertDateTime相关的资料发表在自己的小站里,方便自己以后要用的时候寻找,望对大家也有帮助.

将sqlserver中table表的[datetime]字段值‘2007-11-0716:41:35.033’ 改为‘2007-11-0700:00:00‘去除了时分秒.[datetime]字段要为datetime类型的哦. UPDATE table SET[datetime]= Convert(char(11),[datetime],120)

1、获取当前日期利用 convert 来转换成我们需要的datetime格式. 貌似 oracle的 PLSQl中不能直接用呀。。。只适应与 sql server 之类的数据库???
select CONVERT(varchar(12) , getdate(), 112 ) 类似oracle 中的 to_char(xsdate,'yyyymm')
20040912
------------------------------------------------------------
select CONVERT(varchar(12) , getdate(), 102 )
2004.09.12
------------------------------------------------------------
select CONVERT(varchar(12) , getdate(), 101 )
09/12/2004
------------------------------------------------------------
select CONVERT(varchar(12) , getdate(), 103 )
12/09/2004
------------------------------------------------------------
select CONVERT(varchar(12) , getdate(), 104 )
12.09.2004
------------------------------------------------------------
select CONVERT(varchar(12) , getdate(), 105 )
12-09-2004
------------------------------------------------------------
select CONVERT(varchar(12) , getdate(), 106 )
12 09 2004
------------------------------------------------------------
select CONVERT(varchar(12) , getdate(), 107 )
09 12, 2004
------------------------------------------------------------
select CONVERT(varchar(12) , getdate(), 108 )
11:06:08
------------------------------------------------------------
select CONVERT(varchar(12) , getdate(), 109 )
09 12 2004 1
------------------------------------------------------------
select CONVERT(varchar(12) , getdate(), 110 )
09-12-2004
------------------------------------------------------------
select CONVERT(varchar(12) , getdate(), 113 )
12 09 2004 1
------------------------------------------------------------
select CONVERT(varchar(12) , getdate(), 114 )
11:06:08.177
------------------------------------------------------------

declare @dateTime DateTime--定义一个datetime的变量
set @dateTime=getdate(); --获取系统当前时间,并赋值给@dateTime字段

--短日期格式:yyyy-m-d
SELECTREPLACE(CONVERT(varchar(10),@dateTime,120),N'-0','-')

--长日期格式:yyyy年mm月dd日
SELECTSTUFF(STUFF(CONVERT(char(8),@dateTime,112),5,0,N'年'),8,0,N'月')+N'日'

--长日期格式:yyyy年m月d日
SELECT DATENAME(Year,@dateTime)+N'年'+CAST(DATEPART(Month,@dateTime)AS varchar)+N'月'+DATENAME(Day,@dateTime)+N'日'

--完整日期+时间格式:yyyy-mm-dd hh:mi:ss:mmm
SELECTCONVERT(char(11),@dateTime,120)+CONVERT(char(12),@dateTime,114)

------------------------------------------------------------------------------------------------
2、substring ,该函数用来求一个字符串的字串,该函数的使用频率很高。也不是oracle中的用法
示例字符串:”2011-11-17”
在Oracle中求字符串的函数为:substr
The syntax for the substr function is:
substr( string, start_position, [ length ] )
string:源字符串
start_position:子串第一个字符在源字符串中的起始位置
length:子串长度
测试结果:
1.
substr('2011-11-17',0,7)
2011-11
2.
substr('2011-11-17',1,7)
2011-11
3.
substr('2011-11-17',1,10)
2011-11-17
4.
substr('2011-11-17',1,11)
2011-11-17
5.
substr('2011-11-17',-1,7)
7
6.
substr('2011-11-17',-8,8)
11-11-17
7.
substr('2011-11-17',-10,7)
2011-11
8.
substr('2011-11-17',-11,7)
null
9.
substr('2011-11-17',-11)
null
10.
substr('2011-11-17',-1)
7
11.
substr('2011-11-17',6)
11-17
12.
substr('2011-11-17',11)
null
13.
substr (‘2011-11-17’,1,null)
null
14.
substr (‘2011-11-17’,null,1)
null
15.
substr (‘2011-11-17’,null,null)
null
16.
substr ('2011-11-17',1,0)
null
17.
substr ('2011-11-17',1,-1)
null
18.
substr ('2011-11-17',11,11)
null
Oracle中规定:
1) start_position==0时,子串的起始位置为1,即从第一个字符开始;
2) start_position 3) length参数可以缺省

3、日期推算处理

oracle 时间函数(sysdate) to_char才是王道:oracle

1):取得当前日期是本月的第几周

SQL> select to_char(sysdate,'YYYYMMDD W HH24:MI:SS') from dual;
TO_CHAR(SYSDATE,'YY
-------------------
20030327 4 18:16:09
SQL> select to_char(sysdate,'W') from dual;
T
-
4

2:取得当前日期是一个星期中的第几天,注意星期日是第一天

SQL> select sysdate,to_char(sysdate,'D') from dual;
SYSDATE T
--------- -
27-MAR-03 5

  类似:

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:取当前日期是星期几中文显示:

SQL> select to_char(sysdate,'day') from dual;
TO_CHAR(SYSDATE,'DAY')
----------------------
星期四


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) + 0.99999 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;
select last_day(trunc(sysdate)) from dual;
select trunc(last_day(sysdate)) from dual;
select trunc(add_months(sysdate,1),'mm') - 1 from dual;


11: 得到一年的每一天

select trunc(sysdate,'yyyy')+ rn -1 date0
from
(select rownum rn from all_objects
where rownum


12:今天是今年的第N天

SELECT TO_CHAR(SYSDATE,'DDD') FROM DUAL;


13:如何在给现有的日期加上2年

select add_months(sysdate,24) from dual;


14:判断某一日子所在年分是否为润年

select decode(to_char(last_day(trunc(sysdate,'y')+31),'dd'),'29','闰年','平年') from dual;


15:判断两年后是否为润年

select decode(to_char(last_day(trunc(add_months(sysdate,24),'y')+31),'dd'),'29','闰年','平年') from dual;


16):得到日期的季度
select ceil(to_number(to_char(sysdate,'mm'))/3) from dual;
select to_char(sysdate, 'Q') from dual;

四)其余时间处理方法:

DECLARE @dt datetime

SET @dt=GETDATE()
DECLARE @number int
SET @number=3
--1.指定日期该年的第一天或最后一天
--A. 年的第一天
SELECT CONVERT(char(5),@dt,120)+'1-1'
--B. 年的最后一天
SELECT CONVERT(char(5),@dt,120)+'12-31'
--2.指定日期所在季度的第一天或最后一天
--A. 季度的第一天
SELECT CONVERT(datetime,
CONVERT(char(8),
DATEADD(Month,
DATEPART(Quarter,@dt)*3-Month(@dt)-2,
@dt),
120)+'1')
--B. 季度的最后一天(CASE判断法)
SELECT CONVERT(datetime,
CONVERT(char(8),
DATEADD(Month,
DATEPART(Quarter,@dt)*3-Month(@dt),
@dt),
120)
+CASE WHEN DATEPART(Quarter,@dt) in(1,4)
THEN '31'ELSE '30' END)
--C. 季度的最后一天(直接推算法)
SELECT DATEADD(Day,-1,
CONVERT(char(8),
DATEADD(Month,
1+DATEPART(Quarter,@dt)*3-Month(@dt),
@dt),
120)+'1')
--3.指定日期所在月份的第一天或最后一天
--A. 月的第一天
SELECTCONVERT(datetime,CONVERT(char(8),@dt,120)+'1')
--B. 月的最后一天
SELECTDATEADD(Day,-1,CONVERT(char(8),DATEADD(Month,1,@dt),120)+'1')
--C. 月的最后一天(容易使用的错误方法)
SELECT DATEADD(Month,1,DATEADD(Day,-DAY(@dt),@dt))
--4.指定日期所在周的任意一天
SELECTDATEADD(Day,@number-DATEPART(Weekday,@dt),@dt)
--5.指定日期所在周的任意星期几
--A. 星期天做为一周的第1天
SELECTDATEADD(Day,@number-(DATEPART(Weekday,@dt)+@@DATEFIRST-1)%7,@dt)
--B. 星期一做为一周的第1天
SELECTDATEADD(Day,@number-(DATEPART(Weekday,@dt)+@@DATEFIRST-2)%7-1,@dt)

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.