Search code examples
sqlt-sqlsql-server-2012row-numberdate-conversion

How to generate uniqueRowdate number for a datecolumn i?


I have a date column in my table i need to generate a unique rowdate for date change.I have used CONVERT(varchar(50),cast(product as DATE),112) but i am not getting unique for date. My Table look likes below.

Product 
02/22/2014
02/22/2014
02/22/2014
02/22/2014
02/23/2014
02/23/2014
02/23/2014
02/23/2014
02/23/2014
02/24/2014
02/24/2014
02/24/2014

The output look to be

201402221
201402222
201402223
201402224
201402231
201402232
201402233
201402234
201402235
201402241
201402242
201402243

Note:I need it because no value should be matched.Please help me guys i tried 80% by using above formula but i am not getting any idea adding row numbers for date??


Solution

  • select concat(replace(cast(product as date), '-', ''),
                  row_number() over(partition by product order by product)) as prod_id
      from tbl
    

    SQL Fiddle demo: http://sqlfiddle.com/#!6/0a526/7/0