Miguel Mas Miguel Mas - 21 days ago 5
SQL Question

TSQL: error while converting timestap to integer

I have:

cast(convert(varchar(255), CURRENT_TIMESTAMP, 121) as int) as MYDATENUMBER


Being 121: yyyy-mm-dd hh:mi:ss.mmm (24h)

This is the error that I get:

Conversion failed when converting the varchar value '2016-11-16 10:28:47.330' to data type int.

MYDATENUMBER is defined as INT

Any idea on why I am getting this error?

Thanks!

Answer

You are converting CURRENT_TIMESTAMP to a varchar(255) and then casting a varchar to an int

try:

select convert(int, CURRENT_TIMESTAMP, 121)  MYDATENUMBER