Friday, February 24, 2012

Convert MMYY ?

Hi all,

I am having a problem at a customers site: They are using a month field consisting of 4 digits. '1105' means November 2005. The field is a varchar(6) field. How can I convert this field to a date field, or read it as such? 'Dateadd' doesn't seem to work. There is no need for dates, only months and years are relevant.

Thx

Regards

Birger
You can't have a date without the day. So, my suggestion would be:

SELECT month_column = CONVERT(SMALLDATETIME,

'20'+RIGHT(month_column,2)+LEFT(month_column,2)+'01')

FROM table_unspecified

You can wrap this in a view to make it more convenient. Or, change the

column to a smalldatetime, and show the customer how to populate it

correctly. '1105' is so ambiguous it's not funny... that could be May 11th

or November 5th (never mind, what year?) or it could be the time of day (AM

or PM?).
wrote in message

news:4e97a86a-1023-40a9-a902-d79b24d9e6eb@.discussions.microsoft.com...

> Hi all,

>

> I am having a problem at a customers site: They are using a month field

> consisting of 4 digits. '1105' means November 2005. The field is a

> varchar(6) field. How can I convert this field to a date field, or read

> it as such? 'Dateadd' doesn't seem to work. There is no need for dates,

> only months and years are relevant.

>

> Thx

>

> Regards

>

> Birger

>

>

No comments:

Post a Comment