Showing posts with label datetime. Show all posts
Showing posts with label datetime. Show all posts

Friday, March 30, 2012

How can I convert datetime to number of minutes

I have a column in a table that stores the number of hours a task took to do. The column TaskDuration is a datetime datatype. I need to convert the hours to something that can be summed. Does anyone how this can be done? I tried CONVERT(nvarchar(5), tblTasks.TaskDuration, 108) but of course the nvarchar(5) cannot be summed. Maybe there is a way to convert the time portion to minutes and divide it by 60, anyway if someone can offer some help I appreciate it.

Try something like this

(datepart(hh, tblTasks.TaskDuration) * 60) + datepart(mm, tblTasks.TaskDuration)

|||

I tried this and it will return the number of minutes for the hours; however, the Parenthesis will not stay around the (datepart(hh, tblTasks.TaskDuration) * 60) in the view. So the (mm) are not being added.

Well it is adding time for the minutes but 30 is calculating to 10, so 03:30 is returning 190 minutes and it should be 210.

Any ideas?

|||I gave you the wrong datepart signifier, try datepart(n, tblTasks.TaskDuration)|||

Ok, so now that I have the number of minutes, can I convert this to hours and minutes. What I mean is the reporting tool needs a numeric column to sum on, so 03:15 needs to be 3.25.

Is this possible?

|||

I tried and it looks to be returning the correct format. If you have any comments, I appreciate them.

CONVERT (FLOAT, DATEPART(hh, dbo.tblVolunteerTasks.VTaskDuration) * 60 + DATEPART(n, dbo.tblVolunteerTasks.VTaskDuration)) / 60

How can I convert DateTime to Date as Parameter?

Hi Guys! Need Help on this!! I am using a Datetime data type as my Parameter on my stored procedure in SQL Server 2005. I am also using Crystal Reports XI for my reporting using the stored procedure in SQL but my problem is that I want to use ONLY the DATE data type as my Parameter instead of using the datetime parameter in Crystal Reports! Since the SQL server does not have a Date data type, how can I convert this from DateTime to Only Date data type as my parameter?....Thanks!!

Use datatime data type and pass just date part from CR or strip off the time part wherever you are planning to use it.

declare @.d datetime

set @.d = getdate()

select dateadd(day, datediff(day, 0, @.d), 0)

go

AMB

|||

Thanks! but how do you pass just the date part from CR? Any idea would be greatly appreciated!! I can strip off the time part inside the stored procedure in SQL 2005 but CR is using the parameter which is datetime....

|||

Sorry about that, but I think that question could be answered better in a CR newsgroup. Try:

datetime(datepart("yyyy", {@.d}), datepart("m", {@.d}), datepart("d", {@.d}), 00, 00, 00)

AMB

|||Thanks AMB....that will work but that code is for the inside on the report...my problem lies in the parameter prompt window..how can I let the user only select the date without seeing the the time on the parameter prompt window?....|||

Sorry I have no idea. As I mentioned in my previos post, these questions would be better asked in a CR newsgroup.

AMB

how can i convert binary(8) to datetime?

HI! :shocked:
I tried to convert 0x01C3F0F5012D36E0, binary(8) to datetime
But
How can I do that?
thanks for allwhat does 0x01C3F0F5012D36E0 repesent in datetime as ?|||You can't convert it, it is bigger than the largest possible datetime:DECLARE @.dMax DATETIME

SET @.dMax = '9999-12-31 23:59:59.997'

SELECT Cast(@.dMax AS VARBINARY(8)), 0x01C3F0F5012D36E0-PatP

Wednesday, March 28, 2012

How can I compare datetime in a query?

I have a table with the following columns

company_Id employee_id logon_time_id logoff_time start_valid_period end_valid_period

Employee's working time should only be counted if it is between start_valid_period and end_valid_period

So, if I have for employee1 from company1

logon_time_id = 04/07/2006 11:00
loggoff_time = 04/07/2006 12:20
start_valid_period = 04/07/2006 12:10
end_valid_period = 04/07/2006 12:30

I should consider 04/07/2006 12:10 as the initial datetime, 04/07/2006 12:20 as the final datetime, and count only 10min of work to him. In code:

if(logon_time_id < start_valid_period) initialDatetime = start_valid_period else initialDatetime = logon_time_id

if(logoff_time < end_valid_period) finalDatetime = logoff_time else finalDatetime = end_valid_period

Is there anyway I can do this in a query, without using a stored procedure with "ifs" and everything else?

Thank you!

Hi!

Forget one thing! I'm wondering if there is a way of doing it without using the case statement as well!

Thank you!

|||

use datediff

http://msdn2.microsoft.com/en-us/library/ms189794.aspx

the case keyword in sql is very powerful you can use it in the query to replace value

this is a single select statement to solve your problem

select

datediff( mi,

case
when logon_time_id< start_valid_period then start_valid_period
else logon_time_id
end

,-- comma
case
when loggoff_time > end_valid_period then end_valid_period
else loggoff_time
end

)as minutesworked

from mytime

joeydj

|||

use datediff

http://msdn2.microsoft.com/en-us/library/ms189794.aspx

the case keyword in sql is very powerful you can use it in the query to replace value

this is a single select statement to solve your problem

select

datediff( mi,

case
when logon_time_id< start_valid_period then start_valid_period
else logon_time_id
end

,-- comma
case
when loggoff_time > end_valid_period then end_valid_period
else loggoff_time
end

)as minutesworked

from mytime

joeydj

|||

Case would be easiest and clearest, but you could do:

select (select max(dateval)
from (select login_time_id as dateval
union all
select start_valid_period)) as initialDatetime,

(select min(dateval)
from (select logoff_time
union all
select end_valid_period)) as initialDatetime

from ...

|||

The first thing to note when comparing date overlaps is that there are 6 cases -- 4 with overlap and 2 without. Presumably, if the user was logged in only during a period that was not valid, the elapsed time should be zero. Using the logic as listed (ifs and CASEs) you could end up with a negative time accounting.

Now for a trick from the old days, before CASE was available (we always walked uphill barefoot a lot in those days, too ;) ). There is a function called SIGN() that returns -1, 0, or 1 depending on the sign of a number. By coercing it into a binary 1 or 0 and multiplying by it, you can get the same effect as a conditional.

In this case, the problem can be recast as increase the start_valid by the difference with the logon time as long as long as the difference is greater than zero. Similarly, the end_valid should be decreased by the difference of the logoff time as long as that difference is negative. Once these adjustments have been done, now apply the DateDiff().

Here is some code that should do the trick. As noted above, the case of no overlap is handled separately. The CASE statements are only to explain which case is being handled.

Drop Table #timekeeper
go

Create Table #timekeeper(
username varchar(30),
logon_time_id datetime,
loggoff_time datetime,
start_valid_period datetime,
end_valid_period datetime
)

Insert #timekeeper values( 'Fred', '04/07/2006 11:00', '04/07/2006 12:20', '04/07/2006 12:10', '04/07/2006 12:30' )
Insert #timekeeper values( 'Wilma', '04/07/2006 12:20', '04/07/2006 12:40', '04/07/2006 12:10', '04/07/2006 12:30' )
Insert #timekeeper values( 'Barney', '04/07/2006 11:20', '04/07/2006 12:40', '04/07/2006 12:10', '04/07/2006 12:30' )
Insert #timekeeper values( 'Betty', '04/07/2006 12:20', '04/07/2006 12:25', '04/07/2006 12:10', '04/07/2006 12:30' )
Insert #timekeeper values( 'Pebbles', '04/07/2006 11:00', '04/07/2006 11:25', '04/07/2006 12:10', '04/07/2006 12:30' )
Insert #timekeeper values( 'Bambam', '04/07/2006 12:35', '04/07/2006 12:50', '04/07/2006 12:10', '04/07/2006 12:30' )

Select username,
Datediff( mi,
DateAdd( ms, (( 1 + Sign( DateDiff( ms, start_valid_period, logon_time_id ) ) ) / 2) * DateDiff( ms, start_valid_period, logon_time_id ), start_valid_period ),
DateAdd( ms, (( 1 + Sign( DateDiff( ms, loggoff_time, end_valid_period ) ) ) / 2) * DateDiff( ms, end_valid_period, loggoff_time ), end_valid_period )
) as usage_minutes,
'Overlap',
Case when logon_time_id < start_valid_period then 'Logged on early' else 'Logged on during valid' end,
Case when loggoff_time < end_valid_period then 'Logged out early' else 'Logged off after valid' end
From #timekeeper
Where start_valid_period <= loggoff_time
And logon_time_id <= end_valid_period
Union All
Select username,
0,
'No overlap',
Case when logon_time_id > end_valid_period then 'Logon after valid' else '' end,
Case when loggoff_time < start_valid_period then 'Logged off before valid' else '' end
From #timekeeper
Where Not(
start_valid_period <= loggoff_time
And logon_time_id <= end_valid_period
)


Fred 10 Overlap Logged on early Logged out early
Wilma 10 Overlap Logged on during valid Logged off after valid
Barney 20 Overlap Logged on early Logged off after valid
Betty 5 Overlap Logged on during valid Logged out early
Pebbles 0 No overlap Logged off before valid
Bambam 0 No overlap Logon after valid

|||Thank you!!!

How can I change the format of a date returned from asp:calendar

Hello!

I have a table in an SQL database, in which I have a field in datetime format.

In my aspx page I would like to get the date the user chooses from an asp: calendar I have and submit it to the DB.

I already have all the code ready, the datasource, the gridview, all other fields to submit, and I just added a template field with the asp:calendar so that the user could choose a date.

I′m getting this error when I run the page: "Conversion from type 'Date' to type 'Boolean' is not valid."

It seems to be a problem about the date that is given by the Calendar object (?) and the one I should submit to my DB.

Here′s the part of the code where I have my standard Calendar binded to the correspondant field:

<asp:Calendar ID="Calendar1" runat="server" SelectedDate='<%# Bind("data")%>' Visible='<%# Eval("data")%>'>
</asp:Calendar>

I′m gessing I should probably change the format of the date somehow before submit it to the DB, but how?

Thank you all,

RR

Format(dateVariable,"MM/dd/yyyy")

|||

sorry the noobness, but where can I do that?

in a script section in the beginnig of the page?

|||

You have bound the "data" column to both the SelectedDate and the Visible property. SelectedDate is of type Date, and Visible is of type Boolean. What datatype is the "data" column?

|||

You′re asking about the datatype in the db, right?

It′s datetime. (don′t know if it′s the best datatype, any advise here?) I only need a data like DD-MM-YYYY but when building my table in SQL, I have no format like this...

An update to this issue, I erased the visible property and the page at least runs, but no connection between the calendar and my field... maybe it′s better to explain my objective:

What I would need is a gridview where I can see my records. (done)
In the default view I would see all the fields in normal textboxes, (ok!, done)
When clicking insert new or edit, I would like to let the user choose a date from the calendar!
Can anyone help me to buid a thing like this?

THKS

Monday, March 26, 2012

How can I change a datetime column format?

How can I change a datetime column format?
and How can I make a subtotal'
--
LUIS ESTEBAN VALENCIA
MICROSOFT DCE 2.
MIEMBRO ACTIVO DE ALIANZADEV> How can I change a datetime column format?
Use .NET formatcodes - e.g. on the Format property of a textbox. Details on
MSDN:
Standard datetime format strings:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconstandarddatetimeformatstrings.asp
Custom datetime format strings:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcustomdatetimeformatstrings.asp
> and How can I make a subtotal'
Please read this section in RS BOL:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/rscreate/htm/rcr_creating_structure_objects_v1_7vi0.asp;
in particular the section of how to add subtotals in report designer.
--
This posting is provided "AS IS" with no warranties, and confers no rights.
"Luis Esteban Valencia" <luisvalen@.haceb.com> wrote in message
news:O%23IAS9e3EHA.2696@.TK2MSFTNGP14.phx.gbl...
> How can I change a datetime column format?
>
> and How can I make a subtotal'
>
> --
> LUIS ESTEBAN VALENCIA
> MICROSOFT DCE 2.
> MIEMBRO ACTIVO DE ALIANZADEV
>sql

Wednesday, March 21, 2012

How can I Add 02.45 hour to my Date

StartDate is actually 02:15 as datetime so why I cannot add 02:15hour to
now.
Set @.StartDate = '1899-12-30 02:15:00.000'
Select GetDate() now, GetDate() + @.StartDate as added
-- Result is 2005-02-08 14:11:07.860 -- 2005-02-06 16:26:07.860
Thanks
Murat BUDAKYou should specify to what you want to add it to, SQL's date is broken down
into segments, year, month day etc...
Have a look at the dateadd function
Untested :
select dateadd(hour,2.15,getdate())
You would say, dateadd(hour,@.targetdate,2.15) I don't know if the decimal
might bugger up your solution.
Personally I would convert the amount of time in hours to seconds and then
use dateadd seconds to add the seconds to my current date
"Murat BUDAK" wrote:

> StartDate is actually 02:15 as datetime so why I cannot add 02:15hour to
> now.
> Set @.StartDate = '1899-12-30 02:15:00.000'
> Select GetDate() now, GetDate() + @.StartDate as added
> -- Result is 2005-02-08 14:11:07.860 -- 2005-02-06 16:26:07.860
> Thanks
> Murat BUDAK
>
>|||On Tue, 8 Feb 2005 14:15:10 +0200, Murat BUDAK wrote:

>StartDate is actually 02:15 as datetime so why I cannot add 02:15hour to
>now.
>Set @.StartDate = '1899-12-30 02:15:00.000'
>Select GetDate() now, GetDate() + @.StartDate as added
>-- Result is 2005-02-08 14:11:07.860 -- 2005-02-06 16:26:07.860
>Thanks
>Murat BUDAK
>
Hi Murat,
I don't recommend it, but if you really insist on using the + operator
between two datetime variables to add an amount of time, then you need to
get at least your base date and time right.
Run this in QA
SELECT CAST ('02:15:00.000' AS datetime)
to see why getdate() + '1899-12-30 02:15:00.000' won't result in "now + 2
1/4 hour".
The recommended method to add time to a datetime value is to use DATEADD:
SELECT DATEADD (minute, (60*2) + 15, getdate())
or
SELECT DATEADD (hour, 2, DATEADD (minute, 15, getdate()))
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||On Tue, 8 Feb 2005 04:29:02 -0800, Mal .mullerjannie wrote:
(snip)
>Untested :
>select dateadd(hour,2.15,getdate())
Hi Mal,
DATEADD takes an integer as second parameter, so the fractional part
(0.15) will be discarded.
And even if DATEADD would take fractions, this would still be wrong, as
two hours and fifteen minutes equals 2.25 hours, not 2.15 hours.
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)