Date format to Int in sql server

Learner 226 Reputation points
2021-07-27T15:38:53.207+00:00

Hi All,

I have a long string like below in my column and I am getting date from that.
My issue is when I am converting that to integer as per my requirement I am not getting the exact output

Example :

declare @Steinar varchar(200)

set @Steinar ='Name - Koteswararao vp Department - Prod Location - Hyderabad Domain - ND Job Title - Technical-ABAP Date of Hire - 2021-07-05 Vendor ID - v2345 Candidate ID - Associate ID - 3334 Name - Hardware Allocation'

select convert(bigint,dateadd(hour,3,substring(@Steinar , PATINDEX('%20[0-9][0-9]-[0-1][0-9]-[0-3][0-9]%', @Steinar ), 10) ))

I ran this and getting output as 44380
If I convert this to date using google , its showing as Thursday, January 1, 1970
But its actual date is Monday, July 5, 2021

Could any one please help

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,839 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,640 questions
{count} votes

Accepted answer
  1. Viorel 117.6K Reputation points
    2021-07-27T16:25:45.613+00:00

    Check if these queries give the expected numbers:

    select datediff_big(second, '1970-01-01', dateadd(hour, 3, substring(@str, PATINDEX('%20[0-9][0-9]-[0-1][0-9]-[0-3][0-9]%', @str), 10) ))
    select datediff_big(second, '1970-01-01', getdate())
    
    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.