MS SQL:将日期时间列转换为 nvarchar
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/273009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MS SQL: Convert Datetime column to nvarchar
提问by Eldila
I need to select a datetime column in a table. However, I want the select statement to return the datetime as a nvarchar with the format DD/MM/YYYY.
我需要在表中选择一个日期时间列。但是,我希望 select 语句将日期时间作为格式为 DD/MM/YYYY 的 nvarchar 返回。
回答by Joel Coehoorn
Assuming sql server, here is the convert documentation:
http://msdn.microsoft.com/en-us/library/ms187928.aspx
假设 sql server,这里是转换文档:http:
//msdn.microsoft.com/en-us/library/ms187928.aspx
Looking through that, it looks like you want style 103:
仔细看看,看起来你想要样式 103:
SELECT CONVERT(nvarchar(10), getdate(), 103)
回答by AlexCuse
This should help. It contains all (or most anyway) the different date formats
这应该有帮助。它包含所有(或大部分)不同的日期格式
http://wiki.lessthandot.com/index.php/Formatting_Dates
http://wiki.lessthandot.com/index.php/Formatting_Dates
I think you'd be better off handling the string conversion in client if possible.
如果可能的话,我认为你最好在客户端处理字符串转换。
回答by Ken Gentle
Geez, I type too slowly...
哎呀,我打字太慢了...
select CONVERT (NVARCHAR, GETDATE(), 103)
回答by HLGEM
Look up convert in BOL.
在 BOL 中查找转换。
回答by Ema.H
You can convert a date in many formats, in your case :
您可以将日期转换为多种格式,在您的情况下:
CONVERT(NVARCHAR(10), YOUR_DATE_TIME, 103) => 15/09/2016
CONVERT(NVARCHAR(10), YOUR_DATE_TIME, 3) => 15/09/16
Syntaxe :
语法:
CONVERT('TheDataTypeYouWant', 'TheDateToConvert', 'TheCodeForFormating' * )
The code is an integer, here 3 is the third formating without century, if you want the century just change the code to 103.
代码是一个整数,这里的 3 是没有世纪的第三个格式,如果你想要世纪,只需将代码更改为 103。
See more at : http://www.w3schools.com/sql/func_convert.asp
回答by tsilb
select convert(nvarchar(10), datefield, 103)