faiz
faiz

Reputation: 93

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

Upvotes: 0

Views: 462

Answers (4)

Agustin Meriles
Agustin Meriles

Reputation: 4854

You could use something like this

EDIT

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

Upvotes: 0

Lamak
Lamak

Reputation: 70638

You can do

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

Upvotes: 0

JNK
JNK

Reputation: 65157

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)

Upvotes: 5

KV Prajapati
KV Prajapati

Reputation: 94645

Use Convert (T-SQL) function.

Upvotes: 0

Related Questions