Decimal to Date in SQL

Ramana Kopparapu 306 Reputation points
2023-12-06T17:34:02.5933333+00:00

Hi,

I have a column MGDT having decimal data type in one table. I need to convert it to date and populate the data into NMDT column in other table whose data type is datetime2 in SQL SERVER..

The value in the column MGDT - 202310

Want to get output like : 2023-10-01 00:00:00 0000000

Could anyone please help me in this regard?

SQL Server Other
0 comments No comments
{count} votes

Accepted answer
  1. Dan Guzman 9,401 Reputation points
    2023-12-06T19:29:25.3266667+00:00

    One method to convert the decimal YYYYMM value to a datetime2 is to append '01' to form an ISO 8601 date format string (YYYYMMDD). The example below also includes TRY_CAST to will set invalid dates to NULL.

    SET MNDT = TRY_CAST(CAST(MGDT AS char(6)) + '01' AS datetime2)

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.