I have a time column showing up as an int. 800 would be 08:00am and 1300 would be 1:00pm.
How can I format the int so that it appears as 12h time, e.g. 1:13 pm?
I'm using SQL Server.
Thanks
You can do something like below to achieve the required result. Idea taken from How to convert an integer (time) to HH:MM:SS::00 in SQL Server 2008?
set #time = 1300
select (#time / 1000000) % 100 + ':' +
(#time / 10000) % 100 + ':' +
(#time / 100) % 100 + ':' +
(#time % 100) * 10
Convert the integer to hh:mm:ss representation of datetime and handle formatting (i.e. AM/PM) in the presentation layer:
DECLARE #Time INTEGER = 800;
SELECT
-- convert datetime to hh:mm:ss
CONVERT(VARCHAR,
-- cast hh:mm to datetime
CAST(-- insert semicolon to get hh:mm format
STUFF(-- convert integer to string and pad with zeroes
RIGHT(REPLICATE('0', 4) + CAST(#Time AS VARCHAR), 4),
3, 0, ':')
AS DATETIME),
108);
SELECT #Time = 1300;
SELECT CONVERT(VARCHAR, CAST(STUFF(RIGHT(REPLICATE('0', 4) + CAST(#Time AS VARCHAR), 4), 3, 0, ':') AS DATETIME), 108);
Example selecting from temporary table:
CREATE TABLE #Time ( TimeValue INTEGER );
INSERT INTO #Time VALUES (800);
INSERT INTO #Time VALUES (1300);
SELECT CONVERT(VARCHAR,
CAST(STUFF(RIGHT(REPLICATE('0', 4) + CAST(TimeValue AS VARCHAR), 4),
3, 0, ':')
AS DATETIME), 108) AS Time
FROM #Time;
DROP TABLE #Time;
Related
I have a datetime column ArrivalDateTime which is stored as a varchar value.
Let's say if the value is 20161212093256, I want the output to be 2016-12-12 09:32:56.
I could get the date part in datetime format as below.
SELECT
CONVERT(DATETIME2(0), LEFT('20161212093256', 8))
This returns the output as 2016-12-15 00:00:00.
I tried the following query to get the time part as well.
SELECT
CONVERT(DATE, LEFT('20161212093256', 8)) + ' ' +
CONVERT(TIME, RIGHT('20161212093256', 6))
But this throws an error:
The data types date and varchar are incompatible in the add operator
How can I get both date and time part in datetime format?
Get the date component first and convert it to DATETIME and then get the time component and convert it to DATETIME also. Finally, add the two results:
SELECT
CONVERT(DATETIME,LEFT('20161212093256', 8)) +
CONVERT(DATETIME,
LEFT(RIGHT('20161212093256', 6), 2) + ':' +
SUBSTRING(RIGHT('20161212093256', 6), 3, 2) + ':' +
RIGHT(RIGHT('20161212093256', 6), 2)
)
To further explain, the result first conversion is the date component:
2016-12-12 00:00:00.000
The second conversion is the time component, but when you convert it to DATETIME it adds it to the 0 date or '1900-01-01', so the result is:
1900-01-01 09:32:56.000
Then, you add both DATETIMEs to get:
2016-12-12 09:32:56.000
To get rid of the ms component:
SELECT
CONVERT(DATETIME,LEFT('20161212093256', 8)) +
CONVERT(DATETIME,
LEFT(RIGHT('20161212093256', 6), 2) + ':' +
SUBSTRING(RIGHT('20161212093256', 6), 3, 2) + ':00'
)
Try this,
DECLARE #V_STR VARCHAR(20) = '20161212093256'
SELECT CONVERT(SMALLDATETIME,LEFT(#V_STR,8) +' '+ --date
SUBSTRING(#V_STR,9,2)+':'+ --hour
SUBSTRING(#V_STR,11,2)+':'+ --minute
SUBSTRING(#V_STR,13,2)) AS DATE_TIME --second
Try this
select concat(CONVERT(DATE, LEFT('20161212093256', 8)) , ' ' , CONVERT(TIME, substring(RIGHT('20161212093256', 6),1,2)+ ':' + substring(RIGHT('20161212093256', 4),1,2) + ':' +RIGHT('20161212093256', 2)))
above will display time with miliseconds, below without miliseconds
select concat(CONVERT(DATE, LEFT('20161212093256', 8)) , ' ' , substring(RIGHT('20161212093256', 6),1,2)+ ':' + substring(RIGHT('20161212093256', 4),1,2) + ':' +RIGHT('20161212093256', 2))
SELECT STUFF(STUFF(STUFF(STUFF(STUFF('20161212093256', 5, 0, '-'), 8, 0, '-'), 11, 0, ' '), 14, 0, ':'), 17, 0, ':')
I want to combine two separate columns into date time and subtract 20 hours in SQL Server.
This works in Oracle:
create table t ( x varchar2(8),y varchar2(6));
insert into t values ('20151106','090000');
select to_char(to_date(x||' '||y, 'yyyymmdd hh24miss')
- (20/24),'dd-mm-yyyy hh24:mi:ss') as "date minus 20 hrs" from t;
date minus 20 hrs
- -----------------
05-11-2015 13:00:00
I can't crack it in SQL Server, I can get parts to work but can't combine the two dates (too many functions).
This works:
CREATE TABLE t (x VARCHAR(8), y VARCHAR(6))
INSERT INTO t
VALUES('20151105', '150800')
SELECT
DATEADD(HOUR, -20, CAST(CONCAT(SUBSTRING(y, 1, 2), ':', SUBSTRING(y, 3, 2), ':', SUBSTRING(y, 5, 2)) AS TIME))
FROM t
This works:
SELECT DATEADD(HOUR, -20, x)
FROM t
But I can't combine them:
SELECT
DATEADD(HOUR, -20,
CONCAT(SUBSTRING(x, 1, 4), '-', SUBSTRING(x, 5, 2), '-',
SUBSTRING(x, 7, 2), ' ',
CAST(CONCAT(SUBSTRING(y, 1, 2), ':', SUBSTRING(y, 3, 2),
':', SUBSTRING(y, 5, 2)) AS TIME)))
FROM t
If you CONVERT the values properly to a datetime, there should be no issue with what you're trying to do. You want to give SQL Server a date in the following format, or one of the other acceptable formats:
YYYYMMDD HH:MM:SS
So applying that to your sample, the date portion is fine, but you need to get the time format correct before you can convert it to a valid date:
CREATE TABLE #t ( x VARCHAR(8), y VARCHAR(6) )
INSERT INTO #t
VALUES ( '20151105', '150800' )
-- we want 20151105 15:08:00
SELECT DATEADD(HOUR, -20,
( CONVERT(DATETIME, x + ' ' +
LEFT(y, 2) + ':' +
SUBSTRING(y, 3, 2) + ':' +
RIGHT(y, 2)) )) AS Result
FROM #t
DROP TABLE #t
Produces
2015-11-04 19:08:00.000
The string you're passing to datepart evaluates to:
select dateadd(hour, -20, '2015-11-05T15:08:00.0000000')
But you'll get a Conversion failed error for more than 3 zeroes. An easy way to fix that is to remove the second column's conversion to time. While you're at it, you could use + instead of concat, which is easier to read:
select dateadd(hour, -20,
substring(x,1,4) + '-' +
substring(x,5,2) + '-' +
substring(x,7,2) + 'T' +
substring(y,1,2) + ':' +
substring(y,3,2) + ':' +
substring(y,5,2))
from t
See it working at SE Data.
I have time as 04:02:00, I want it only as 4:02 using SQL Server
I have tried this code:
#time time
set #time ='04:02:00'
SELECT RIGHT(convert(varchar, #time, 100), 8)
It produces output:
4:02AM
please help ..
Thanks
You can use:
DECLARE #dt DATETIME
SET #dt = GETDATE()
SELECT CONVERT(varchar(5),#dt,108)
This query gives result in the format 'HH:mm'.
Hope it helps.
DECLARE #time datetime
set #time ='04:02:00'
SELECT LEFT(RIGHT(convert(varchar, #time, 100), 8), 6)
You can use DATEPART and concatenate from there:
SELECT
RIGHT('00' + CONVERT(VARCHAR(2), DATEPART(HOUR, #time)), 2) + ':'
+ RIGHT('00' + CONVERT(VARCHAR(2), DATEPART(MINUTE, #time)), 2)
Notice the RIGHT('00' + ..., 2), this is to pad the hour or minute part with leading zeroes so that it'll be two characters long, e.g 4 becomes 04.
Been struggling with this and can't seem to find the right answer, although there are plenty of mentions for converting, but nothing specific is working.
I need to convert a time with data type of float into hours and minutes. So 13.50 as 13.30. The data type as fixed as float in DB so cannot change. DB is SQL Server 2008R2
Have tried:
cast(cast(floor(fdsViewTimesheet.perStandardHours) as
float(2))+':'+cast(floor(100*(
fdsViewTimesheet.perStandardHours - floor(fdsViewTimesheet.perStandardHours)))as
float(2)) as time) AS STANDARD_HOURS
But I get error message "Explicit conversion from data type real to time is not allowed" Have tried as char instead of as float but query hangs.
What am I doing wrong? I just want to convert a float value into hours and minutes.
Would be grateful if someone could point me in the right direction.
You can try:
DECLARE #HOURS decimal(7,4) = 20.5599
SELECT CAST(CONVERT(VARCHAR,DATEADD(SECOND, #HOURS * 3600, 0),108) AS TIME)
output : 20:33:35
But remember : Type Time in MSSQL only under 24hrs
If you want greater than 24hrs, try:
DECLARE #HOURS decimal(7,4) = 25.5599
SELECT
RIGHT('0' + CAST (FLOOR(#HOURS) AS VARCHAR), 2) + ':' +
RIGHT('0' + CAST(FLOOR((((#HOURS * 3600) % 3600) / 60)) AS VARCHAR), 2) + ':' +
RIGHT('0' + CAST (FLOOR((#HOURS * 3600) % 60) AS VARCHAR), 2)
output : 25:33:35
-- Update
Decimal minutes to more than 24hrs
DECLARE #MINUTES decimal(7,4) = 77.9
SELECT
RIGHT('0' + CAST (FLOOR(COALESCE (#MINUTES, 0) / 60) AS VARCHAR (8)), 2) + ':' +
RIGHT('0' + CAST (FLOOR(COALESCE (#MINUTES, 0) % 60) AS VARCHAR (2)), 2) + ':' +
RIGHT('0' + CAST (FLOOR((#MINUTES* 60) % 60) AS VARCHAR (2)), 2);
output: 01:17:54
This should work for you
DECLARE #f [real]
SET #f = 13.50
SELECT DATEADD(mi, (#f - FLOOR(#f)) * 60, DATEADD(hh, FLOOR(#f), CAST ('00:00:00' AS TIME)))
DECLARE #f FLOAT = 13.5;
SELECT CONVERT(TIME(0), DATEADD(MINUTE, 60*#f, 0));
Or if you just want hh:mm as a string:
SELECT CONVERT(CHAR(5), DATEADD(MINUTE, 60*#f, 0), 108);
Just be careful if you have values >= 24.
How about you convert to minutes and add to the 00:00 time like so:
DECLARE #c datetime
select #c = dateadd(mi,fdsViewTimesheet.perStandardHours*60,'00:00')
If you wanted to do it in the statement with Time only:
select CONVERT(TIME,dateadd(mi,fdsViewTimesheet.perStandardHours*60,'00:00') )
If you have values that are larger than 24 hours, then the standard datetime and time types in sql cannot hold these. They are limited to holding 24 hour ranges.
What you would need to do is store the time representation in a string for example like so:
select cast(floor(fdsViewTimesheet.perStandardHours) as varchar(10)) + ':' + cast(FLOOR( (fdsViewTimesheet.perStandardHours - floor(fdsViewTimesheet.perStandardHours))*60)as varchar(2))
I am getting a whole number for the duration of product. For example : 62, 103, 184, 4543. I want to convert it in time format. Let say , in case of 62, it would format like '00:01:02'. I am looking for a function which can resolve my problem. Is there any function in SQL Server which convert a number into time format?
declare #d datetime
Set #d = Convert(datetime, '20010101',112 )
Set #d = dateadd(s, 62, #d) --add 62 seconds to base date
Select convert(varchar,#D,108)
Well, you have two choices. You can do the division by 60 yourself like so:
SELECT RIGHT('0' + CAST(#seconds / 3600 AS varchar), 2) + ':' +
RIGHT('0' + CAST((#seconds / 60) % 60 AS varchar), 2) + ':' +
RIGHT('0' + CAST(#seconds % 60 AS varchar), 2)
Or you can do some sort of datetime-related kludge:
SELECT CONVERT(varchar,
DATEADD(second, #seconds, CAST('1-Jan-2000' AS datetime)),
8)
Format 8 is hh:mi:ss from here.