Frage

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??

War es hilfreich?

Lösung

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

Andere Tipps

You can use ROW_NUMBER()

SELECT ROW_NUMBER() OVER( ORDER BY Product) AS rn, *
FROM your_table  

The ROW_NUMBER() function would generate count starting with 1. In case you want to initialize the count from some arbitrary value say 201402221, then you can add a base count to count column like below:

SELECT ROW_NUMBER() OVER( ORDER BY Product) + 201402220 AS rn, *
FROM your_table 
declare @FromDate date = '2014-03-05'
declare @ToDate date = '2014-12-31'

select dateadd(day,rand(checksum(newid()))*(1+datediff(day, @FromDate, @ToDate)),@FromDate)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top