Under the hood, a SQL Server DateTime
is a tuple of 2 32-bit integers:
- The first integer is a count of days since since the epoch, which for SQL Server is
1 January 1900
- The second integer is a count of milliseconds since start of day (
00:00:00.000
). Except that the count ticks up in 3- or 4-milliscond increments. Microsoft only knows why that decision was made.
You can get the count of days since the epoch with
convert( int, convert( date, t.call_date ) )
[wrap that in convert(varchar, ... )
to turn it into a string]
Looks like your id
is already a varchar
, so you can say:
select compound_key = convert(varchar,
convert(int,
convert(date,
call_date
)
)
)
+ t.id
from test t
I would suggest padding both fields with leading zeros to a fixed length so as to avoid possible collisions (assuming you're trying to generate a key here). Signed 32-bit integer overflows a 2.1 billion-ish, so 9 digits for each field is sufficient.