• jbalbo (2/15/2013)


    Hi,

    I want to display only MM/YY from the formula of @enddate -21??

    Thanks

    This type of display should be handled in the front end. SQL is not good at string manipulation like for presentation. Also, you should not use simple math with datetime data like this. You should use DATEADD because the simple math does not work with date or datetime2 datatypes.

    If you are deadset on using a t-sql hammer to force sql to do your presentation you can do something like this.

    declare @enddate datetime = getdate()

    select dateadd(day, -21, @enddate),

    right('0' + cast(month(dateadd(day, -21, @enddate)) as varchar(2)), 2) + '/' + cast(year(dateadd(day, -21, @enddate)) as varchar(4))

    Please note that this is not my recommendation.

    _______________________________________________________________

    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/