[英]Convert Date format into DD/MMM/YYYY format in SQL Server
I have a query in SQL
, I have to get a date in a format of dd/mm/yy
我在SQL
有一个查询,我必须以dd/mm/yy
格式获取日期
Example: 25/jun/2013
.示例: 25/jun/2013
。
How can I convert
it for SQL server
?如何convert
其convert
为SQL server
?
There are already multiple answers and formatting types for SQL server 2008. But this method somewhat ambiguous and it would be difficult for you to remember the number with respect to Specific Date Format. SQL server 2008 已经有多种答案和格式类型。但是这种方法有些含糊,您很难记住特定日期格式的数字。 That's why in next versions of SQL server there is better option.这就是为什么在下一版本的 SQL Server 中有更好的选择。
FORMAT ( value, format [, culture ] )
With culture option, you can specify date as per your viewers.使用文化选项,您可以根据观众指定日期。
DECLARE @d DATETIME = '10/01/2011';
SELECT FORMAT ( @d, 'd', 'en-US' ) AS 'US English Result'
,FORMAT ( @d, 'd', 'en-gb' ) AS 'Great Britain English Result'
,FORMAT ( @d, 'd', 'de-de' ) AS 'German Result'
,FORMAT ( @d, 'd', 'zh-cn' ) AS 'Simplified Chinese (PRC) Result';
SELECT FORMAT ( @d, 'D', 'en-US' ) AS 'US English Result'
,FORMAT ( @d, 'D', 'en-gb' ) AS 'Great Britain English Result'
,FORMAT ( @d, 'D', 'de-de' ) AS 'German Result'
,FORMAT ( @d, 'D', 'zh-cn' ) AS 'Chinese (Simplified PRC) Result';
US English Result Great Britain English Result German Result Simplified Chinese (PRC) Result
---------------- ----------------------------- ------------- -------------------------------------
10/1/2011 01/10/2011 01.10.2011 2011/10/1
US English Result Great Britain English Result German Result Chinese (Simplified PRC) Result
---------------------------- ----------------------------- ----------------------------- ---------------------------------------
Saturday, October 01, 2011 01 October 2011 Samstag, 1. Oktober 2011 2011年10月1日
For OP's solution, we can use following format, which is already mentioned by @Martin Smith:对于 OP 的解决方案,我们可以使用@Martin Smith 已经提到的以下格式:
FORMAT(GETDATE(), 'dd/MMM/yyyy', 'en-us')
Some sample date formats:一些示例日期格式:
If you want more date formats of SQL server, you should visit:如果你想要更多 SQL 服务器的日期格式,你应该访问:
Try using the below query.尝试使用以下查询。
SELECT REPLACE(CONVERT(VARCHAR(11),GETDATE(),6), ' ','/');
Result: 20/Jun/13结果: 20/6/13
SELECT REPLACE(CONVERT(VARCHAR(11),GETDATE(),106), ' ','/');
Result: 20/Jun/2013结果: 20/Jun/2013
The accepted answer already gives the best solution using built in formatting methods in 2008.接受的答案已经在 2008 年使用内置格式方法提供了最佳解决方案。
It should be noted that the results returned is dependent on the language of the login however.应该注意的是,返回的结果取决于登录的语言。
SET language Russian
SELECT replace(CONVERT(NVARCHAR, getdate(), 106), ' ', '/')
Returns退货
06/апр/2015
at the time of writing.在撰写本文时。
For people coming across this question on more recent versions of SQL Server a method that avoids this issue - and the need to REPLACE
is对于在较新版本的 SQL Server 上遇到此问题的人来说,一种避免此问题的方法 - 并且需要REPLACE
FORMAT(GETDATE(),'dd/MMM/yyyy', 'en-us')
On 2005+ it would be possible to write a CLR UDF that accepted a DateTime, Formatting Pattern and Culture to simulate the same.在 2005+ 上,可以编写一个接受日期时间、格式模式和文化的 CLR UDF 来模拟相同的内容。
Try this试试这个
select convert(varchar,getdate(),100)
third parameter is format, range is from 100
to 114
, any one should work for you.第三个参数是格式,范围是100
到114
,任何一个都适合你。
If you need date in dd/mmm/yyyy
use this:如果您需要dd/mmm/yyyy
日期,请使用以下命令:
replace(convert(char(11),getdate(),113),' ','-')
Replace getdate()
with your column name.将getdate()
替换为您的列名。 This worked for me.这对我有用。
You can use this query-您可以使用此查询-
SELECT FORMAT (getdate(), 'dd/MMM/yy') as date
Hope, this query helps you.希望,此查询对您有所帮助。
Thanks!!谢谢!!
试试这个 :
select replace ( convert(varchar,getdate(),106),' ','/')
任何试图手动输入日期到 sql server 'date type' 变量的人在输入时使用此格式:
'yyyy-mm-dd'
Just format after convert to a date (tested in SQL Server v.2017)转换为日期后仅格式化(在 SQL Server v.2017 中测试)
dd/mon/yyyy
Select Format(cast('25/jun/2013' as date),'dd/MMM/yyyy')
dd/mm/yyyy
Select Format(cast('25/jun/2013' as date),'dd/MM/yyyy')
dd/Month/yyyy
Select Format(cast('25/jun/2013' as date),'dd/MMMM/yyyy')
Simply get date and convert只需获取日期并转换
Declare @Date as Date =Getdate()
Select Format(@Date,'dd/MM/yyyy') as [dd/MM/yyyy] // output: 22/10/2020
Select Format(@Date,'dd-MM-yyyy') as [dd-MM-yyyy] // output: 22-10-2020
//string date
Select Format(cast('25/jun/2013' as date),'dd/MM/yyyy') as StringtoDate // output: 25/06/2013
Source: SQL server date format and converting it (Various examples)来源: SQL server 日期格式和转换(各种例子)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.