开发者

Convert DateTime to Varchar

I am working on a query in Sql Server 2008 where I'm migrating the date to a different table.

The source table has a 开发者_如何学编程column with DateTime and the destination column has varchar column. I need to convert a DateTime values into varchar.

Source Column has DateTime: 2007-02-13 00:00:00.000

Destination Column has Varchar: mm/dd format


Let me preface with:

THIS IS A TERRIBLE IDEA

If your data represents a date, it needs to be a date datatype!

If you insist on going forward, though, you can do something along the lines of:

SELECT CAST(MONTH(Datefield) as varchar) + '/' + CAST(DAY(Datefield) as varchar)


Use Convert (T-SQL) function.


You can do

SELECT CONVERT(VARCHAR(10),YourDate,101) NewDate
FROM YourTable


You could use something like this

EDIT

SELECT CAST(DATEPART(MM, YourDateTimeField) AS VARCHAR(2)) + '/' + CAST(DATEPART(DAY, YourDateTimeField) AS VARCHAR(2))

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜