• candide (2/25/2013)


    Hi,

    i have a view with two columns, lets say SpecYear and SpecMonth, both are integer.

    How can I build two new columns with the first and last day of this year/month:pinch:

    ex.

    2013 02 => 2013-02-01 2013-02-28

    etc.

    thanx

    This will work.

    declare @Month int = 2, @Year int = 2013

    declare @ThisDate datetime

    set @ThisDate = cast(@Year as char(4)) + right('0' + cast(@Month as varchar(2)), 2) + '01'

    select @ThisDate

    select dateadd(mm, datediff(mm, 0, @ThisDate), 0) -- Beginning of this month

    select dateadd(day, -1, dateadd(mm, datediff(mm, 0, @ThisDate) + 1, 0)) -- End of this month

    Take a look at Lynn's article for a number of datetime routines here. http://www.sqlservercentral.com/blogs/lynnpettis/2009/03/25/some-common-date-routines/[/url]

    If at all possible you should consider storing datetime information as datetime instead of multiple integer columns.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/