All we need is an easy explanation of the problem, so here it is.
I have got today’s data as Bigint: 202104171830. How could I cast this Bigint value to today’s date ? Please help. MSSQL
How to solve :
if only date:
select convert(date, substring('202104171830',1,8))
- First use
CAST()to convert it to a string representing that same value
- Then construct a new string my using
SUBSTRING()to pull out the parts and add punctuation as needed, i.e.
SUBSTRING(@TimestampIntAsString, 1, 4)to pull out the year, so you end up with
- Then the
CAST()function will recognise that as an ISO8601-ish datetime that it can convert to a proper
'2021-04-17' and cast to
DATE if you don’t need the time portion.
IIRC none of the conversion functions in SQL Server will recognise a datetime without punctuation as a valid date representing string, as some systems will, so you can’t avoid the tedious mucking about with
Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂