Showing posts with label create. Show all posts
Showing posts with label create. Show all posts

Wednesday, March 21, 2012

Datetime Parameters reset to String

When I go to Report > Report Parameters... in VS, I create date parameters and specify the type as datetime. I then add the parameters to my SQL statement. I then go back to look at the parameters and they have changed to string. This happens every time I make a modification to the SQL statement. Is this a bug, or am I missing a step? I thought someone would have asked this before, but I can't find a post for it in this forum.

Thanks in advance,

Scott

Parameter's type can change to string, but it should only happen once. What version are you using?|||I'm using 2005. I'm not 100% sure but I'm fairly certain that it happens every time I change the SQL statement.|||Its Not a bug, Its a Feature :)))

But yes - I don belive its a bug.
It happened to me a few time that when I played with the dataset its changed my parameter type from dateTime to string.

If this will be your only problem with the dateTime parameter you should be happy.
Its also have some problems with some localization sometimes....

Monday, March 19, 2012

DateTime parameter in report

Hi All:

I wanna have DateTime calendar filter in report, so i create a parameter call EndDate which defualt vavlue today, now i have problem how can i get the value from Date Time calendar after every time user selected new date, then i need to pass this date time value to subreport.

Thanks

Nick

Not sure I understand your request well. Are you asking how to pass a parameter value from a main report to a subreport? You may want to read the following BOL page: http://msdn2.microsoft.com/en-us/library/aa337167.aspx. In particular, you can pass parameter values to subreports by editing the subreport properties in the main report and setting the parameter expression to =Parameters!EndDate.Value

-- Robert

|||

hi Robert.

Thanks for your reply, now i got another error after i set up the date time parameter in subreports.

In the property of sub report , i use =Parameters!WeekEnding.Value, weekEnding is date time type parameter both in main and sub report.

now i run the report. i got error like below:

An error occured during local report processing. The value provided for the report parameter "WeekEnding" is not valid for it's type.

I have no ideas about this error message. it works fine if i change WeekEnding type to string both in main and sub report. is that anything i missing out here?

thanks

Nick

DateTime parameter format issue?

Hi All:

I create a report with a DateTime parameter, i would love to use DD/MM/YYYY this format, but it doesn't work . it works when i change to usa datetime format ,YYYY-MM-DD,

any helps are appreciated.

thanks

Nick

hi Nick

this is an example of what I use

this is put in as an expression - Layout view on a table

=Format(Fields!Date3.Value, "dd/MMM/yyyy")

hope this helps

|||

Hi, I had the same issue, this is a documented fix in SP1 for SQL 2005, using formatdatetime function you can fix this,

Andy

|||

It is really help.

i used "DD/mm/yyyy", it works

Thanks you very much jewelfire.

|||

Hi,

I have a report which has a datetime parameter,

when i choose the datetime from calendar control , then click view report button. if i choose 13/12/2006, it would give a error, because the report think it's 13 is month , how can i fix this issue. also i want keep dd/MM/yyyy format?

Thanks

Nick

DateTime parameter format issue?

Hi All:

I create a report with a DateTime parameter, i would love to use DD/MM/YYYY this format, but it doesn't work . it works when i change to usa datetime format ,YYYY-MM-DD,

any helps are appreciated.

thanks

Nick

hi Nick

this is an example of what I use

this is put in as an expression - Layout view on a table

=Format(Fields!Date3.Value, "dd/MMM/yyyy")

hope this helps

|||

Hi, I had the same issue, this is a documented fix in SP1 for SQL 2005, using formatdatetime function you can fix this,

Andy

|||

It is really help.

i used "DD/mm/yyyy", it works

Thanks you very much jewelfire.

|||

Hi,

I have a report which has a datetime parameter,

when i choose the datetime from calendar control , then click view report button. if i choose 13/12/2006, it would give a error, because the report think it's 13 is month , how can i fix this issue. also i want keep dd/MM/yyyy format?

Thanks

Nick

Thursday, March 8, 2012

datetime conversion question

Using SQL2005 DTS - I am trying to import data from a CSV file into a table
created with the following
CREATE TABLE [maint].[dbo].[SpaceAnalysis_v2] (
[Date-Time] datetime,
[Server] text,
[Drive] text,
[Drive Size] numeric(29,0),
[Space Free] numeric(29,0)
)
the first field is date and time and looks like this >>
09/20/2006 06:30:03 PM
But no matter what I try to use for a final field format the result of that
data after it's imported displays the same time for every record >> 12:00:00
AM <<. The date comes through fine, but it just does not seem to recognize
the time. What do I need to do to get the time to be imported correctly ?It appears that the time is not included as part of the date data.
Is the date/time field enclosed in single quotes? (Such as: '09/20/2006
06:30:03 PM') This is a non-standard date format, having two spaces between
the date and time portions, as well as a space between the time and the
AM/PM indicator.
Please post an EXACT excerpt from the import file so that we can visually
see the data to determine if there are problems that are causing a
'mis-load'.
--
Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
You can't help someone get up a hill without getting a little closer to the
top yourself.
- H. Norman Schwarzkopf
"WANNABE" <breichenbach AT istate DOT com> wrote in message
news:eXDUZT1AHHA.4472@.TK2MSFTNGP03.phx.gbl...
> Using SQL2005 DTS - I am trying to import data from a CSV file into a
> table created with the following
> CREATE TABLE [maint].[dbo].[SpaceAnalysis_v2] (
> [Date-Time] datetime,
> [Server] text,
> [Drive] text,
> [Drive Size] numeric(29,0),
> [Space Free] numeric(29,0)
> )
> the first field is date and time and looks like this >>
> 09/20/2006 06:30:03 PM
> But no matter what I try to use for a final field format the result of
> that data after it's imported displays the same time for every record >>
> 12:00:00 AM <<. The date comes through fine, but it just does not seem
> to recognize the time. What do I need to do to get the time to be
> imported correctly ?
>|||As I paste this in here I just realized that my first post was not
absolutely correct, sorry I was looking at the file through excel.
Thanks for your time Arnie, here are the first 2 lines as
displayed using notepad>>
9/19/2006 16:50,EXCEDE,C,36265226240,14397304832
9/19/2006 16:50,EXCEDE,D,147000000000,41808166912
======================================"Arnie Rowland" <arnie@.1568.com> wrote in message
news:O8sLGE3AHHA.3560@.TK2MSFTNGP04.phx.gbl...
> It appears that the time is not included as part of the date data.
> Is the date/time field enclosed in single quotes? (Such as: '09/20/2006
> 06:30:03 PM') This is a non-standard date format, having two spaces
> between the date and time portions, as well as a space between the time
> and the AM/PM indicator.
> Please post an EXACT excerpt from the import file so that we can visually
> see the data to determine if there are problems that are causing a
> 'mis-load'.
> --
> Arnie Rowland, Ph.D.
> Westwood Consulting, Inc
> Most good judgment comes from experience.
> Most experience comes from bad judgment.
> - Anonymous
> You can't help someone get up a hill without getting a little closer to
> the top yourself.
> - H. Norman Schwarzkopf
>
> "WANNABE" <breichenbach AT istate DOT com> wrote in message
> news:eXDUZT1AHHA.4472@.TK2MSFTNGP03.phx.gbl...
>> Using SQL2005 DTS - I am trying to import data from a CSV file into a
>> table created with the following
>> CREATE TABLE [maint].[dbo].[SpaceAnalysis_v2] (
>> [Date-Time] datetime,
>> [Server] text,
>> [Drive] text,
>> [Drive Size] numeric(29,0),
>> [Space Free] numeric(29,0)
>> )
>> the first field is date and time and looks like this >>
>> 09/20/2006 06:30:03 PM
>> But no matter what I try to use for a final field format the result of
>> that data after it's imported displays the same time for every record >>
>> 12:00:00 AM <<. The date comes through fine, but it just does not seem
>> to recognize the time. What do I need to do to get the time to be
>> imported correctly ?
>

Datetime comparsion

Hi,
i need to create a sql statement to find out record between a period, but i
only want to search with the date part only (e.g. 2006/1/23).
In my database, the type of the field is in datetime format. How can i
bypass the time part but still can find out what i need (e.g. 2006/1/23
00:00:00 - 2006/1/23 23:59:59)
sample SQL statement
select * from [table] where [datetime] between [fromdate] and [todate]
Apologies for non-good english, hope you get what i mean.
Thanks and Best Regards,
Sexball20060123 = 20060123 00:00
and
DATEADD(ss,-1,'20060124') = 20060123 23:59:59
select * from [table] where [datetime] between '20060123' and
DATEADD(ss,-1,'20060124')
HTH. Ryan
"sexball" <sexball@.sexball.com> wrote in message
news:%23vw7jBAIGHA.1192@.TK2MSFTNGP11.phx.gbl...
> Hi,
> i need to create a sql statement to find out record between a period, but
> i only want to search with the date part only (e.g. 2006/1/23).
> In my database, the type of the field is in datetime format. How can i
> bypass the time part but still can find out what i need (e.g. 2006/1/23
> 00:00:00 - 2006/1/23 23:59:59)
> sample SQL statement
> select * from [table] where [datetime] between [fromdate] and [todate]
> Apologies for non-good english, hope you get what i mean.
>
> Thanks and Best Regards,
> Sexball
>|||This will work,
SELECT getdate()
SELECT CAST(FLOOR(CAST( getdate() AS float)) AS DATETIME)
Thanks,
Sree
"sexball" wrote:

> Hi,
> i need to create a sql statement to find out record between a period, but
i
> only want to search with the date part only (e.g. 2006/1/23).
> In my database, the type of the field is in datetime format. How can i
> bypass the time part but still can find out what i need (e.g. 2006/1/23
> 00:00:00 - 2006/1/23 23:59:59)
> sample SQL statement
> select * from [table] where [datetime] between [fromdate] and [todate]
> Apologies for non-good english, hope you get what i mean.
>
> Thanks and Best Regards,
> Sexball
>
>|||Hi
You can try as:
select * from [table] where [datetime]
between cast([fromdate] as datetime) and cast([todate] as datetime)
I am not sure what you are looking for. But please let me know if this helps
you.
Please let me know if you have any questions
best Regards,
Chandra
http://chanduas.blogspot.com/
http://www.SQLResource.com/
---
"sexball" wrote:

> Hi,
> i need to create a sql statement to find out record between a period, but
i
> only want to search with the date part only (e.g. 2006/1/23).
> In my database, the type of the field is in datetime format. How can i
> bypass the time part but still can find out what i need (e.g. 2006/1/23
> 00:00:00 - 2006/1/23 23:59:59)
> sample SQL statement
> select * from [table] where [datetime] between [fromdate] and [todate]
> Apologies for non-good english, hope you get what i mean.
>
> Thanks and Best Regards,
> Sexball
>
>|||Thx Ryan, problem solved.
"Ryan" <Ryan_Waight@.nospam.hotmail.com> glsD:%23geO$GAIGHA.3056@.TK2MSFTNGP09.phx.
gbl...
> 20060123 = 20060123 00:00
> and
> DATEADD(ss,-1,'20060124') = 20060123 23:59:59
>
> select * from [table] where [datetime] between '20060123' and
> DATEADD(ss,-1,'20060124')
>
> --
> HTH. Ryan
> "sexball" <sexball@.sexball.com> wrote in message
> news:%23vw7jBAIGHA.1192@.TK2MSFTNGP11.phx.gbl...
>|||hi this
SELECT CONVERT(DATETIME,CONVERT(CHAR(10),GETDAT
E(),120),120)
?
"Chandra" <chandra@.discussions.microsoft.com> ะด?
news:6C1E767F-4026-4752-BA9F-5210A3274F81@.microsoft.com...
> Hi
> You can try as:
> select * from [table] where [datetime]
> between cast([fromdate] as datetime) and cast([todate] as datetime)
> I am not sure what you are looking for. But please let me know if this
helps
> you.
> Please let me know if you have any questions
>
> --
> best Regards,
> Chandra
> http://chanduas.blogspot.com/
> http://www.SQLResource.com/
> ---
>
> "sexball" wrote:
>
but i

Wednesday, March 7, 2012

datetime

create proc dbo.GetList
(
@.OrgList varchar(1000),
@.startDateTime datetime

)
as
begin

declare @.SQL varchar(1000)

set @.SQL = 'Select A.TransactionID,
A.PermitId,
A.IssuingOrganizationId,
A.VehicleId

From Vehicle A WITH (NOLOCK)
join PurchasingCompany B WITH (NOLOCK)
on A.PurchasingCompanyId = B.PurchasingCompanyId
Where
A.IssueDate >= '+ '@.startDateTime' +' And
A.IssuingOrganizationId IN ('+ @.OrgList+')'
exec(@.sql)
end
go

This doesn't work, But If I substitute@.startDateTime with '2/1/2004', it works. I think iam missing some formatting, I tried several ways to make it work. Could anyone tell me how I should do this.

You are including the literal "@.StartDateTime", which is clearly not what you want.

create proc dbo.GetList
(
@.OrgList varchar(1000),
@.startDateTime datetime

)
as
begin

Look at the BOL article on CONVERT to determine what format is right for you - I am using the ODBC canonical format.

declare @.SQL varchar(1000)

set @.SQL = 'Select A.TransactionID,
A.PermitId,
A.IssuingOrganizationId,
A.VehicleId

From Vehicle A WITH (NOLOCK)
join PurchasingCompany B WITH (NOLOCK)
on A.PurchasingCompanyId = B.PurchasingCompanyId
Where
A.IssueDate >= '''+ CONVERT(nvarchar(30),@.startDateTime,120) +''' And
A.IssuingOrganizationId IN ('+ @.OrgList+')'
exec(@.sql)
end
go

|||That really worked for me, thank you so much for the reply.

Saturday, February 25, 2012

Dates parameters

I create a report that brings information from an informix database. I have a
problem with date parameters when I put them static on query design (with
date format mm/dd/yyyy) it retreives information on a good response time. But
if I preview report with the option to add date paramaters reports doesn't
display information it always says "Processing report". I think that the
problem is that SQL adds the time to parameter because if I add time to date
value on design mode it sends an error.
Please let me know if you have comments
I will appreciate it
thanksThe date parameter is definitely date/time. If the data type in your
informix database just holds date then you need to use one of the date
functions and strip off the time. Go to your dataset and click on the ...
and go to the parameters tab. You will see the query parameter that maps to
the report parameter, change the report parameter to an expression and have
the expression convert it to just the date. You can use any of the functions
in Microsoft.VisualBasic, System.Convert, and System.Math without have to
write any custom code. Read up on expressions.
Bruce L-C
"Yoya" <Yoya@.discussions.microsoft.com> wrote in message
news:0CDE2FB6-C49F-47AB-B11D-40B54FB32965@.microsoft.com...
> I create a report that brings information from an informix database. I
have a
> problem with date parameters when I put them static on query design (with
> date format mm/dd/yyyy) it retreives information on a good response time.
But
> if I preview report with the option to add date paramaters reports doesn't
> display information it always says "Processing report". I think that the
> problem is that SQL adds the time to parameter because if I add time to
date
> value on design mode it sends an error.
> Please let me know if you have comments
> I will appreciate it
> thanks
>

dates issue

Hi,
In SQL Server when I create a stored procedure, in the list of stored
procedures the Create Date column used to be formatted as:
28/09/2004 14:35:24
But since yesterday, for some reason, when a new stored procedure is
created, the date appear as :
2005-05-31 14:03:07.854

Why??!? This is very annoying to sort my procedures by date as it
messes up everything..

RegardsSam (samuel.berthelot@.voila.fr) writes:
> In SQL Server when I create a stored procedure, in the list of stored
> procedures the Create Date column used to be formatted as:
> 28/09/2004 14:35:24
> But since yesterday, for some reason, when a new stored procedure is
> created, the date appear as :
> 2005-05-31 14:03:07.854
> Why??!? This is very annoying to sort my procedures by date as it
> messes up everything..

SQL Server is a server application, so SQL Server itself does not
display anything. So apparently you see this in some client tool,
and thus the formatting depends on that tool. Since I don't know
which tool you use, I will have to guess a bit.

In Query Analyzer, dates are always displayed as YYYY-MM-DD HH:mm:ss.fff,
unless you check "Use regional settings" under Tools->Options.

In Enterprise Manager, under Databases->db->stored procedures there is
a listing. I would expect this listing to respect your regional settings.
Currently I see YYYY-MM-DD HH:mm:ss, which are my regional settings, but
this could very well be a hard-coded format.

I don't know about this screen, but I seem to recall that there are
other screesn in EM, when you sort on dates, it sorts the string, which
is a disaster with a format like DD/MM/YYYY.

Anyway, YYYY-MM-DD is the international standard for date formats, so
why not get used to it?

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||why not get used to it?

Simply because stored procedures created before yesterday have the
format YYYY-MM-DD and the stored procedure that I create now have
another format, therefore I can't sort by date the list anymore or it's
messed up...:(|||Sam (samuel.berthelot@.voila.fr) writes:
> why not get used to it?
> Simply because stored procedures created before yesterday have the
> format YYYY-MM-DD and the stored procedure that I create now have
> another format, therefore I can't sort by date the list anymore or it's
> messed up...:(

Huh? Sounds very strange. First of all, whatever function you are using,
close it down and start it again. (Or just press a Refresh button.)

If the problem still is there, take a screen shot, put it on a web
site, and post the link.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||for some reasons, I've closed down sql server and all dates are in the
same format now... weird... but my problem is solved anyway ... :)
thx|||Sam (samuel.berthelot@.voila.fr) writes:
> for some reasons, I've closed down sql server and all dates are in the
> same format now... weird... but my problem is solved anyway ... :)

And whatever you closed down, I'm quite sure that it wasn't SQL Server.
You closed down some tool which you did not tell us what it was. I
might be pedantic by making this point, but in many situations it's
important to understand what is SQL Server what is the tool. SQL Server
itself does *not* have a user interface.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||By SQL Server, I meant Entreprise Manager... sorry for the confusion

Dates being saved incorrectly

Hi,
I've got a question about saving into datetime fields in a SQL Server table. A form I have create has two fields both for dates as well as other form fields, but the user may or may not fill in all the form fields, so when they click the save link I have a query which saves all the form fields whether they are blank or not. Unfortunately this is causing the two date fields to be saved in the database as "01/01/1900" even thoough they are blank fields.

What is a good way to not save blank date fields as "01/01/1900"?

Thanks

Stephen

Two choises:

1). If user doesn't leave the field blank, you can set DateTime = System.DateTime.Now, so that you can get the current dattime;

2). Allow your datetime column in your table as NULL value, or GetDate() as Default value, so that if user didn't put value, you can set the field as NULL or current datetime

Hope it helps

Friday, February 24, 2012

Dates - information entered 3 months ago

Hello All,
I need to create stored procedure that will output information created 3
months after the record was created. For example: if the stored procedure was
run today or based on a date parameter I would like it to output all records
created 3 months ago to that day. There are other parameters I need,but I
think I can take care of those,
Thanks in advance.
--
Message posted via SQLMonster.com
http://www.sqlmonster.com/Uwe/Forums.aspx/sql-server-reporting/200804/1On Apr 29, 5:22=A0pm, "Jay via SQLMonster.com" <u7124@.uwe> wrote:
> Hello All,
> I need to create stored procedure that will output information created 3
> months after the record was created. For example: if the stored procedure =was
> run today or based on a date parameter I would like it to output all recor=ds
> created 3 months ago to that day. There are other parameters I need,but I
> think I can take care of those,
> Thanks in advance.
> --
> Message posted via SQLMonster.comhttp://www.sqlmonster.com/Uwe/Forums.aspx=
/sql-server-reporting/200804/1
In SQL try:
SET DATEPARAM =3D DATEADD(MONTH,-3,GETDATE())
In SSRS/VB try:
=3DDateAdd(DateInterval.Month, -3, Today())
HTH
toolman

Dates

I'd like to create a query with a where clause that gets data for the
previous month, ie: when running the report in January the report would use
dates from 12/1 to 12/31.
How can this be done?Something like this?
/ ****************************************
*******************************
Determine the start and end of the previous month
****************************************
*******************************/
DECLARE @.dt datetime,
@.PrevMonthStart datetime,
@.PrevMonthEnd datetime
SELECT @.dt = getdate()
SELECT @.PrevMonthStart = dateadd(month,datediff(month,'1900',@.dt)
-1,'1900'),
@.PrevMonthEnd = dateadd(month,datediff(month,'1900',@.dt)
,'1900')-1
SELECT @.PrevMonthStart AS PreviousMonthStart, @.PrevMonthEnd AS
PreviousMonthEnd
Keith
"Jim" <Jim@.discussions.microsoft.com> wrote in message
news:DD304C3D-5BFD-460B-956B-37D0B4944D4B@.microsoft.com...
> I'd like to create a query with a where clause that gets data for the
> previous month, ie: when running the report in January the report would
use
> dates from 12/1 to 12/31.
> How can this be done?|||Thanks Keith, it worked.
"Keith Kratochvil" wrote:

> Something like this?
>
> / ****************************************
*******************************
> Determine the start and end of the previous month
> ****************************************
*******************************/
> DECLARE @.dt datetime,
> @.PrevMonthStart datetime,
> @.PrevMonthEnd datetime
> SELECT @.dt = getdate()
> SELECT @.PrevMonthStart = dateadd(month,datediff(month,'1900',@.dt)
-1,'1900'
),
> @.PrevMonthEnd = dateadd(month,datediff(month,'1900',@.dt)
,'1900')-1
> SELECT @.PrevMonthStart AS PreviousMonthStart, @.PrevMonthEnd AS
> PreviousMonthEnd
>
> --
> Keith
>
> "Jim" <Jim@.discussions.microsoft.com> wrote in message
> news:DD304C3D-5BFD-460B-956B-37D0B4944D4B@.microsoft.com...
> use
>

Dates

trying to create a stored proc with this query:
select * from table1 where datefield BETWEEN @.startdate
AND @.enddate.
When using Crystal reports against this,it works fine
except when you are just using one day- for example your
start date and end date would both be 9/2/2004. Anyone run
into this? Should I do something different? THANKS!probably you are not considering time component. See following example.
create table t(dt datetime)
insert into t values ('20040903 12:24:15')
insert into t values ('20040903 1:24:15')
insert into t values ('20040903 16:24:15')
insert into t values ('20040903')
insert into t values ('20040904')
select * from t
where dt between '20040903' and '20040903 23:59:59'
Vishal Parkar
vgparkar@.yahoo.co.in | vgparkar@.hotmail.com
"Ann" <anonymous@.discussions.microsoft.com> wrote in message
news:51bb01c49134$9db6b420$a501280a@.phx.gbl...
> trying to create a stored proc with this query:
> select * from table1 where datefield BETWEEN @.startdate
> AND @.enddate.
> When using Crystal reports against this,it works fine
> except when you are just using one day- for example your
> start date and end date would both be 9/2/2004. Anyone run
> into this? Should I do something different? THANKS!

Sunday, February 19, 2012

DateDiff That Only Counts Working Days

Hi! I'm trying to create a query to calculate the number of days between two dates, but I only want to include working days. Is there a way to do this?Most DBAs work seven days a week, weekends and holidays included ;). The provided DateDiff works fine for us!

On a (very slightly) more serious note, what constitutes a "working day" by your definition?

-PatP|||LOL, I definitely realize what days DBAs work... :)

Working days are just the typical Monday through Friday.|||Vacations? Holidays? Snow days? Office closed due to threat of terrorist attack? Those all affect "working days". Or did you mean to ask "I'm trying to create a query to calculate the number of weekdays between two dates, excluding weekends."?|||Would you include Holidays as work days?

IF not I would suggest you have a table calendar with field workday
values 1 , 0

sum the workday field to get your answer.|||Yes, this is what I'm trying to say:

"I'm trying to create a query to calculate the number of weekdays between
two dates, excluding weekends."|||http://www.aspfaq.com/show.asp?id=2453

Friday, February 17, 2012

DATEDIFF in Report Builder

I'm having a bit of trouble with the DATEDIFF function in my Report Model project and in Report Builder.

I am trying to create a new Expression field that will work out a persons age from the current date and their Date of Birth, here is the formula that I have entered,

DATEDIFF("y", NOW(), DOB)

where DOB is the field that holds the persons date of birth in the database.

When I enter this into the formula box and click OK i get the following error,

"Operation is not valid due to the current state of the object."

The detailed error text is

Program Location:

at Microsoft.ReportingServices.Modeling.Expression.GetResultType()
at Microsoft.ReportingServices.ModelDesigner.ModelDesignerControl.m_ListNoItemExpression_Click(Object sender, EventArgs e)
at Microsoft.ReportingServices.ModelDesigner.ProjectModelView.Microsoft.DataWarehouse.Interfaces.ICommandTarget.InvokeCommand(MenuCommand menuCommand)
at Microsoft.DataWarehouse.VsIntegration.Designer.Host.CommandTargetMenuService.InvokeCommand(CommandID commandID)

This error ocurrs both in a Report Model project and also in report builder if I try to create a new field after the model has been deployed.

This also happens for DATEADD as well, I'm thinking it may be a problem with the first parameter but I can't find any reference on how to use this Function properly. Has anyone got either of these working in report builder, even if you don't could you post here to let me know you have the same problem.

Would be good to know i'm not alone in this.

Thanks|||I still really need help on this one.

If someone could load up Report Builder and try to create a new field using the DATEDIFF function this would really help me out.

If you get it working then i'll be able to take that onboard, if you don't get it working then I know i'm not alone.

Any help is appreciated|||

Report Builder? You mean Report Designer?
Why don't you get the age from the Query instead of getting it as a new field in the Report Desinger? I mean, add a calculated field directly to the Query, somehting like

SELECT [whatever you actually have], DATEDIFF(YEAR, GETDATE(), DOB) AS age
[and the rest of your query]

Or maybe I didn't understand your question

|||"When I enter this into the formula box"

Are you creating a field in the Repor Designer?
I wouldn't use NOW(), but Globals!ExecutionTime|||This issue has now been resolved, it was not occurring in Report Designer, it was occuring in Report Builder.

The DATEDIFF function in Report Builder must use the "long" names for the Interval and these must be capatalized.

E.g
"Day" - Will work
"dd" - Won't work
"day" - Won't work|||I have a similar question.

I can't seem to get the DATEDIFF function to work. I am trying to display the date from seven days prior to now.

My textbox has the value of...

=format(dateadd(Day, -7, Globals!ExecutionTime), "M/d")

and the error I get is...

Argument not specified for parameter 'DateValue' of Public Function Day(DateValue as Date) as Integer.

I just can't get my head around this one, and I'm sure it's simple. ANY help would be appreciated!|||Sorry, I meant to say I can't get the DateAdd function to work.|||

Day is a function, so it's expecting a parameter (a date value). I'd try to get that value from the query, not from a formula in a Text Box.
Maybe it's not the best solution, but I'd create a Dataset called DataSet1wkago with this query string:

SELECT DATEADD(DAY, -7, GETDATE()) AS last_week

And in the textbox I'd write

=First(Fields!last_week, "DataSet1wkago")

Again: this could be not the best solution, but it works.
I hope it helps you. Regards

|||Thanks, it's not the best solution, but it is still a solution!|||If you are trying to use this function in Report Builder then you will have to enclose the Interval in quotes e.g.

DATEADD("Day", -7, Globals!ExecutionTime)|||

I have the same problems with the data interval.

=Datediff("Day",Parameters!fromdate.Value,Now())

That I am using in a field in a Reporting Services report. I can not use the datediff in the query as I am using a parameter formattet at datetime.

Any suggestions on how this work or where to find useful documentation on expressions in Report Designer?

Thomas Black

|||

Hi,

try using "d" instead of "Day":

=Datediff("d", Parameters!fromdate.Value, now())

below are the list of 'code':

Setting Description
yyyy Year
q Quarter
m Month
y Day of year
d Day
w Weekday
ww Week of year
h Hour
n Minute
s Second

DATEDIFF in Report Builder

I'm having a bit of trouble with the DATEDIFF function in my Report Model project and in Report Builder.

I am trying to create a new Expression field that will work out a persons age from the current date and their Date of Birth, here is the formula that I have entered,

DATEDIFF("y", NOW(), DOB)

where DOB is the field that holds the persons date of birth in the database.

When I enter this into the formula box and click OK i get the following error,

"Operation is not valid due to the current state of the object."

The detailed error text is

Program Location:

at Microsoft.ReportingServices.Modeling.Expression.GetResultType()
at Microsoft.ReportingServices.ModelDesigner.ModelDesignerControl.m_ListNoItemExpression_Click(Object sender, EventArgs e)
at Microsoft.ReportingServices.ModelDesigner.ProjectModelView.Microsoft.DataWarehouse.Interfaces.ICommandTarget.InvokeCommand(MenuCommand menuCommand)
at Microsoft.DataWarehouse.VsIntegration.Designer.Host.CommandTargetMenuService.InvokeCommand(CommandID commandID)

This error ocurrs both in a Report Model project and also in report builder if I try to create a new field after the model has been deployed.

This also happens for DATEADD as well, I'm thinking it may be a problem with the first parameter but I can't find any reference on how to use this Function properly. Has anyone got either of these working in report builder, even if you don't could you post here to let me know you have the same problem.

Would be good to know i'm not alone in this.

Thanks|||I still really need help on this one.

If someone could load up Report Builder and try to create a new field using the DATEDIFF function this would really help me out.

If you get it working then i'll be able to take that onboard, if you don't get it working then I know i'm not alone.

Any help is appreciated|||

Report Builder? You mean Report Designer?
Why don't you get the age from the Query instead of getting it as a new field in the Report Desinger? I mean, add a calculated field directly to the Query, somehting like

SELECT [whatever you actually have], DATEDIFF(YEAR, GETDATE(), DOB) AS age
[and the rest of your query]

Or maybe I didn't understand your question

|||"When I enter this into the formula box"

Are you creating a field in the Repor Designer?
I wouldn't use NOW(), but Globals!ExecutionTime|||This issue has now been resolved, it was not occurring in Report Designer, it was occuring in Report Builder.

The DATEDIFF function in Report Builder must use the "long" names for the Interval and these must be capatalized.

E.g
"Day" - Will work
"dd" - Won't work
"day" - Won't work|||I have a similar question.

I can't seem to get the DATEDIFF function to work. I am trying to display the date from seven days prior to now.

My textbox has the value of...

=format(dateadd(Day, -7, Globals!ExecutionTime), "M/d")

and the error I get is...

Argument not specified for parameter 'DateValue' of Public Function Day(DateValue as Date) as Integer.

I just can't get my head around this one, and I'm sure it's simple. ANY help would be appreciated!|||Sorry, I meant to say I can't get the DateAdd function to work.|||

Day is a function, so it's expecting a parameter (a date value). I'd try to get that value from the query, not from a formula in a Text Box.
Maybe it's not the best solution, but I'd create a Dataset called DataSet1wkago with this query string:

SELECT DATEADD(DAY, -7, GETDATE()) AS last_week

And in the textbox I'd write

=First(Fields!last_week, "DataSet1wkago")

Again: this could be not the best solution, but it works.
I hope it helps you. Regards

|||Thanks, it's not the best solution, but it is still a solution!|||If you are trying to use this function in Report Builder then you will have to enclose the Interval in quotes e.g.

DATEADD("Day", -7, Globals!ExecutionTime)|||

I have the same problems with the data interval.

=Datediff("Day",Parameters!fromdate.Value,Now())

That I am using in a field in a Reporting Services report. I can not use the datediff in the query as I am using a parameter formattet at datetime.

Any suggestions on how this work or where to find useful documentation on expressions in Report Designer?

Thomas Black

|||

Hi,

try using "d" instead of "Day":

=Datediff("d", Parameters!fromdate.Value, now())

below are the list of 'code':

Setting Description
yyyy Year
q Quarter
m Month
y Day of year
d Day
w Weekday
ww Week of year
h Hour
n Minute
s Second

DateDiff - Return Months in Decimal?

I've been trying to create a function that returns the difference, in months, between two dates. The DateDiff function (m) returns an integer, but I really need a decimal. I could return the days instead, but it would be difficult to figure out how the number of months from this, especially when the dates span multiple years and I need to adjust for leap year. Does anyone know of a resource that might have a solution for this?

Thanks,
RobI guess you're looking for the decimal because you want what percentage of the month?

Do you need to know the number of days in that month?

It's not really a straight forward kind of thing.

I mean is this really 2 months?

SELECT DATEDIFF(m,'6/30/2004','8/31/2004')

Or this one being 1?

SELECT DATEDIFF(m,'6/30/2004','7/1/2004')|||A value of 1.5 days has meaning since it always comes out to 36 hours or x minutes or whatever. A value of 1.5 months has no meaning since months are not all the same length. 1.5 Februarys has less days than 1.5 Marchs.

Or this one being 1?

SELECT DATEDIFF(m,'6/30/2004','7/1/2004')

Seems like it if I'm at work... :)|||You could do years with a decimal...

check out the bio

http://www.sqlteam.com/forums/pop_profile.asp?mode=display&id=7198|||.695890

Cool... I'll use that formula next time I have a government form to fill out. Drive them nuts for a change.

Only problem is that years don't have 365 days in them. Actually around 365.25 I think. Hence the 'makeup' day in leap years. Since time is a human concept, why not just make every month 30 days. Year end would see us 4 days short but Dec 31st we could make 120 hours long vs. 24. That would keep the calendar in sync with the seasons. Unfortunately though, Jan 1st, at only 24 hours would not have enough time to recover from the 120 hour hangover... :)|||.695890

Cool... I'll use that formula next time I have a government form to fill out. Drive them nuts for a change.

Only problem is that years don't have 365 days in them. Actually around 365.25 I think. Hence the 'makeup' day in leap years. Since time is a human concept, why not just make every month 30 days. Year end would see us 4 days short but Dec 31st we could make 120 hours long vs. 24. That would keep the calendar in sync with the seasons. Unfortunately though, Jan 1st, at only 24 hours would not have enough time to recover from the 120 hour hangover... :)

That's funny....|||Actually, the solar year is pretty close to 365.2425 terrestrial days. So we just... Oh nevermind, I've been down this road WAY too many times now!

-PatP|||Unfortunately though, Jan 1st, at only 24 hours would not have enough time to recover from the 120 hour hangover... :)You can recover from a hangover ?!?! We just figured it would never end, so we never allow the hangover to start!

-PatP|||A pre-req for most DBA positions for sure. Listening to end users gripe for 8 hours a day used to be painful. Since I took up drinking I can now blissfully tune them out. Boss came to me years ago and said he felt my drinking was interferring with my work. I replied that my work was interferring with my drinking and quit on the spot. Never looked back. Next round's on me!

datediff

when I create the table I used

[from_time] [smalldatetime] NULL ,
[to_time] [smalldatetime] NULL ,
[total_hrs] AS (datediff(hh,[from_time],[to_time])) ,

but the hours calculation is comming wrong. moreover i want set the format like hh:mi. but i could not. pls helpRefer to DATEPART function under books online.#

HTH|||elams if your goal is to return elapsed time, you might want to change your calculated field to hold minutes or seconds rather than hours, this would make it easier to re-format.

as an alternative, here is a function that will return elapsed time.

Create function fn_ElapsedTime (
@.starttime datetime,
@.endtime datetime = Null)
returns varchar(40)
as
begin
declare @.d int, @.h int, @.m int, @.s int, @.ms int, @.dif1 int, @.ret varchar(40)
select @.d = 0, @.h = 0, @.m = 0, @.s = 0, @.ms = 0

set @.d = datediff(dd,@.starttime,@.endtime)
set @.dif1 = datediff(ms,dateadd(dd,@.d,@.starttime),@.endtime)

if (@.dif1 > 0) begin
set @.ms = @.dif1 % 1000
set @.dif1 = @.dif1 - @.ms
set @.s = ((@.dif1 / 1000) % 60)
set @.dif1 = @.dif1 - (@.s * 1000)
set @.m = ((@.dif1 / 60000) % 60)
set @.dif1 = @.dif1 - (@.m * 60000)
set @.h = ((@.dif1 / 3600000) % 60)
end

set @.ret = cast(@.d as varchar(25)) + ':' +
right('00' + cast(@.h as varchar(2)),2) + ':' +
right('00' + cast(@.m as varchar(2)),2) + ':' +
right('00' + cast(@.s as varchar(2)),2) + ':' +
right('000' + cast(@.ms as varchar(3)),3)

return @.ret
end

GO

create table #Tmp(
from_time smalldatetime
, to_time smalldatetime
, total_hrs as (datediff(hh,from_time, to_time))
)
insert into #Tmp values(getdate(), dateadd(dd,4,getdate()))
insert into #Tmp values(getdate(), dateadd(hh,2,getdate()))
insert into #Tmp values(getdate(), dateadd(mi,3,getdate()))
select from_time
, to_time
, total_hrs
, dbo.fn_ElapsedTime(from_time,to_time) as Elapsed_Time
From #Tmp|||it works. Thanks mate
elam

Tuesday, February 14, 2012

DATEADD - Add Weekdays Only

Hi,

I have a problem with working out some dates.

I have a query that has a start date field and a number of days field.

I know i can create another field that could provide the return date (DATEADD function) by adding the number of days to the start date.

However the problem I have is that i need to discount the weekends from the return date.

For example if the start date was a wednesday and the number of days was 3 the datadd sum would give a return date of saturday when in reality it should be monday.

I am not sure if i am making sense but if anyone out there has any ideas it would be more than welcome.

Andrew

What is generally being referred to here is CALENDAR days and WORKING days

The .Net system revolves around calendars, which do not support the notion of working days.

You have to also check if holiday days of any sort have an impact (Bank holidays, Christmas etc). If this is the case, thse change from year to year, so you might need to keep a database of these special days

Assuming that you only want weekdays to be part of the calculation, then creating your own class might be the answer. You cannot inherit from DateTime, which is sealed.

Here is an (UNTESTED) example of what you might do - it isn't the only solution you might consider. however, use it as a starting point to get you thinking

publicclassWeekdayDateTime

{

staticTimeSpan DateDiff(DateTime startDate,DateTime endDate)

{

// move start to MOnday if weekend

if (startDate.DayOfWeek ==DayOfWeek.Saturday)

startDate.AddDays(2);

elseif (startDate.DayOfWeek ==DayOfWeek.Sunday)

startDate.AddDays(1);

// move end date to Friday if weekend

if (endDate.DayOfWeek ==DayOfWeek.Sunday)

endDate.AddDays(-2);

elseif (endDate.DayOfWeek ==DayOfWeek.Saturday)

endDate.AddDays(-1);

// get duration including rest of weekends

TimeSpan ts = endDate.Subtract(startDate);

//if less than a week (including mmoving weekends in initial process, then return diff

if (startDate.AddDays(5) <= endDate)

return ts;

// must be one or more weekends to remove

// complete weeks to consider

int completeWeeks = ts.Days / 7;

// 2 week end days for every weekday

TimeSpan weekendTime =newTimeSpan(completeWeeks * 2, 0, 0, 0, 0);

// remove weekends

return ts.Subtract(weekendTime);

}

}

|||

Thanks for that sbyard,

Though what you have said is of great interest, i maybe should clarify a bit more on what i am actually trying to do.

I am trying to use the DATEADD function in an sql query, all my data is stored in a table for holidays. It has the start date of the holiday and the number of days.

My ultimate goal is to provide a datagrid webpage that shows people who are currently on holiday.

For example if someone takes the 10th of September of for 4 days and i run the page on the 11th or 12th they will be included in the list.

My logic at the moment is if i can get the query itself to show the start date and the return date with DATEADD i can get the webpage to filter the datagrid based on those two dates.

I may be going down the wrong road for this but at the moment it is the best idea i have got.

Of course any suggestions are openly accepted.

Andrew

|||

Working type calendars are always a lot of work as there is a lot to consider.

Let’s consider data volume against speed and ease of use.

If you record every day for every user in a table and whether they are working (user id, date, dayTypeId), then each user has 365 rows per year (366 leap year). Each user is created with the public holidays already set, and it is easy to add/remove holiday days by setting the dayTypeId field. This links to a table where the Id is described as (e.g.) 1=working day, 2=public holiday, 3=annual leave, 4 = TOIL, etc.

This sounds like a lot of data, but you can archive old data off after <n> years, and records are only 12 bytes each – about 4k per person per year – hardly taxing to the system - and you can easily index all the fields to quickly select data how you want.

The above method is easy to query and you not only get the data you want, but the type of holiday and who did exactly what in one place

If you store holiday days only, you use a lot less data volume, but you simply move the work into processing complexity – as you are finding! So ease of use is reduced – especially when someone asks you to extend the system with different types of day off, which the first model is easy to change.

I suggest starting with my initial option above, and if you can shoot it down with another method, then go that way. I would suggest simplicity every time.

|||

SELECTDATEADD(d,CASEDATEPART(dw,StartDate)WHEN 7THEN 2WHEN 1THEN 1ELSE 0END,StartDate)+(DATEPART(dw,DATEADD(d,CASEDATEPART(dw,StartDate)WHEN 7THEN 2WHEN 1THEN 1ELSE 0END,StartDate))-2+Duration)%5+((DATEPART(dw,DATEADD(d,CASEDATEPART(dw,StartDate)WHEN 7THEN 2WHEN 1THEN 1ELSE 0END,StartDate))-2+Duration)/5)*7-(DATEPART(dw,DATEADD(d,CASEDATEPART(dw,StartDate)WHEN 7THEN 2WHEN 1THEN 1ELSE 0END,StartDate))-2) As ReturnDate

FROM {YourTable}

of course that assumes your fields are named StartDate (datetime) and Duration (int).

|||

That is the point sbyard is trying to make but the user did not understand it can get both complicated and convoluted. The link below is for the US but I am sure there maybe a UK version or sbyard could have one handy.


http://www.smart.net/~mmontes/ushols.html

http://www.timeanddate.com/worldclock/full.html

|||Of course, what I gave does not account for Holdays, and your logic is going to start to get really complicated if your have multiple holidays that are consecutive, or the duration does not count holidays that may occur during this "Holiday". In which case, it'll get many times more complicated. What I gave is fairly crude. It's what DATEADD(dw,Duration,StartDate) would return *IF* it actually added weekdays (Although reading the help it would seem to indicate it would, it doesn't unfortunately -- What is referred to as "weekday" really should have been called "DayOfWeek" which doesn't make a lot of sense for dateadd).|||

With my simple record-per-day method, you can also have holiday types that are full day, half day, etc, if you do not want to record hours worked.

However, I suggest you might want to add a small decimal field xx.xx to allow accurate quarter hours to be captured as a persons working day (obviously set to zero for full holiday days) - if this appropriate for your project

You now also have a way of tracking worked time for little extra effort - you might even want to extend the system into being a timesheet/holiday package.

Of course, there is over-time and other time types to consider, as well as flexi-time, etc., etc.

If your application is multi-country, you will also need to extend the public-holiday table with a country field (linked to a country table), and link each user to a country - but don'e even ask about more migrant workers who travel from office to office and might be not-working of different public holidays in different countries!

The water looks lovely lad, but by 'eck it's fair deep - as they say in my adopted county here in England.

|||

Motley,

I am sorry for the time it has taken for me to reply but your answer was exactly what i was looking for.

Thanks very much.

I know it doesnt take in to account holidays but i will save that task for another day.

Regards

Andrew