Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Date format statement when SqlServer query

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

The default date field datetime format for SQL Server Chinese is y-mm-dd Thh:mm:ss.mmm

For example:

select getdate() 2004-09-12 11:06:08.177

SQL Server may be used to convert the date format:

Examples are as follows:

select CONVERT(varchar, getdate(), 120 ) 2004-09-12 11:06:08 select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),'-',''),' ',''),':','') 20040912110608 select CONVERT(varchar(12) , getdate(), 111 ) 2004/09/12 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

Convert The third parameter, the format for date type conversion:

Convert(Char(10), GetDate(), 111)

101 United States mm/dd/yy

102 ANSI yy.mm.dd 103 UK/France dd/mm/yy

Germany dd.mm.yy

105 Italy dd-mm-yy

106 - dd mon yy

107 - mon dd, yy

108 - hh:mm:ss

109 (*) Default + ms mon dd yy hh:mi:ss:mmmAM (or PM)

110 United States mm-dd-yy

111 Japan yy/mm/dd

112 ISO yymmdd

113 (*) European default + ms dd mon yy hh:mm:ss:mmm(24h)

114 - hh:mi:ss:mmm(24h)

120 (*) ODBC specification y-mm-dd hh:mm:ss[.fff]

121 (*) ODBC specification with milliseconds y-mm-dd hh:mm:ss[.fff]

126(***) ISO8601 y-mm-dd Thh:mm:ss:mmm (without spaces)

130* Kuwait dd mon yhh:mi:ss:mmmAM

131* Kuwait dd/mm/yy hh:mi:ss:mmmAM

A very powerful date formatting function in SQL Server

Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AMSelect CONVERT(varchar(100), GETDATE(), 1): 05/16/06Select CONVERT(varchar(100), GETDATE(), 2): 06.05.16Select CONVERT(varchar(100), GETDATE(), 3): 16/05/06Select CONVERT(varchar(100), GETDATE(), 4): 16.05.06Select CONVERT(varchar(100), GETDATE(), 5): 16-05-06Select CONVERT(varchar(100), GETDATE(), 6): 16 05 06Select CONVERT(varchar(100), GETDATE(), 7): 05 16, 06Select CONVERT(varchar(100), GETDATE(), 8): 10:57:46Select CONVERT(varchar(100), GETDATE(), 9): 05 16 2006 10:57:46:827AMSelect CONVERT(varchar(100), GETDATE(), 10): 05-16-06Select CONVERT(varchar(100), GETDATE(), 11): 06/05/16Select CONVERT(varchar(100), GETDATE(), 12): 060516Select CONVERT(varchar(100), GETDATE(), 13): 16 05 2006 10:57:46:937Select CONVERT(varchar(100), GETDATE(), 14): 10:57:46:967Select CONVERT(varchar(100), GETDATE(), 20): 2006-05-16 10:57:47Select CONVERT(varchar(100), GETDATE(), 21): 2006-05-16 10:57:47.157Select CONVERT(varchar(100), GETDATE(), 22): 05/16/06 10:57:47 AMSelect CONVERT(varchar(100), GETDATE(), 23): 2006-05-16Select CONVERT(varchar(100), GETDATE(), 24): 10:57:47Select CONVERT(varchar(100), GETDATE(), 25): 2006-05-16 10:57:47.250Select CONVERT(varchar(100), GETDATE(), 100): 05 16 2006 10:57AMSelect CONVERT(varchar(100), GETDATE(), 101): 05/16/2006Select CONVERT(varchar(100), GETDATE(), 102): 2006.05.16Select CONVERT(varchar(100), GETDATE(), 103): 16/05/2006Select CONVERT(varchar(100), GETDATE(), 104): 16.05.2006Select CONVERT(varchar(100), GETDATE(), 105): 16-05-2006Select CONVERT(varchar(100), GETDATE(), 106): 16 05 2006Select CONVERT(varchar(100), GETDATE(), 107): 05 16, 2006Select CONVERT(varchar(100), GETDATE(), 108): 10:57:49Select CONVERT(varchar(100), GETDATE(), 109): 05 16 2006 10:57:49:437AMSelect CONVERT(varchar(100), GETDATE(), 110): 05-16-2006Select CONVERT(varchar(100), GETDATE(), 111): 2006/05/16Select CONVERT(varchar(100), GETDATE(), 112): 20060516Select CONVERT(varchar(100), GETDATE(), 113): 16 05 2006 10:57:49:513Select CONVERT(varchar(100), GETDATE(), 114): 10:57:49:547Select CONVERT(varchar(100), GETDATE(), 120): 2006-05-16 10:57:49Select CONVERT(varchar(100), GETDATE(), 121): 2006-05-16 10:57:49.700Select CONVERT(varchar(100), GETDATE(), 126): 2006-05-16T10:57:49.827Select CONVERT(varchar(100), GETDATE(), 130): 18 ???? ?????? 1427 10:57:49:907AMSelect CONVERT(varchar(100), GETDATE(), 131): 18/04/1427 10:57:49:920AM

Commonly used:

Select CONVERT(varchar(100), GETDATE(), 24): 10:57:47Select CONVERT(varchar(100), GETDATE(), 108): 10:57:49Select CONVERT(varchar(100), GETDATE(), 12): 060516Select CONVERT(varchar(100), GETDATE(), 23): 2006-05-16select CONVERT(varchar(12) , getdate(), 112 ) : 20040912 Select CONVERT(varchar(100), GETDATE(), 8): 10:57:46

For example:

convert(varchar(100),a.makedate,120) as makedate

PS: The following introduces Sql Server about date query, if the date in the table to a specific time

1. If the query date parameter is '2017/02/21', and the field in the database table is'2017/02/21 12:34: 16.963', you need to format the date to query it, as follows

select * from table t where t.date between CONVERT(datetime, '2017/02/21', 120) and CONVERT(datetime, '2017/02/21', 120)+' 23:59:59') ;

The query range is '2017/02/21 00:00:00'~'2017/02/21 23:59: 59', which will solve the problem.

2. Or use the dateadd method to add 1 day to the date, as follows

select * from table t where t.date >= CONVERT(datetime, '2017/02/21') and t.date < CONVERT(datetime, dateadd(day,1,'2017/02/21'));

The scope of the query is '2017/02/21'

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report