I am working with stored procedure while coding I feel some difficulties to add an hour to a time.I mean I have already a predefined time like 08:00 in my database and now I want to add 4 to this time and I want to get result as 12:00. How can I achieve it?? The way which I tried is below,
ALTER PROCEDURE [dbo].[AttandenceEdit1](
#machid numeric(18,0),
)
AS
declare #time as time(0),#castedtime as time(0),
set #timein1=(select convert(time(0), from ShiftType where machId=#machid )// Value=08:00
print #time
set #addvalue =(DATEADD(HH,#timein1,4))
print #addvalue
I want the result 08:00+4=12:00
now it show error like Argument data type time is invalid for argument 2 of dateadd function
You have the arguments to dateadd in the wrong order. This:
declare #time as time(0)
set #time='08:00'
print #time
declare #addvalue time(0)
set #addvalue =(DATEADD(HOUR, 4, #time))
print #addvalue
Will result in:
08:00:00
12:00:00
There are other issues with your stored proc code though that you need to fix (like undeclared variables, surplus comma, logic).
There seems to be a bunch of issues in the code you've supplied, for example #timein1 and #addvalue are not declared.
So here's a simple example:
DECLARE #time AS TIME = '08:00:00'
SELECT #time AS OriginalTime,
DATEADD(HOUR, 4, #time) TimePlus4Hours
Produces:
OriginalTime | TimePlus4Hours
====================================
08:00:00.0000000 | 12:00:00.0000000
Reference:
DATEADD (Transact-SQL)
Returns a specified date with the specified number interval (signed integer) added to a specified datepart of that date.
DATEADD (datepart , number , date )
Try by Changing the DATEADD() function syntax like this:
set #addvalue =(DATEADD(HH,4,#timein1))
Related
I tried to convert a varchar variable stored in my database as HH:MM:SS to an actual datetime format HH:MM:SS. I did get the value for HH:MM:SS but the attempt also prefixed the YYYY:MM:DD along with the expected result.
Following is the code that I used to convert this varchar value to HH:MM:SS and the result I got.
Code I tried :
DECLARE #Duration Varchar(10)
SET #Duration = '00:01:23'
SELECT CONVERT(datetime, Duration, 8) AS duration
The output I got :
1900-01-01 00:01:23.000
The expected output:
00:01:23
Please let me know what needs to be changed in this. Thank you!
If you want time, why are you converting to datetime? Given the name it shouldn't be surprising you get both date and time. Try:
DECLARE #Duration char(8) = '00:01:23';
SELECT duration = CONVERT(time(0), #Duration);
Results:
duration
00:01:23
Example db<>fiddle
Just keep in mind that time (nor any date/time type) is not meant to represent a duration or interval. Because what happens when your duration or interval exceeds 24 hours?
What you posted is a time, not a date or datetime. A duration isn't a date. The date types are binary, they don't have prefixes.
You can define a time directly with :
Declare #Duration time ='00:01:23'
Or you can cast a string to a time:
Declare #Duration varchar(10)
Set #Duration = '00:01:23'
Select cast(#Duration as time) as duration
or
Declare #Duration varchar(10)
Set #Duration = '00:01:23'
Select convert(time, #Duration,8) as duration
Unfortunately that's not a duration, it's a time of day. It can only store values between 00:00 and 23:59:59.9999999.
SQL Server has no interval/duration type.
Declare #Duration Varchar(10)
Set #Duration = '00:01:23'
select convert(time,#duration,8);
I have two tables I am trying to get a difference from in SQL. Table A has a time in the following format: 07:40:06,
08:33:34,
13:42:09,
Table B is in the same format. I want to return in an actual time for example 7:40:00 in A and 7:50:00 in B result in new column 00:10:00.
Thanks,
I admit that this is sort of cheesy, but appears to work. You could use a constant date, and add the times to that as strings. Then subtract and format however you like. For example: HH:mm:ss. This will give the result of 00:10:00
DECLARE #contstantDate DATETIME = DATEFROMPARTS(1900,1,1)
DECLARE #date1 DATETIME = #contstantDate + '7:40:00'
DECLARE #date2 DATETIME = #contstantDate + '7:50:00'
SELECT FORMAT(#date2-#date1, 'HH:mm:ss')
I have two columns within my table they are set as nvarchar fields but contain time values.
one column is a time field one is the duration field
eg.
Time 1 = 15:05:22 (time field)
Time 2 = 00:02:00 (duration field)
I want to output Time 1 + Time 2 = 15:07:22
I have tried CAST(time1 as datetime)+CAST(time2 as datetime)
but I get 1900-01-01 15:07:22.000, and I don't want the date part. I can't use cast as time as I get an error I presume this is because the fields are set as nvarchar and not date/time?
Just cast the result to time to get rid of the date portion:
DECLARE #time_txt varchar(8);
DECLARE #duration_txt varchar(8);
SET #time_txt = '15:05:22';
SET #duration_txt = '00:02:00';
SELECT CAST(CAST(#time_txt as datetime) + CAST(#duration_txt as datetime) as time);
-- yields the time value 15:07:22.0000000
If you need this as a string (for example, in hh:mm:ss format), you can use CONVERT with the appropriate format option:
...
SELECT CONVERT(varchar(8), CAST(#time_txt as datetime) + CAST(#duration_txt as datetime), 108);
-- yields the string 15:07:22
PS: In general, you should use time columns for time values instead of varchar columns. Unfortunately, SQL Server does not have a really good data type for durations (time spans).
select dateadd(second,datediff(second,0,time1),time2) as Time3
from your_table
I am trying to write a query for round up a time functionality as same as MRound
(Ex: =MRound(2015-1-1 10:10:000,"1:15")) function in Excel.
I would like to roundup the given time to 75 minutes [1 Hr and 15 Min].
i am trying to write a query for the same but my excel out put [for given input] is not matching with sql query output.[expecting output same as excel]
Currently i am using below code to roundup but output is not matching with Excel Mround function output.
DECLARE #RoundedTime smalldatetime
DECLARE #Multiplier float
DECLARE #Time datetime
DECLARE #RoundTo float
set #Time='1900-1-1 00:00:00'
set #RoundTo=1.25
SET #Multiplier= 24.00/#RoundTo
SET #RoundedTime= ROUND(CAST(CAST(CONVERT(varchar,#Time,121) AS datetime) AS float) * #Multiplier,0)/#Multiplier
select #RoundedTime
Ex -In Excel if i want to round up "01-01-1900 12:00:00 AM" date then it will give "00-01-1900 23:45" as output but in sql it will give "1900-01-01 00:00:00"
could you please help me to resolve this issue.
DECLARE #Time datetime
DECLARE #RoundTo float
set #Time='1900-1-1 00:00:00'
set #RoundTo=1.25
SELECT CAST(#Time AS DATETIME) +
CONVERT(DATETIME, DATEADD(MINUTE, 60 * #RoundTo, 0));
RESULT: 1900-01-01 01:15:00.000
I am storing all my dates in SQL Server Datetime fields in UTC Date.
There is a requirement where by I have to calculate local Datetime in a procedure from the UTC Date field, and for that i have the Time zone offset of the local datetime.
For ex. my Timezone offset is:'05:30:00'
and UTC Date is: 2013-02-09 08:34:12.037
Desired output: 2013-02-09 14:04:12.037
Now is there a simple way where of doing this without DateAdd and splitting the offset in hours and minutes.
You should be able to use the SWITCHOFFSET function. Here is an example:
declare #dt datetime;
set #dt = '2013-02-09 08:34:12.037';
select SWITCHOFFSET(CONVERT(datetimeoffset, #dt), '+05:30') as 'DATETIMEOFFSET',
CAST(SWITCHOFFSET(CONVERT(datetimeoffset, #dt), '+05:30') as datetime) as 'DATETIME'
-- Outputs:
--
-- 2013-02-09 14:04:12.0370000 +05:30 2013-02-09 14:04:12.037
Use the convert with 112:
declare #d datetime
set #d = getdate()
declare #s nvarchar(20)
set #s = convert(varchar, #d, 112)
print #s
That string will have the year, month, seconds etc.. always on the same position
Extract the desired part with substring:
print substring(#s, 1, 4) -- the year
Now recalculate the entire thing to minutes, by multiplying the hours by 60 and adding the minutes. Now substract your minutes delta from that number. Build a new string with the adjusted date-time, and convert that back to datetime. But... if you need to know the date as well, and you want to do it correct.... there is some coding left.
My advice: do use dateadd it's simple and correct (substract minutes is my advice).