2

Example:

seconds = 272920

My Expected output = 3Days hours:minutes:seconds

I am trying to convert the seconds to day time.

Right now I am currently converted it to time using these query:

SELECT CONVERT(SEC_TO_TIME(Seconds), CHAR) FROM table;

and my output is 75:48:40 -------> Format(hh:mm:ss)

But my expected value is days, hours, mins, secs

.

Any suggestion is a big help, thanks in advance..

Syntax Rommel
  • 932
  • 2
  • 16
  • 40

3 Answers3

2

Try;

SELECT 
CONCAT(
    FLOOR(HOUR(SEC_TO_TIME(272920)) / 24), ' days ',
    MOD(HOUR(SEC_TO_TIME(272920)), 24), 'H:',
    MINUTE(SEC_TO_TIME(272920)), 'Min:',
    second(SEC_TO_TIME(272920)), 'Sec'
)
Praveen
  • 8,945
  • 4
  • 31
  • 49
1
DECLARE @s INT = 139905;

SELECT 
    CONVERT(VARCHAR(12), @s /60/60/24)   + ' Day(s), ' 
  + CONVERT(VARCHAR(12), @s /60/60 % 24) + ' Hour(s), '
  + CONVERT(VARCHAR(2),  @s /60 % 60)    + ' Minute(s), ' 
  + CONVERT(VARCHAR(2),  @s % 60)        + ' Second(s).';

For more details look at this below link SQL Server Convert seconds to Days & hours

Community
  • 1
  • 1
Dilmah
  • 1,137
  • 11
  • 14
1

With some basic arithmetic, using a combination of modulo and integer division operators:

SELECT CONCAT(
    seconds DIV (60 * 60 * 24),
    ' Days, ',
    seconds DIV (60 * 60) % 24,
    ':',
    seconds DIV 60 % 60,
    ':',
    seconds % 60) FROM table;

With your example input value of 272920:

SELECT CONCAT(
    272920 DIV (60 * 60 * 24),
    ' Days, ',
    272920 DIV (60 * 60) % 24,
    ':',
    272920 DIV 60 % 60,
    ':',
    272920 % 60);

This will yield the following output:

3 Days, 3:48:40
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156