开发者

Convert/format a column in a SQL Server 2005 select statement

开发者 https://www.devze.com 2023-01-07 00:02 出处:网络
I have an SQL query which is select DateOfBirth from people, and it shows up in the result pane as DateOfBirth

I have an SQL query which is select DateOfBirth from people, and it shows up in the result pane as

DateOfBirth
07/07/2010 5:08:02
07/09/2010 5:08:02
07/13/2010 5:08:02

I want to format开发者_StackOverflow社区 as,

07/Jul/2010 
09/Jul/2010
13/Jul/2010

NOTE: DateOfBirth column has datatype nvarchar(50), not datetime...


This is a little tricky as the best way to do this is to take the varchar convert it into a datetime and then format it. Annother complication is that the format you want is not a format that SQLServer will output.

So.

SELECT CONVERT(DateTime, DateOfBirth) from people

will get you the date time and we can then convert it to a string format as follows

SELECT CONVERT(DateTime, DateOfBirth), 106) from people

this will produce the string output 'dd Mon YYYY'

then its just a matter of replacing the spaces with '/'

SELECT REPLACE(CONVERT(varchar, CONVERT(DateTime, DateOfBirth), 106), ' ','/')  FROM people

will get you the format you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消