[英]How i can convert format date to d/M/yyyy and convert format time to h:mm:ss AM in SQL
I have created a field of type datetime. 我创建了一个类型为datetime的字段。 I want to display this in YYYY/M/D format
我想以YYYY / M / D格式显示
example 例
date 09/09/2014 to display as 2014/9/9 日期为2014年9月9日,显示为2014/9/9
Also I need display a time field [Time_Orde] in this format 2:43:14 PM 我还需要以以下格式显示时间字段[Time_Orde]:2:43:14 PM
I use SQL server 2008 我使用SQL Server 2008
I have tried the function 我已经试过了
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )
My code: 我的代码:
SELECT CONVERT(date,Order_Date) from Orders_Teakeaway
But I could not get this format 2014/9/9 但我无法获得此格式2014/9/9
There is no single style number that will give you YYYY/M/D 2014/9/9 没有单一样式编号可为您提供YYYY / M / D 2014/9/9
Try this: 尝试这个:
convert(varchar(5),Order_Date,111) + replace(convert(varchar(5),Order_Date,1),'0','')
Style 111 is YYYY/MM/DD but we only take the first 5 chars (YYYY/) Style 1 is MM/DD/YY but again we only take the first 5 chars (MM/DD) then replace any zero's in the MM/DD part and concatenate 样式111为YYYY / MM / DD,但我们仅采用前5个字符(YYYY /)。样式1为MM / DD / YY,但同样,我们仅采用前5个字符(MM / DD),然后替换MM /中的任何零。 DD部分并连接
For the time, try this 暂时尝试一下
left(right(CONVERT(VARCHAR(20), Time_Orde, 100),7),5)
+ left(right(CONVERT(VARCHAR(27), Time_Orde, 9),9),3)
+ right(CONVERT(VARCHAR(20), Time_Orde, 100),2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.