Question

First day of week gives different results in sql and crystal reports

Asked by: sullym6

I have a function writen in SQL that returns the first day of a given week as follows
( @pInputDate    DATETIME )
RETURNS datetime
Begin
Declare @result datetime
if datepart(dw,@pInputDate)= 7
      set @result = convert(datetime,@pInputDate,126)
else
      set @result = convert(datetime,DATEADD(DD,  0-DATEPART(DW, @pInputDate),@pInputDate),126)
RETURN @result
END
This works fine in SQL but when the query is run in Crystal 11 reports I get different results (seem to be 1 day out) Now I am thinking it is because DATEPART(DW, @pInputDate) gives different results in SQL and Crystal.

In SQL a date of 27/07/2009 give result of 25/07/2009 (correct)
In Crystal a date of 27/07/2009 gives result of 26/07/2009 (wrong)

I am running SQL server 2000 SP3 and crsytal reports X1 revison 2

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-09-16 at 03:18:50ID24735764
Tags

SQL Server

,

Crystal reports

Topics

Crystal Reports Software

,

SQL Server 2005

Participating Experts
3
Points
500
Comments
23

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. crystal reports datepart and dateadd
    I have a begindatetime field and a shift field I am passing as a parameter to a report. If the shift =1, I want to return all records from the begindatetime up to midnight of that day. I was trying to do a datepart function to remove the time portion of this and then add i...
  2. Help w/ DatePart and DateAdd to get weekending
    I have a select statment that will tell me how many records were recorded each week. The result shows me the date of the end of the week and how many records. Ex. weekending num 4/15/06 5 4/22/06 6 SELECT CONVERT(varchar(10), DATEADD(day, 1 - DATEPA...
  3. Crystal Reports  datepart
    I have created a formula field, with the following formula: datepart("yyyy", {sp_Reports;1.issued} ). The problem is that it returns the year in the following format: 2,005.00. I would like to see the year like this: 2005. How can I fix this? Thank you
  4. Convert DATEADD and DATEPART functions to Oracle
    I am have troubling figuring out how to convert the code snippet that I have attached. Is there a similar function to the DATEADD ms sql function and DATEPART functions in Oracle? Or is the DATEADD function not needed at all with just the appropriate DATEPART function suffi...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: rrjegan17Posted on 2009-09-16 at 03:37:24ID: 25343926

>> In SQL a date of 27/07/2009 give result of 25/07/2009 (correct)
In Crystal a date of 27/07/2009 gives result of 26/07/2009 (wrong)

Are they both referring to the same database..
If not, then SET DATEFIRST differs in both databases.

Kindly check it by using

SELECT @@DATEFIRST

Default Beginning date of a week is 7 ie., Sunday..
You have to set the First date of your week to your required value to make it work

Values 1-Monday....7-Sunday

 

by: sullym6Posted on 2009-09-16 at 03:45:54ID: 25343993

Hi rrjegan17

The function is written in sql and then a view is created in sql which is then linked to by crystal reports.
I know it is something to do with how each system interprets the first day of the week, sql give 7 for select @@datefirst but I don't know how to check or change it in crystal.
What I don't understand is that if the function is in sql and the view in sql and gives the correct result then why does crystal not just see the results. It seems like crystal runs the view and funtions from within itself and gets a different value for the datepart(dw,@pInputDate)  and causes my problem.

 

by: rrjegan17Posted on 2009-09-16 at 04:00:45ID: 25344097

Ok.. Include this at the beginning of your Function

SET DATEFIRST 7;

Kindly try it out and update me..

 

by: rrjegan17Posted on 2009-09-16 at 04:03:54ID: 25344110

I just looked into this one..
It will bring wrong results by fetching last day of earlier week
set @result = convert(datetime,DATEADD(DD,  0-DATEPART(DW, @pInputDate),@pInputDate),126)

Instead you have to use the one below to fetch first day of this week

set @result = convert(datetime,DATEADD(DD,  0-DATEPART(DW, @pInputDate),@pInputDate),126)

 

by: rrjegan17Posted on 2009-09-16 at 04:04:15ID: 25344116

Typo..


Instead you have to use the one below to fetch first day of this week

set @result = convert(datetime,DATEADD(DD, 1-DATEPART(DW, @pInputDate),@pInputDate),126)

 

by: sullym6Posted on 2009-09-16 at 04:25:21ID: 25344230

I should have explained the first day of our week is a Sat and hence the 0-Datepart etc. Either way with the change you sugested the answers are different in SQL and Crystal as above example

In SQL a date of 27/07/2009 give result of 26/07/2009 now a sunday
In Crystal a date of 27/07/2009 gives result of 27/07/2009 which is a monday and wrong

I have tried to use SET DATEFIRST 7 at the begining of funcion bt get error you can't use it in a function

Thanks

Sully

 

by: rrjegan17Posted on 2009-09-16 at 05:02:50ID: 25344579

>> I should have explained the first day of our week is a Sat

Have you made any configuration changes for this in SQL Server say.. Regional settings, Date & Time, etc..

 

by: sullym6Posted on 2009-09-16 at 05:13:27ID: 25344691

no the only differnece is that sql is on our server and crystal on my local machine, maybe there is a difference in reginal settings, I will check and report back.

Sully

 

by: sullym6Posted on 2009-09-16 at 06:04:26ID: 25345115

O.k the regional settings are the same but I have found a way around the problem, I just need to know how to adjust the first day of week setting in Crystal report.
Currently
SQL is Sun =1 to Sat =7
crystal mon=1 to Sun=7

The code I used is a follows (please remember my 1st day of week needed to be sat)
( @pInputDate    DATETIME )
RETURNS datetime
Begin
Declare @result datetime

if datepart(dw,convert(datetime,@pInputDate,126))= 6 --checks for Sat
      set @result = convert(datetime,@pInputDate,126)
else if datepart(dw,convert(datetime,@pInputDate,126))= 7 --if sunday we want to -1 not -8 as next bit of code would have done
      set @result = convert(datetime,DATEADD(DD,  -1,@pInputDate),126) --deals with all other days
else
      set @result = convert(datetime,DATEADD(DD,  -1-DATEPART(DW, @pInputDate),@pInputDate),126)
 RETURN @result

END

 

by: PCIIainPosted on 2009-09-16 at 06:07:50ID: 25345137

Are you using the same database user in your connection to SQL server from crystal as you are when checking the results in sql server. You may find that datefirst is a user dependant value.

 

by: rrjegan17Posted on 2009-09-16 at 06:28:49ID: 25345337

Can you try the slightly modified one, which would work for both SQL Server and Crystal

IF datename(dw,@pInputDate)= 'Saturday'
      set @result = convert(datetime,@pInputDate,126)
ELSE
BEGIN
      set @result = CASE datename(dw,convert(datetime,DATEADD(DD,  0-DATEPART(DW, @pInputDate),@pInputDate),126)) WHEN 'Saturday'
      THEN convert(datetime,DATEADD(DD,  0-DATEPART(DW, @pInputDate),@pInputDate),126)
      ELSE dateadd(dd, -1, convert(datetime,DATEADD(DD,  0-DATEPART(DW, @pInputDate),@pInputDate),126)) end
END

                                              
1:
2:
3:
4:
5:
6:
7:
8:

Select allOpen in new window

 

by: sullym6Posted on 2009-09-16 at 07:19:31ID: 25345835

PCIIain yes I am using the same user when connecting to SQL and Crystal, the only thing I can think of is because the machines are different.
rrjegan17 I copied and pasted your code and get Incorrect syntax near end.

 

by: rrjegan17Posted on 2009-09-16 at 07:46:05ID: 25346166

Can you provide the code which you tried so that I can fix in it..

 

by: sullym6Posted on 2009-09-16 at 07:51:30ID: 25346220

here you are

CREATE FUNCTION [dbo].[fnDayOfWeek3
]
( @pInputDate    DATETIME )
RETURNS datetime
Begin
Declare @result datetime
IF datename(dw,@pInputDate)= 'Saturday'
      set @result = convert(datetime,@pInputDate,126)
ELSE
BEGIN
      set @result = CASE datename(dw,convert(datetime,DATEADD(DD,  0-DATEPART(DW, @pInputDate),@pInputDate),126)) WHEN 'Saturday'
      THEN convert(datetime,DATEADD(DD,  0-DATEPART(DW, @pInputDate),@pInputDate),126)
      ELSE dateadd(dd, -1, convert(datetime,DATEADD(DD,  0-DATEPART(DW, @pInputDate),@pInputDate),126)) 
	
end
 
END
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:

Select allOpen in new window

 

by: rrjegan17Posted on 2009-09-16 at 08:20:21ID: 25346575

You missed one more END..
This should work..

CREATE FUNCTION [dbo].[fnDayOfWeek3]
( @pInputDate    DATETIME )
RETURNS datetime
Begin
Declare @result datetime
IF datename(dw,@pInputDate)= 'Saturday'
      set @result = convert(datetime,@pInputDate,126)
ELSE
BEGIN
      set @result = CASE datename(dw,convert(datetime,DATEADD(DD,  0-DATEPART(DW, @pInputDate),@pInputDate),126)) WHEN 'Saturday'
      THEN convert(datetime,DATEADD(DD,  0-DATEPART(DW, @pInputDate),@pInputDate),126)
      ELSE dateadd(dd, -1, convert(datetime,DATEADD(DD,  0-DATEPART(DW, @pInputDate),@pInputDate),126)) end
END 
END

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:

Select allOpen in new window

 

by: sullym6Posted on 2009-09-16 at 08:33:56ID: 25346741

Hi

I tried the code and it gives different answers to what I have. I am comparing against known answers so I would say that the code you provided may not work in crystal as you expect. I am thinking there is maybe a config setting I need to change to get crystal to see sun=1 to Sat=7 so matches SQL

Thanks

Sully

 

by: mlmccPosted on 2009-09-16 at 09:59:49ID: 25347715

I thought the default in Crystal was Sun=1 Sat = 7

Crystal date functions allow the use of a FIrstDayofWeek indicator.

mlmcc

 

by: rrjegan17Posted on 2009-09-17 at 00:33:02ID: 25353538

sullym6,
    Have one question here..

You are creating an SQL Function.
So whatever date you provide from Crystal or SQL Server to that Function, it should provide the same set of output since it is going to execute in SQL Server only and not in any other place.

Kindly confirm

 

by: sullym6Posted on 2009-09-17 at 01:20:26ID: 25353786

rrjegan17, it is very strange what is happening and although I have written a function which works it is still not ideal.
The function I posted is the one I am using in a query in SQL this is then linked to via odbc connection by crystal. Now running the query in SQL and browsing the field data in crystal give differen results. We have worked out that Crystal in looking at the weekday numbers differently  but I have no idea of how to change this.
I hope that answers your question

I have included my function code and also the query written in sql. As you can see from the screenshots the same query gives different results as a result of different answers from datepart.
Sully

CREATE FUNCTION [dbo].[fnDayOfWeek2]
( @pInputDate    DATETIME )
RETURNS datetime
Begin
Declare @result datetime
 
if datepart(dw,convert(datetime,@pInputDate,126))= 6
	set @result = convert(datetime,@pInputDate,126)
else if datepart(dw,convert(datetime,@pInputDate,126))= 7
	set @result = convert(datetime,DATEADD(DD,  -1,@pInputDate),126)
else
	set @result = convert(datetime,DATEADD(DD,  -1-DATEPART(DW, @pInputDate),@pInputDate),126)
 RETURN @result
 
END
 
----query using above
 
CREATE VIEW dbo.vqryWeeklyTotals
AS
SELECT     dbo.fnDayOfWeek2(BookingDate) AS Weeks, SUM(TimeInHours) AS TimeA, SUM(TimeInHoursRateA) AS TimeB, SUM(TimeInHoursRateB) 
                      AS TimeC
FROM         dbo.tblTimesheetBookings
WHERE     (BookingDate >= CONVERT(DATETIME, '2009-07-01 00:00:00', 102))
GROUP BY dbo.fnDayOfWeek2(BookingDate)

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:

Select allOpen in new window

 

by: PCIIainPosted on 2009-09-17 at 02:01:07ID: 25354015

Your datefirst setting in SQL server has got to be different for the two queries.

1. Try running a command in a crystal report :-
select @@datefirst as date_first, dbo.fnDayOfWeek2(current_timestamp) AS Testdate
and see what this returns.

2. Try explicitly setting datefirst in your function

 

by: sullym6Posted on 2009-09-17 at 02:17:04ID: 25354097

There is a difference as I have proved this already with my results
The result of your query in SQL
Datefirst = 7
Testdate = 11/09/2009
and in crystal
Datefirst =1
Testdate=12/09/2009

I have tried to set datefirst my function but get error this is not allowed.

Thanks

Sully

 

by: PCIIainPosted on 2009-09-17 at 03:48:25ID: 25354573

Yeah, but that's not IN crystal, that's FROM crystal.

Crystal is not calculating @@datefirst, SQL server is. Something in the connection protocol from Crystal to SQL server is resetting/not setting datefirst.

Some googling indicates that the datefirst value is set in the syslanguages table.
I think the language setting is different for your two different connection methods. Are you using ODBC or ADO to connect from crystal. If ODBC, have you checked the language setting in the DSN (third page of configuration, at the top).

 

by: sullym6Posted on 2009-09-17 at 04:20:40ID: 25354746

PCllain you sir are genius, my connection was not stating what the language should be, setting it to us_english fixed the problem.

Thanks to rrjegan17 also for the time and effort you put in to help me with this.

Sully

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...