简体   繁体   English

我如何在SQL中将格式日期转换为d / M / yyyy并将格式时间转换为h:mm:ss AM

[英]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.

相关问题 在SQL中以YYYY-MM-DD HH:MM:SS格式转换日期和时间字符串 - Convert a date and time string in the format YYYY-MM-DD HH:MM:SS in SQL 如何在 sql 中将日期格式从 YYYY-MM-DD 转换为 YYYY-MM - How can I convert date format from YYYY-MM-DD to YYYY-MM in sql 以hh:mm:ss AM(PM)格式转换时间(sql) - Convert time in hh:mm:ss AM(PM) format (sql) 如何将mm / dd / yyyy格式的日期转换为yyyy-mm-dd hh:mi:ss.mmm - How to convert date in mm/dd/yyyy format to yyyy-mm-dd hh:mi:ss.mmm 如何在sqlite数据库中转换sqlite中的日期格式(从dd/mm/yy到dd/mm/yyyy)和(d/m/yy到dd/mm/yyyy) - how to convert date format in sqlite (from dd/mm/yy to dd/mm/yyyy) and (d/m/yy to dd/mm/yyyy) in sqlite database 以yyyy-MM-dd HH:mm:ss“格式转换日期时间 - Convert the date time in yyyy-MM-dd HH:mm:ss" format 在SQL中以YYYY-MM-DD HH:MM:SS格式转换日期,日期和时间戳记字符串 - Convert a day, date and timestamp string in the format YYYY-MM-DD HH:MM:SS in SQL 如何在mssql中将NVARCHAR值转换为dd-mm-yyyy hh:mm:ss AM / PM格式 - How to convert NVARCHAR value to dd-mm-yyyy hh:mm:ss AM/PM format in mssql 尝试以(yyyy-mm-dd hh:mm:ss)日期格式转换字符串格式(dd-mm-yyyy hh:mm:ss) - Trying to convert string format (dd-mm-yyyy hh:mm:ss) in (yyyy-mm-dd hh:mm:ss) date format pyspark sql 将日期格式从 mm/dd/yy hh:mm 或 yyyy-mm-dd hh:mm:ss 转换为 yyyy-mm-dd hh:mm 格式 - pyspark sql convert date format from mm/dd/yy hh:mm or yyyy-mm-dd hh:mm:ss into yyyy-mm-dd hh:mm format
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM