Home Forums SQL Server 2005 T-SQL (SS2K5) How-to-return-next-values-and-concatenate-with_string (SQL Server 2012) RE: How-to-return-next-values-and-concatenate-with_string (SQL Server 2012)

  • mandania (6/29/2015)


    Hi, i tried but the rowNum column is returning null. I can only see the inserted values.

    I just checked the docs and they state that cte's and Row_Number are valid for SQL 2005.

    This is the output that I get when I run the query I posted:

    subIDprodgdatesalesdateprodendDateRowNumNewCol1 NewCol2

    20002006-02-272006-02-272008-04-0402000_0 2006-02-27

    20002006-02-272006-02-272008-04-0412000_1 2006-03-27

    20002006-02-272006-02-272008-04-0422000_2 2006-04-27

    30002009-09-032009-09-032010-04-0103000_0 2009-09-03

    30002009-09-032009-09-032010-04-0113000_1 2009-10-03

    30002009-09-032009-09-032010-04-0123000_2 2009-11-03

    40002006-06-162006-06-162015-04-0304000_0 2006-06-16

    40002006-06-162006-06-162015-04-0314000_1 2006-07-16

    You could try moving the cte into the body of the query:

    select *, CAST(subID as CHAR(4)) + '_' + CAST(RowNum as char(2)) NewCol1, DATEADD(mm, RowNum, prodgdate) NewCol2

    from (

    select subID, prodgdate, salesdate, prodendDate,

    ROW_NUMBER() over(Partition by subID order by prodgdate)-1 RowNum

    from @t

    ) a

    __________________________________________________________________________________________________________
    How to Post to get the most: http://www.sqlservercentral.com/articles/Best+Practices/61537/