Sherin1589
Sherin1589

Reputation: 15

Converting varchar to datetime

Can anyone please help me with converting 1218100860 to datetime?

I tried like this:

SELECT CONVERT(VARCHAR(11), DATEADD(s,1218100860, '1970-01-01 00:00:00'), 101)

and it works but when I try like this, it throws an error:

SELECT 
     CONVERT(VARCHAR(11), DATEADD(s, rd.Request_Date, '1970-01-01 00:00:00'), 101) 
FROM 
     dbo.RequestDetails rd

where Request_Date is the column where the the value has to be converted to datetime the error is:

Argument data type varchar is invalid for argument 2 of dateadd function.

Upvotes: 1

Views: 6645

Answers (1)

janos
janos

Reputation: 124804

I don't know what RDBMS you're using (maybe sQL Server?), but:

Argument data type varchar is invalid for argument 2 of dateadd function

This means that argument 2 (rd.Request_Date) should not be a varchar. Since in your first example you used an integer and it worked, then it seems you just have to convert rd.Request_Date to integer and it should work, like this:

SELECT CONVERT(VARCHAR(11),DATEADD(s,CONVERT(INT, rd.Request_Date), '1970-01-01 00:00:00'),101 )

Upvotes: 1

Related Questions