how to get date format from known month and year in sql server [Resolved]

Posted by Amritha444 under Sql Server on 6/18/2011 | Points: 10 | Views : 13911 | Status : [Member] | Replies : 4
Hi all

how to get date format from known month and year in sql server

eg: i have to form a date using month 5 and year 2011..the new should be in format date 05/01/2011.

how to get it

Thanks in Advance




Responses

Posted by: Ndebata on: 6/20/2011 [Member] Starter | Points: 50

Up
0
Down

Resolved
Hi
As there is no date , it can be defaulted to 1st of that month. You cast the string to datetime as bellow
Declare @Month int,@Year int

Set @Month=5
Set @Year=2011
Select CAST(CAST(@Month as varchar)+'/1/'+CAST(@Year as varchar) as DateTime)


Thanks,
Debata

Amritha444, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Lakn2 on: 6/20/2011 [Member] Starter | Points: 50

Up
0
Down

Resolved
DECLARE @m_year INT

DECLARE @m_month INT
SET @m_year = 2011
SET @m_month = 5
SELECT CAST(CONVERT(varchar, @m_year) + '-' + CONVERT(varchar, @m_month) + '-01' AS DATETIME)


Thanks&Regards
LakshmiNarayana Nalluri.

Amritha444, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Reena_jainy2k1 on: 6/18/2011 [Member] Starter | Points: 25

Up
0
Down
hi,

Just use convert function like this

select * from table where CONVERT(VARCHAR(10), dbo, 111)

or

Select * from table1 where Convert(datetime, dbo, 101)

this will return you the dbo field in mm/dd/yyyy format

Hope this will help you

Reena Jain
Project Manager

Amritha444, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Amritha444 on: 6/18/2011 [Member] Starter | Points: 25

Up
0
Down
I really meant not this

i want date format from known month and year

i have only month and year i want to convert it in to date format.how its possible in sqlserver

Amritha444, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response