Converting to mm/dd/yyyy format

[亡魂溺海] 提交于 2019-12-21 02:43:21

问题


I have a table called SF_Data and there is a column called IN_Date, ID the data looks like:

ID      IN_Date
1       9/8/2010
2       26/04/2011
3       20/09/2010

The datatatype of IN_Date is varchar(50).

I am trying to convert the IN_Date to mm/dd/yyyy format. I tried doing this:

Select convert(varchar,IN_Date,103) From dbo.SF_Data

But still the format doesn't change. Can anyone tell me where I am going wrong


回答1:


You need a convert to fix the data (to the correct datatype) before formatting...

Select
     convert(varchar,
         convert(date, IN_Date, 103),
     101)
from dbo.SF_Data



回答2:


The 3rd parameter to convert has no meaning when converting from varchar to varchar. So per @marc_s' comment, you'd have to convert the varchar to a datetime using the 103 format, and then from datetime to varchar specifying the 101 format:

Select convert(varchar(12),convert(datetime,IN_Date,103),101) From dbo.SF_Data

For example:

select convert(varchar(12),convert(datetime,'31/12/2001',103),101)

prints 12/31/2001.

See MSDN.



来源:https://stackoverflow.com/questions/7248329/converting-to-mm-dd-yyyy-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!