Conversion of separate columns for mm, dd and year into a feasible format mm / dd / yr

advertisements

Basically I have 3 separate columns in a table. I will call them SMonth, Sday, Syear. They are stored as numeric values for some reason. I can use the following string to format them into what looks like a date but doesn't allow me to use functions such as sort, order by, datediff or dateadd.

CAST(SMonth AS varchar(2)) + '/' + CAST(SDay varchar(2)) + '/' + CAST(SYear AS varchar(4))

Anyone know how to convert this into a workable date, without changing the table?

It doesn't matter how it looks as long as I can use it ie a date or datetime makes no difference.

Thanks in advance.


Just convert your result into a date or datetime.

DECLARE @SMonth AS INT = 12
DECLARE @SDay AS INT = 31
DECLARE @SYear as INT = 2013
SELECT CONVERT(DATE,CAST(@SMonth AS varchar(2)) + '/' + CAST(@SDay AS varchar(2)) + '/' + CAST(@SYear AS varchar(4)))