DiigIT | IT Community
No Profile Image
Welcome Guest
New User? Register | Login
SQL
IT Tags
DiigIT » SQL » qna

Converting to Date Time

By: rekha singh | 12 May 2010 11:15 am

I have a table with three columns Month (values JAN,FEB,MAR, APR) etc

Monthno (values 1-12)
Year 2009,2010, etc
 
How do I take the above fields and create a date that represents the first day of the month?
 
i.e. Convert Jan 2009 to 1/1/2009
 
I don't need hours:minutes: seconds
 
 
Thanks for any suggestions.
 

Comments

 Something like this could work:

 
cast(( cast( [MonthNo] as varchar(2)) + '/01/' + cast( [Year] as varchar(4))) as date )
 
the 'date' datatype is new with SQL Server 2008. If you are using SQL Server 2005, cast as a datetime datatype instead.
 
Regards,
By: rekha singh | 12 May 2010
HI , 
 
 select convert(varchar, '1' + space(2) + [MONTH] + space(2) + [year], 102) from datetable
 
Try this , this will be usefull. and let me know in case of any concern.
I would like to help you.
 
[month] , [year] its your column name of datetable table
 
Thanks and Regards

 

By: rekha singh | 12 May 2010

 DATEADD(mm,DATEDIFF (mm,0,theDate) ,0

 
Wouldn't work to get the 01 for the day of any passed in date in place of the CASTING? I guess really which would be more efficient to the server cpu?
 
By: rekha singh | 12 May 2010

Leave a comment

Enter the text in the image
img
Can't read?
Type the characters you see in the picture below.


Close Move