search
Homephp教程PHP开发SQL Server Convert DateTime format conversion

Change the [datetime] field value of the table table in sqlserver from '2011-11-07 16:41:35.033' to '2011-11-07 00:00:00' and remove the hours, minutes and seconds. The [datetime] field must be datetime Type. UPDATE table SET [datetime]= Convert(char(11),[datetime],120)

 Get the current date and use convert to convert it into the datetime format we need.

 select CONVERT(varchar(12), getdate(), 112 )

 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--Define a datetime variable

 set @dateTime =getdate(); --Get the current time of the system and assign it to the @dateTime field

  --Short date format: yyyy-m-d

  SELECT REPLACE(CONVERT(varchar(10),@dateTime,120),N'- 0','-')

 --Long date format: yyyy year mm month dd day

SELECT STUFF(STUFF(CONVERT(char(8),@dateTime,112),5,0,N'year') ,8,0,N'month')+N'day'

 --Long date format: yyyy year m month d day

  SELECT DATENAME(Year,@dateTime)+N'year'+CAST(DATEPART(Month ,@dateTime) AS varchar)+N'month'+DATENAME(Day,@dateTime)+N'日'

  --Complete date + time format: yyyy-mm-dd hh:mi:ss:mmm

SELECT CONVERT(char(11),@dateTime,120)+CONVERT(char(12),@dateTime,114)

 -------------------------- -------------------------------------------------- -----------------------

 2. Date calculation processing

 DECLARE @dt datetime

 SET @dt=GETDATE()

 DECLARE @number int

  SET @number=3

  --1. Specify the first day or last day of the year

  --A. The first day of the year

  SELECT CONVERT(char(5),@dt,120 ; One day or the last day

 --A. The first day of the quarter

  SELECT CONVERT(datetime,CONVERT(char(8),DATEADD(Month,DATEPART(Quarter,@dt)*3-Month(@dt)- 2,@dt),120)+'1')

  --B. The last day of the quarter (CASE judgment method)

  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. The last day of the quarter (direct calculation method)

SELECT DATEADD(Day,-1,CONVERT(char(8),DATEADD(Month,1+DATEPART(Quarter,@dt)*3-Month(@dt) ),@dt),120)+'1')

 --3. The first or last day of the month where the specified date is located

 --A. The first day of the month

  SELECT CONVERT(datetime,CONVERT( char(8),@dt,120)+'1')

   --B. The last day of the month

  SELECT DATEADD(Day,-1,CONVERT(char(8),DATEADD(Month,1,@dt ),120)+'1')

 --C. The last day of the month (easy to use wrong method)

  SELECT DATEADD(Month,1,DATEADD(Day,-DAY(@dt),@dt))

   --4. Any day of the week where the specified date falls

  SELECT DATEADD(Day,@number-DATEPART(Weekday,@dt),@dt)

   --5. Any day of the week where the specified date falls

 --A. Sunday is the first day of the week

 SELECT DATEADD(Day,@number-(DATEPART(Weekday,@dt)+@@DATEFIRST-1)%7,@dt)

 --B. Monday is the first day of the week

SELECT DATEADD(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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool