Norcarde Norcarde - 3 months ago 26
SQL Question

Add numeration in columns sql

I have the following chart:

enter image description here

I wonder if there is any way to make a query, I would like to add a numbering as shown in the following example.
enter image description here

The columns of my table (fechaSuspencion, plazoSuspencion, fechaTerminacion)
a number that is added increases.

He did not know what title is appropriate.

Answer

You can use the ROW_NUMBER windowing function. Sample code below:

declare @test  table (fechaSuspencion datetime, plazoSuspencion varchar(50), fechaTerminacion datetime)
insert into @test values ('20161212', '5 meses', '20160112')
select 
    'fechaSuspencion' + ltrim(rtrim(str(row_number() over (order by fechaSuspencion)))) as fsnumber
    ,fechaSuspencion
    ,'plazoSuspencion' + ltrim(rtrim(str(row_number() over (order by fechaSuspencion)))) as psnumber
    ,plazoSuspencion
    ,'fechaTerminacion' + ltrim(rtrim(str(row_number() over (order by fechaSuspencion)))) as psnumber
    ,fechaTerminacion
from
    @test