Convert Varchar To Datetime and Add Seconds
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
I have the following two columns in my table
callstarttime callduration
20180101215910 120
20180101215710 220
The first column's data type is varchar(255) whilst the second column is int.
What I want is to add the callduration to the callstarttime inorder to get the time which the call ended.
So I need to first convert the callstarttime column to datetime and then use the Dateadd function.
My script is as follows:
select convert(datetime, callstarttime) from Mytable
I have also tried using cast as follows
select cast(callstarttime as datetime) from Mytable
However I am getting the following error
Conversion failed when converting date and/or time from character string.
Similar questions have been asked on Convert varchar column to datetime and how to convert this varchar to datetime format?, however I can't tailor my script based on the solutions provided there.
sql-server sql-server-2016 type-conversion
New contributor
db100 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
2
down vote
favorite
I have the following two columns in my table
callstarttime callduration
20180101215910 120
20180101215710 220
The first column's data type is varchar(255) whilst the second column is int.
What I want is to add the callduration to the callstarttime inorder to get the time which the call ended.
So I need to first convert the callstarttime column to datetime and then use the Dateadd function.
My script is as follows:
select convert(datetime, callstarttime) from Mytable
I have also tried using cast as follows
select cast(callstarttime as datetime) from Mytable
However I am getting the following error
Conversion failed when converting date and/or time from character string.
Similar questions have been asked on Convert varchar column to datetime and how to convert this varchar to datetime format?, however I can't tailor my script based on the solutions provided there.
sql-server sql-server-2016 type-conversion
New contributor
db100 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have the following two columns in my table
callstarttime callduration
20180101215910 120
20180101215710 220
The first column's data type is varchar(255) whilst the second column is int.
What I want is to add the callduration to the callstarttime inorder to get the time which the call ended.
So I need to first convert the callstarttime column to datetime and then use the Dateadd function.
My script is as follows:
select convert(datetime, callstarttime) from Mytable
I have also tried using cast as follows
select cast(callstarttime as datetime) from Mytable
However I am getting the following error
Conversion failed when converting date and/or time from character string.
Similar questions have been asked on Convert varchar column to datetime and how to convert this varchar to datetime format?, however I can't tailor my script based on the solutions provided there.
sql-server sql-server-2016 type-conversion
New contributor
db100 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I have the following two columns in my table
callstarttime callduration
20180101215910 120
20180101215710 220
The first column's data type is varchar(255) whilst the second column is int.
What I want is to add the callduration to the callstarttime inorder to get the time which the call ended.
So I need to first convert the callstarttime column to datetime and then use the Dateadd function.
My script is as follows:
select convert(datetime, callstarttime) from Mytable
I have also tried using cast as follows
select cast(callstarttime as datetime) from Mytable
However I am getting the following error
Conversion failed when converting date and/or time from character string.
Similar questions have been asked on Convert varchar column to datetime and how to convert this varchar to datetime format?, however I can't tailor my script based on the solutions provided there.
sql-server sql-server-2016 type-conversion
sql-server sql-server-2016 type-conversion
New contributor
db100 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
db100 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 22 hours ago


Michael Green
13.2k82857
13.2k82857
New contributor
db100 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked yesterday
db100
111
111
New contributor
db100 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
db100 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
db100 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
Let me know how this works for you.
CREATE TABLE #t (callstarttime VARCHAR(255), callduration INT);
INSERT #t ( callstarttime, callduration )
SELECT *
FROM (
VALUES('20180101215910', 120),('20180101215710', 220)
) AS x (callstarttime, callduration);
WITH munge AS (
SELECT *,
LEFT(callstarttime, 8) AS d,
STUFF(STUFF(SUBSTRING(callstarttime, 9, LEN(callstarttime)) , 3, 0, ':'), 6, 0, ':') AS t
FROM #t AS t
)
SELECT *,
TRY_CONVERT(DATETIME, d + ' ' + t),
DATEADD(SECOND, munge.callduration, TRY_CONVERT(DATETIME, d + ' ' + t))
FROM munge
add a comment |Â
up vote
1
down vote
The problem, as I'm sure you're aware, is that SQL Server requires certain punctuation between the numerals before it will consider a character sequence to be a valid datetime. sp_BlitzErik has shown one way to inject those characters. Here's another using the FORMAT function introduced in SQL Server 2012:
declare @s varchar(255) = '20180101215910';
declare @i bigint = @s; -- implicit type conversion
select
Raw = @i,
WithSeparators = FORMAT(@i, '####-##-##T##:##:##'),
AsDateTime = CONVERT(datetime, FORMAT(@i, '####-##-##T##:##:##')),
Incremented = DATEADD(SECOND, 120, CONVERT(datetime, FORMAT(@i, '####-##-##T##:##:##')));
Raw WithSeparators AsDateTime Incremented
-------------- -------------------- ----------------------- -----------------------
20180101215910 2018-01-01T21:59:10 2018-01-01 21:59:10.000 2018-01-01 22:01:10.000
I have to convert to integer type because that is one that FORMAT will accept. It has to be BIGINT due to the number of digits.
Performance-wise I think you would find it very difficult to measure the difference between the two techniques.
This may have a smaller cognitive load, depending on how familiar you are with reading T-SQL.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
Let me know how this works for you.
CREATE TABLE #t (callstarttime VARCHAR(255), callduration INT);
INSERT #t ( callstarttime, callduration )
SELECT *
FROM (
VALUES('20180101215910', 120),('20180101215710', 220)
) AS x (callstarttime, callduration);
WITH munge AS (
SELECT *,
LEFT(callstarttime, 8) AS d,
STUFF(STUFF(SUBSTRING(callstarttime, 9, LEN(callstarttime)) , 3, 0, ':'), 6, 0, ':') AS t
FROM #t AS t
)
SELECT *,
TRY_CONVERT(DATETIME, d + ' ' + t),
DATEADD(SECOND, munge.callduration, TRY_CONVERT(DATETIME, d + ' ' + t))
FROM munge
add a comment |Â
up vote
3
down vote
Let me know how this works for you.
CREATE TABLE #t (callstarttime VARCHAR(255), callduration INT);
INSERT #t ( callstarttime, callduration )
SELECT *
FROM (
VALUES('20180101215910', 120),('20180101215710', 220)
) AS x (callstarttime, callduration);
WITH munge AS (
SELECT *,
LEFT(callstarttime, 8) AS d,
STUFF(STUFF(SUBSTRING(callstarttime, 9, LEN(callstarttime)) , 3, 0, ':'), 6, 0, ':') AS t
FROM #t AS t
)
SELECT *,
TRY_CONVERT(DATETIME, d + ' ' + t),
DATEADD(SECOND, munge.callduration, TRY_CONVERT(DATETIME, d + ' ' + t))
FROM munge
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Let me know how this works for you.
CREATE TABLE #t (callstarttime VARCHAR(255), callduration INT);
INSERT #t ( callstarttime, callduration )
SELECT *
FROM (
VALUES('20180101215910', 120),('20180101215710', 220)
) AS x (callstarttime, callduration);
WITH munge AS (
SELECT *,
LEFT(callstarttime, 8) AS d,
STUFF(STUFF(SUBSTRING(callstarttime, 9, LEN(callstarttime)) , 3, 0, ':'), 6, 0, ':') AS t
FROM #t AS t
)
SELECT *,
TRY_CONVERT(DATETIME, d + ' ' + t),
DATEADD(SECOND, munge.callduration, TRY_CONVERT(DATETIME, d + ' ' + t))
FROM munge
Let me know how this works for you.
CREATE TABLE #t (callstarttime VARCHAR(255), callduration INT);
INSERT #t ( callstarttime, callduration )
SELECT *
FROM (
VALUES('20180101215910', 120),('20180101215710', 220)
) AS x (callstarttime, callduration);
WITH munge AS (
SELECT *,
LEFT(callstarttime, 8) AS d,
STUFF(STUFF(SUBSTRING(callstarttime, 9, LEN(callstarttime)) , 3, 0, ':'), 6, 0, ':') AS t
FROM #t AS t
)
SELECT *,
TRY_CONVERT(DATETIME, d + ' ' + t),
DATEADD(SECOND, munge.callduration, TRY_CONVERT(DATETIME, d + ' ' + t))
FROM munge
answered yesterday


sp_BlitzErik
19.5k1161101
19.5k1161101
add a comment |Â
add a comment |Â
up vote
1
down vote
The problem, as I'm sure you're aware, is that SQL Server requires certain punctuation between the numerals before it will consider a character sequence to be a valid datetime. sp_BlitzErik has shown one way to inject those characters. Here's another using the FORMAT function introduced in SQL Server 2012:
declare @s varchar(255) = '20180101215910';
declare @i bigint = @s; -- implicit type conversion
select
Raw = @i,
WithSeparators = FORMAT(@i, '####-##-##T##:##:##'),
AsDateTime = CONVERT(datetime, FORMAT(@i, '####-##-##T##:##:##')),
Incremented = DATEADD(SECOND, 120, CONVERT(datetime, FORMAT(@i, '####-##-##T##:##:##')));
Raw WithSeparators AsDateTime Incremented
-------------- -------------------- ----------------------- -----------------------
20180101215910 2018-01-01T21:59:10 2018-01-01 21:59:10.000 2018-01-01 22:01:10.000
I have to convert to integer type because that is one that FORMAT will accept. It has to be BIGINT due to the number of digits.
Performance-wise I think you would find it very difficult to measure the difference between the two techniques.
This may have a smaller cognitive load, depending on how familiar you are with reading T-SQL.
add a comment |Â
up vote
1
down vote
The problem, as I'm sure you're aware, is that SQL Server requires certain punctuation between the numerals before it will consider a character sequence to be a valid datetime. sp_BlitzErik has shown one way to inject those characters. Here's another using the FORMAT function introduced in SQL Server 2012:
declare @s varchar(255) = '20180101215910';
declare @i bigint = @s; -- implicit type conversion
select
Raw = @i,
WithSeparators = FORMAT(@i, '####-##-##T##:##:##'),
AsDateTime = CONVERT(datetime, FORMAT(@i, '####-##-##T##:##:##')),
Incremented = DATEADD(SECOND, 120, CONVERT(datetime, FORMAT(@i, '####-##-##T##:##:##')));
Raw WithSeparators AsDateTime Incremented
-------------- -------------------- ----------------------- -----------------------
20180101215910 2018-01-01T21:59:10 2018-01-01 21:59:10.000 2018-01-01 22:01:10.000
I have to convert to integer type because that is one that FORMAT will accept. It has to be BIGINT due to the number of digits.
Performance-wise I think you would find it very difficult to measure the difference between the two techniques.
This may have a smaller cognitive load, depending on how familiar you are with reading T-SQL.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
The problem, as I'm sure you're aware, is that SQL Server requires certain punctuation between the numerals before it will consider a character sequence to be a valid datetime. sp_BlitzErik has shown one way to inject those characters. Here's another using the FORMAT function introduced in SQL Server 2012:
declare @s varchar(255) = '20180101215910';
declare @i bigint = @s; -- implicit type conversion
select
Raw = @i,
WithSeparators = FORMAT(@i, '####-##-##T##:##:##'),
AsDateTime = CONVERT(datetime, FORMAT(@i, '####-##-##T##:##:##')),
Incremented = DATEADD(SECOND, 120, CONVERT(datetime, FORMAT(@i, '####-##-##T##:##:##')));
Raw WithSeparators AsDateTime Incremented
-------------- -------------------- ----------------------- -----------------------
20180101215910 2018-01-01T21:59:10 2018-01-01 21:59:10.000 2018-01-01 22:01:10.000
I have to convert to integer type because that is one that FORMAT will accept. It has to be BIGINT due to the number of digits.
Performance-wise I think you would find it very difficult to measure the difference between the two techniques.
This may have a smaller cognitive load, depending on how familiar you are with reading T-SQL.
The problem, as I'm sure you're aware, is that SQL Server requires certain punctuation between the numerals before it will consider a character sequence to be a valid datetime. sp_BlitzErik has shown one way to inject those characters. Here's another using the FORMAT function introduced in SQL Server 2012:
declare @s varchar(255) = '20180101215910';
declare @i bigint = @s; -- implicit type conversion
select
Raw = @i,
WithSeparators = FORMAT(@i, '####-##-##T##:##:##'),
AsDateTime = CONVERT(datetime, FORMAT(@i, '####-##-##T##:##:##')),
Incremented = DATEADD(SECOND, 120, CONVERT(datetime, FORMAT(@i, '####-##-##T##:##:##')));
Raw WithSeparators AsDateTime Incremented
-------------- -------------------- ----------------------- -----------------------
20180101215910 2018-01-01T21:59:10 2018-01-01 21:59:10.000 2018-01-01 22:01:10.000
I have to convert to integer type because that is one that FORMAT will accept. It has to be BIGINT due to the number of digits.
Performance-wise I think you would find it very difficult to measure the difference between the two techniques.
This may have a smaller cognitive load, depending on how familiar you are with reading T-SQL.
answered 21 hours ago


Michael Green
13.2k82857
13.2k82857
add a comment |Â
add a comment |Â
db100 is a new contributor. Be nice, and check out our Code of Conduct.
db100 is a new contributor. Be nice, and check out our Code of Conduct.
db100 is a new contributor. Be nice, and check out our Code of Conduct.
db100 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f217316%2fconvert-varchar-to-datetime-and-add-seconds%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password