Showing posts with label requirements. Show all posts
Showing posts with label requirements. Show all posts

Friday, March 23, 2012

Is there a better way, problems with dates

Hi

I have a bunch of info that I need to order by date. I have two requirements and I can get one or the other to work but not both. I need the date to be returned in the format:

dd/mm/yyyy

I have no problem with this I use a function called DatePart:


CREATE FUNCTION dbo.DatePart
( @.fDate datetime )
RETURNS varchar(10)
AS
BEGIN
RETURN ( CONVERT(varchar(10),@.fDate,103) )
END

that removes the extra parts that I don't need.

So for eg the result for my select statement is :


SELECT DISTINCT Master_Jobs.JobID, Profiles.ProfileDescriptor, Backup_UserNotes.BackUp_Read, Master_Jobs.Job_Title, Master_Jobs.Contact,
dbo.DatePart(Master_Jobs.Due_Date) as Due_Date, Master_Jobs.Due_Time, Master_Jobs.Next_Action, Master_Jobs.By_Who
<code
problem is this wont allow me to order on the following:

<code>
Order By Due_Date ASC
<code
because its not longer a datetime, ie it was converted to nvarchar by the function.

I can if I remove the above and use:

<code>
CONVERT(datetime,Due_Date, 102) AS Due_Date
<code
in my selects, and order by

<code>
ORDER BY CONVERT(datetime,Due_Date, 102) ASC

like this.

But then I lose the ability to format my date. I know some might say well why don't you format it out on the presentation layer but I don't want to do that. I simply want to have my dates formatted by ascending date and truncated to give dd/mm/yyyy. Sorry for the long story. Any help would be most appreciated.wanna dirty fix?

return (Month(@.fDate) + '/' + Day(@.fDate) + '/' + Year(@.fDate))

:)|||It should still work if you do not use the Alias name for ordering but the full qualified name, so
instead of:


Order By Due_Date ASC

do:

Order By Master_Jobs.Due_Date ASC

Further there is already a build in DatePart() function so you should use a different name for yours.

Hth,

Moon|||OK, this works:


Order By Master_Jobs.Due_Date ASC

but how do I get this formatted to dd/mm/yyyy before I return it?|||OK I have sql returning my dates in the fashion:

2002-02-21 00:00:00.000
2004-02-27 00:00:00.000
2002-02-16 00:00:00.000
2004-02-06 00:00:00.000
2004-02-06 00:00:00.000

which would be perfect if i could:

1. get it in the format dd/mm/yyyy

2. if I could lose the 00:00:00:000

But since nobody seems to know how, It looks like I'm going to have to format it at the presentation layer, something I wanted to avoid. I know this question was originally placed as a sql question but now that I'm having to format it asp.net side could someone tell me how I might set my datagrid to format this in dd/mm/yyyy.

Thanks for your help.|||On the SQL side:

CONVERT(nvarhcar(20),yourdatefield,103)

This will return the date as a string. That is the only way on the SQL side to "get rid" of the time part of a DateTime field.|||Thanks for that.

I tried it but it when I go to run it it throws the error:

Server: Msg 104, Level 15, State 1, Line 7
ORDER BY items must appear in the select list if the statement contains a UNION operator.

any idea why|||Exactly what it says. If you are using a UNION operator in the select clause, then the ORDER BY items need to appear in the SELECT list. So, even if you do not need it, you must include whatever is in the ORDER BY in the list of SELECTed fields.|||I don't understand. I get this error even though both my order items appear in both selects. I've posted my code just in case i've missed something obvious.


--CREATE PROCEDURE spGetJobsByUnreadAndReadByUserID
DECLARE @.UserID INT
-- AS
SET @.UserID = 5

SELECT DISTINCT Master_Jobs.JobID, Profiles.ProfileDescriptor, Backup_UserNotes.BackUp_Read, Master_Jobs.Job_Title, Master_Jobs.Contact,
CONVERT(nvarhcar(20),Master_Jobs.Due_Date,103), Master_Jobs.Due_Time, Master_Jobs.Next_Action, Master_Jobs.By_Who
FROM Master_Jobs INNER JOIN
Note ON Master_Jobs.JobID = Note.FK_JobID INNER JOIN
Backup_UserNotes ON Note.NoteID = Backup_UserNotes.BackUp_NoteID INNER JOIN
User_Notes ON Note.NoteID = User_Notes.FK_UN_NoteID INNER JOIN
Job_Assignments ON Master_Jobs.JobID = Job_Assignments.FK_Master_JobID INNER JOIN
Profiles ON Master_Jobs.FK_ProfileID = Profiles.ProfileID INNER JOIN
Users ON Backup_UserNotes.BackUp_UserID = Users.UserID AND User_Notes.FK_UN_UserID = Users.UserID AND
Job_Assignments.UserID = Users.UserID
WHERE Users.UserID = @.UserID AND BackUp_Read = 'Read'
AND Master_Jobs.JobID not in (
SELECT Master_Jobs.JobID
FROM Master_Jobs INNER JOIN
Note ON Master_Jobs.JobID = Note.FK_JobID INNER JOIN
Backup_UserNotes ON Note.NoteID = Backup_UserNotes.BackUp_NoteID INNER JOIN
User_Notes ON Note.NoteID = User_Notes.FK_UN_NoteID INNER JOIN
Job_Assignments ON Master_Jobs.JobID = Job_Assignments.FK_Master_JobID INNER JOIN
Profiles ON Master_Jobs.FK_ProfileID = Profiles.ProfileID INNER JOIN
Users ON Backup_UserNotes.BackUp_UserID = Users.UserID AND User_Notes.FK_UN_UserID = Users.UserID AND
Job_Assignments.UserID = Users.UserID
WHERE Users.UserID = @.UserID AND BackUp_Read = 'UnRead')

union

SELECT DISTINCT Master_Jobs.JobID, Profiles.ProfileDescriptor, Backup_UserNotes.BackUp_Read, Master_Jobs.Job_Title, Master_Jobs.Contact,
CONVERT(nvarhcar(20),Master_Jobs.Due_Date,103), Master_Jobs.Due_Time, Master_Jobs.Next_Action, Master_Jobs.By_Who

FROM Master_Jobs INNER JOIN
Note ON Master_Jobs.JobID = Note.FK_JobID INNER JOIN
Backup_UserNotes ON Note.NoteID = Backup_UserNotes.BackUp_NoteID INNER JOIN
User_Notes ON Note.NoteID = User_Notes.FK_UN_NoteID INNER JOIN
Job_Assignments ON Master_Jobs.JobID = Job_Assignments.FK_Master_JobID INNER JOIN
Profiles ON Master_Jobs.FK_ProfileID = Profiles.ProfileID INNER JOIN
Users ON Backup_UserNotes.BackUp_UserID = Users.UserID AND User_Notes.FK_UN_UserID = Users.UserID AND
Job_Assignments.UserID = Users.UserID

WHERE Users.UserID = @.UserID AND-- Note.FK_UserID = User_Notes.FK_UN_UserID AND
BackUp_Read = 'UnRead'

ORDER BY Master_Jobs.Due_Date asc-- BackUp_Read DESC, Master_Jobs.Due_Date asc
GO

|||No, you are not.

Look at your select. It includes:

CONVERT(nvarhcar(20),Master_Jobs.Due_Date,103)

However, your ORDER BY includes:

Master_Jobs.Due_Date

These are two seperate things. Add Master_Jobs.Due_Date to your SELECT list in both SELECTS (yes, I know you do not really need it in this context) and I expect you will be fine.|||Thanks for that, that did it.

But could you quickly tell me why I need two references to Master_Jobs.Due_Date in my select statement when using an order by.

<code>
SELECT DISTINCT Master_Jobs.JobID, Profiles.ProfileDescriptor, Backup_UserNotes.BackUp_Read, Master_Jobs.Job_Title, Master_Jobs.Contact,
CONVERT(nvarchar(20),Master_Jobs.Due_Date,103), Master_Jobs.Due_Date, Master_Jobs.Due_Time, Master_Jobs.Next_Action, Master_Jobs.By_Who
<code
Why two:
<code>
CONVERT(nvarchar(20),Master_Jobs.Due_Date,103)
Master_Jobs.Due_Date
<code
I'm a bit slow!!|||:: It looks like I'm going to have to format it at the presentation layer, something I wanted to avoid

Why? As you can see there is no clean/convenient way to format a date in SQL-Server so ... doing it in the presentation layer is the right place to do this imo.
You can format dates in C# like this:


DateTime.Parse(myDataSetValue).ToShortDateString()
|||

CONVERT(nvarchar(20),Master_Jobs.Due_Date,103)

>> this column is a computed column which is not part of the original table and therefore a different thing.

Monday, March 19, 2012

Is SSRS right for our needs?

I'd really appreciate some help deciding if SQL Server Reporting Services is
right for our needs. We have the following two requirements:
1) Users of our ASP .Net application pull reports from the new ASP .Net
SSRS embedded control.
(Users log in with Forms Authentication)
2) Automated nightly push of emailed reports to thousands of users.
Q1: Does the new ASP .Net embedded control for SSRS work with the
aspnet_users and aspnet_roles tables to identify the current user, display
the list of reports for them and manage security login to SQL Server?
Q2: Can the generation of SSRS report Excel-Output be automated to file
from a nightly SQL Server Job or perhaps from an application that is kicked
off at midnight?
I apologize if this seems a little vague. It's almost as if asking the
question is more difficult than answering it. Ultimately, the question is:
Will SSRS meet our needs for both Pull and Push reports?
Danny LesandriniOn Jul 13, 10:17 am, "dlesandrini" <dataf...@.comcast.net> wrote:
> I'd really appreciate some help deciding if SQL Server Reporting Services is
> right for our needs. We have the following two requirements:
> 1) Users of our ASP .Net application pull reports from the new ASP .Net
> SSRS embedded control.
> (Users log in with Forms Authentication)
> 2) Automated nightly push of emailed reports to thousands of users.
> Q1: Does the new ASP .Net embedded control for SSRS work with the
> aspnet_users and aspnet_roles tables to identify the current user, display
> the list of reports for them and manage security login to SQL Server?
> Q2: Can the generation of SSRS report Excel-Output be automated to file
> from a nightly SQL Server Job or perhaps from an application that is kicked
> off at midnight?
> I apologize if this seems a little vague. It's almost as if asking the
> question is more difficult than answering it. Ultimately, the question is:
> Will SSRS meet our needs for both Pull and Push reports?
> Danny Lesandrini
If I'm following you correctly, in Q1 you are referring to the report
viewer control (which works w/both Crystal Reports and Reporting
Services). The current user is available in SSRS via this expression:
=User!UserID.ToString
As far as I know, controlling access to SQL Server is done via the
datasource set up and controlled via the Report Manager. Otherwise,
controlling report access can only be set via general user security in
the Report Manager. Users can be set up to just browse/view reports,
etc (but no control over which reports, just all). Of course, the
stored procedure/query that sources the report can control whether or
not the user sees the results if the stored procedure accepts an input
parameter that receives the User expression given above.
In regards to Q2, the Report Manager has built in snapshot reporting
and scheduling, etc. The report can be scheduled to execute on a
schedule and exported to Excel.
The main difference between Crystal Reports and Reporting Services is
that the license for SSRS is free w/a SQL Server license on the same
box; however, Crystal Reports requires its own license. So, SSRS is
much cheaper on the budget. Also, Crystal Reports and SSRS have
slightly different export options. SSRS has very convenient report
export scheduling.
Hope this helps in your decision.
Regards,
Enrique Martinez
Sr. Software Consultant