Showing posts with label date. Show all posts
Showing posts with label date. 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.

Is the SP3a the most recent MSDE distribution

I ask this question because the sp3a version I have contains a merge module -
atl.msm - which is dated (modified date) 5/14/03 and is 76KB in size whereas
the "atl.msm" that is issued with VB6 SP6 contains the same merge module but
is dated Mar 14, 2004 and is 87KB in size.
Is this simply because VB6 has issued a more recent atl? If so, when will
the MSDE be brought up to date? Or better yet, since I am distributing the
atl.msm with my setup package, can I safely replace the one in the MSDE with
the more recent version?
Regards,
Jamie
hi,
thejamie wrote:
> I ask this question because the sp3a version I have contains a merge
> module - atl.msm - which is dated (modified date) 5/14/03 and is 76KB
> in size whereas the "atl.msm" that is issued with VB6 SP6 contains
> the same merge module but is dated Mar 14, 2004 and is 87KB in size.
> Is this simply because VB6 has issued a more recent atl? If so, when
> will the MSDE be brought up to date? Or better yet, since I am
> distributing the atl.msm with my setup package, can I safely replace
> the one in the MSDE with the more recent version?
MSDERelA is based on the service pack 3a of SQL Server, which is the most
recent complete distribution...
we have to wait until sp4 is available for newer versions..
Andrea Montanari (Microsoft MVP - SQL Server)
http://www.asql.biz/DbaMgr.shtmhttp://italy.mvps.org
DbaMgr2k ver 0.11.1 - DbaMgr ver 0.57.0
(my vb6+sql-dmo little try to provide MS MSDE 1.0 and MSDE 2000 a visual
interface)
-- remove DMO to reply
|||Is there any word that an sp4 is imminent? Is it waiting for the SQL Server
2005 to come out of beta?
"Andrea Montanari" <andrea.sqlDMO@.virgilio.it> wrote in message
news:3c4hfeF6johu1U1@.individual.net...
> hi,
> thejamie wrote:
> MSDERelA is based on the service pack 3a of SQL Server, which is the most
> recent complete distribution...
> we have to wait until sp4 is available for newer versions..
> --
> Andrea Montanari (Microsoft MVP - SQL Server)
> http://www.asql.biz/DbaMgr.shtmhttp://italy.mvps.org
> DbaMgr2k ver 0.11.1 - DbaMgr ver 0.57.0
> (my vb6+sql-dmo little try to provide MS MSDE 1.0 and MSDE 2000 a visual
> interface)
> -- remove DMO to reply
>
|||Dont think so , SP4 is announced for this or next month.
HTH, Jens Smeyer
http://www.sqlserver2005.de
"jamie" <anonymous@.nospam.somewhere.com> schrieb im Newsbeitrag
news:enT%23QkTQFHA.2132@.TK2MSFTNGP09.phx.gbl...
> Is there any word that an sp4 is imminent? Is it waiting for the SQL
> Server 2005 to come out of beta?
> "Andrea Montanari" <andrea.sqlDMO@.virgilio.it> wrote in message
> news:3c4hfeF6johu1U1@.individual.net...
>
|||Look like it is out as of 5/6/05
"jamie" <anonymous@.nospam.somewhere.com> wrote in message
news:enT%23QkTQFHA.2132@.TK2MSFTNGP09.phx.gbl...
> Is there any word that an sp4 is imminent? Is it waiting for the SQL
> Server 2005 to come out of beta?
> "Andrea Montanari" <andrea.sqlDMO@.virgilio.it> wrote in message
> news:3c4hfeF6johu1U1@.individual.net...
>
|||SP4 just shipped. See http://www.microsoft.com/sql/downloads/2000/sp4.asp
for details on MSDE.
joe.
"jamie" <anonymous@.nospam.somewhere.com> wrote in message
news:%23YovRe$UFHA.1148@.tk2msftngp13.phx.gbl...
> Look like it is out as of 5/6/05
> "jamie" <anonymous@.nospam.somewhere.com> wrote in message
> news:enT%23QkTQFHA.2132@.TK2MSFTNGP09.phx.gbl...
>
sql

Wednesday, March 21, 2012

Is the a similar function to NOW()

Hi,

I just imported a MS Access table to SQL Server via DTC. The table has a date field in it that I would like to populate automatically. In fact the access table used the Now() funtion to do just that. Evertime an insert statement occured the current date and time would be inserted into the u_createDate field.

The now function is not working in MS SQL. Is there a similar funciton that will automatically update the u_createDate field when a record in inserted?

MikeYou can use the GETDATE() function.|||Originally posted by manowar
You can use the GETDATE() function.

Thank you, Mike

Friday, March 9, 2012

Is SP command quicker than recordset?

Hi,
I have a web site that uses ASP. I want to know if a record exists for
a specific date. I will be doing this 30 times for each refresh of the
page.
What I've setup is a simple SP that takes a date as an input and
returns a count.
CREATE PROCEDURE spGetNumber
(
@.Date smalldatetime
)
AS
SET NOCOUNT ON
DECLARE @.Count tinyint
SELECT @.Count = COUNT(ID) FROM Race WHERE Date=@.Date
RETURN @.Count
--
In my ASP, I'm calling a command object.
It seems pretty fast, but I only have 100 records in the table, 1 user
at a time. In the future I expect 100's of users and 10,000's of
records.
Is this the best method?
thx,
Bodihi bodi
u can continue using the SP. it is advisible to contact the database only
once from the ASP page instead of contacting 30 times
best Regards,
Chandra
http://chanduas.blogspot.com/
http://groups.msn.com/SQLResource/
---
"BodiKlamph@.gmail.com" wrote:

> Hi,
> I have a web site that uses ASP. I want to know if a record exists for
> a specific date. I will be doing this 30 times for each refresh of the
> page.
> What I've setup is a simple SP that takes a date as an input and
> returns a count.
> --
> CREATE PROCEDURE spGetNumber
> (
> @.Date smalldatetime
> )
> AS
> SET NOCOUNT ON
> DECLARE @.Count tinyint
> SELECT @.Count = COUNT(ID) FROM Race WHERE Date=@.Date
> RETURN @.Count
> --
> In my ASP, I'm calling a command object.
> It seems pretty fast, but I only have 100 records in the table, 1 user
> at a time. In the future I expect 100's of users and 10,000's of
> records.
> Is this the best method?
> thx,
> Bodi
>|||On 7 Aug 2005 11:18:46 -0700, BodiKlamph@.gmail.com wrote:

>Hi,
>I have a web site that uses ASP. I want to know if a record exists for
>a specific date. I will be doing this 30 times for each refresh of the
>page.
Hi Bodi,
Issuing 30 consecutive calls is not a good idea. If the sole purpose is
to find the dates for which no row exists, rewrite the stored procedure
to take two parameters (starting and ending date) and return a resultset
of all dates for which no rows exist.

>What I've setup is a simple SP that takes a date as an input and
>returns a count.
Another thing: if you only need to verify existence, use EXISTS, not
COUNT. A query with COUNT will always continue until all matching rows
are found. A query with EXISTS will stop after the first matching row.

>It seems pretty fast, but I only have 100 records in the table, 1 user
>at a time. In the future I expect 100's of users and 10,000's of
>records.
10,000's of rows is not very much for SQL Server, but still - why incur
extra overhead that can easily be avoided.

>Is this the best method?
The best method involves a calendar table. If you don't have one yet in
your database, go ahead and create it now. Instructions for making a
calendar table and many examples of how you can use one are at this
site: http://www.aspfaq.com/show.asp?id=2519.
Remember that you only need to create the calendar table once! Just
don't forget to add some new rows every year or two.
Once you have a calendar table, you can find the dates without row in
your Race table with the following stored procedure:
CREATE PROC FindDatesWithoutRowInRace
(@.StartDate datetime,
@.EndDate datetime)
AS
SELECT c.dt
FROM dbo.Calendar AS c
WHERE c.dt >= @.StartDate
AND c.dt <= @.EndDate
AND NOT EXISTS
(SELECT *
FROM Race AS r
WHERE e.[Date] = c.dt)
go
The final step would be to change the calling code to issue one call
with two parameters, then fetch and process the rows returned by the
stored procedure.
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||A couple things in addition to Chandra's and Hugo's posts...
The return value from an SP is intended to be a success/failure indicator,
like a windows app - returning non-zero indicates failure, so when you get a
count > 0 and return it you're indicating that the SP failed.
If you want a count of matching records you should use an output parameter;
if you simply want to test for existance you should use EXISTS like Hugo
said, and set the output BIT to 1...
Another note, with COUNT - generally you want to use COUNT(*) not
COUNT([field_name]) because then SQL can optimize to use the best index
rather than have to use an index or constraint (assuming one exists) with th
e
specified field.
I'm going to go out on a limb and guess that you're trying to display a
calendar, hence the 30 calls per request. For this scenario you should retur
n
records that fall in a specific date range:
SELECT DISTINCT [date]
FROM [table_owner].Race
WHERE [date] BETWEEN @.lo_date AND @.hi_date
Then populate the calendar from the recordset in your ASP. Rememebr that
datetime includes time, so if you want records thru the end of the last date
specified then you'll have to be sure either all dates in the table have
00:00:00.000 for the time portion or @.lo_date has time 00:00:00.000 and
@.hi_date has time 23:59:59.997.
Be sure your command objkect's type is set to stored procedure, not text
query, for fastest execution.
Good luck, - KH
"BodiKlamph@.gmail.com" wrote:

> Hi,
> I have a web site that uses ASP. I want to know if a record exists for
> a specific date. I will be doing this 30 times for each refresh of the
> page.
> What I've setup is a simple SP that takes a date as an input and
> returns a count.
> --
> CREATE PROCEDURE spGetNumber
> (
> @.Date smalldatetime
> )
> AS
> SET NOCOUNT ON
> DECLARE @.Count tinyint
> SELECT @.Count = COUNT(ID) FROM Race WHERE Date=@.Date
> RETURN @.Count
> --
> In my ASP, I'm calling a command object.
> It seems pretty fast, but I only have 100 records in the table, 1 user
> at a time. In the future I expect 100's of users and 10,000's of
> records.
> Is this the best method?
> thx,
> Bodi
>|||As above...
But think in terms of SETS of data.
What is the ideal SET of data that the web application could get - KH has
already suggested "the set of dates where there are no races". This may not
be ideal for your real needs, but is definitely a substantial improvement on
making multiple calls that s out many times singular Yes / No answers -
which aren't sets at all, they are simple questions.
- Tim
<BodiKlamph@.gmail.com> wrote in message
news:1123438726.697862.102120@.g43g2000cwa.googlegroups.com...
> Hi,
> I have a web site that uses ASP. I want to know if a record exists for
> a specific date. I will be doing this 30 times for each refresh of the
> page.
> What I've setup is a simple SP that takes a date as an input and
> returns a count.
> --
> CREATE PROCEDURE spGetNumber
> (
> @.Date smalldatetime
> )
> AS
> SET NOCOUNT ON
> DECLARE @.Count tinyint
> SELECT @.Count = COUNT(ID) FROM Race WHERE Date=@.Date
> RETURN @.Count
> --
> In my ASP, I'm calling a command object.
> It seems pretty fast, but I only have 100 records in the table, 1 user
> at a time. In the future I expect 100's of users and 10,000's of
> records.
> Is this the best method?
> thx,
> Bodi
>|||In addition to the already mentioned ideas:
If you are always making exactly 30 calls, do the different values of date
have a relationship to one another?
For example, using a resultset instead of the return output:
SELECT sum (case when date = @.date then 1 else 0 end) as first,
sum(case when date = dateadd(minute,-1,@.date) then 1 else 0 end)
as second
...
sum(case when date = dateadd(minute,N-1,@.date) then 1 else 0 end)
as Nth
FROM race
WHERE date between @.date and dateadd(minute,N-1,@.date)
or something along these lines and it will probably be faster. Even doing
thirty parms and then putting the thirty sums would likely be better because
of networkIO type stuff.
----
Louis Davidson - http://spaces.msn.com/members/drsql/
SQL Server MVP
<BodiKlamph@.gmail.com> wrote in message
news:1123438726.697862.102120@.g43g2000cwa.googlegroups.com...
> Hi,
> I have a web site that uses ASP. I want to know if a record exists for
> a specific date. I will be doing this 30 times for each refresh of the
> page.
> What I've setup is a simple SP that takes a date as an input and
> returns a count.
> --
> CREATE PROCEDURE spGetNumber
> (
> @.Date smalldatetime
> )
> AS
> SET NOCOUNT ON
> DECLARE @.Count tinyint
> SELECT @.Count = COUNT(ID) FROM Race WHERE Date=@.Date
> RETURN @.Count
> --
> In my ASP, I'm calling a command object.
> It seems pretty fast, but I only have 100 records in the table, 1 user
> at a time. In the future I expect 100's of users and 10,000's of
> records.
> Is this the best method?
> thx,
> Bodi
>|||thx Hugo, your answer seems to best suite my needs.
Yes, 30 calls is quite bad. Returning a recordset with just hte valid
dates is a much better idea. then i can loop in my asp code and add
only the valid calendar days.
question? wut's up with the calendar table you mentioned; why would I
need that?
Can't I just do:
SELECT DISTINCT [Date] FROM myTable WHERE [Date] >= startDate and
[Date] <= endDate
that would return only the valid dates|||K, I finished making the changes.
2 questions.
I have a loop that writes out my calendar days (part of an ASP calendar
class). For each day, i want to see if there is a corresponding record
in the resultset I do this by an inefficeint (tha'ts why i'm here)
recordset.find method. I have to search from record 1 each time tho
This is probaly the wrong group now, since it's more ADO than SQL, but
I'll give it a host since you're already familiar with my question.
I was using set rs = cmd.execute, but it compalined that my recordset
was forward only. So I cahnged it form a SP to a simple recordset.open
(using dbopendynamic so i could use the .find method).
I was thinking it may be easier to use GetRows() then loop through the
much less bulky array. Not sure how I'd do this yet, but I can think
of something...unless there is already a set way of doing this?
thx,
Bodi|||lol
at this point I'm just talking to myself.
I used the GetString method, with a row deliminator of comma
then, instead of .find, I did:
if instr(rsDays, "," & DAY(DateString) & ",")
works like a charm, and i imagine it's way faster to do a string check
then it is a .find
thx everybody|||On 8 Aug 2005 21:13:48 -0700, BodiKlamph@.gmail.com wrote:

>thx Hugo, your answer seems to best suite my needs.
>Yes, 30 calls is quite bad. Returning a recordset with just hte valid
>dates is a much better idea. then i can loop in my asp code and add
>only the valid calendar days.
>question? wut's up with the calendar table you mentioned; why would I
>need that?
>Can't I just do:
>SELECT DISTINCT [Date] FROM myTable WHERE [Date] >= startDate and
>[Date] <= endDate
>that would return only the valid dates
Hi Bodi,
I must have misunderstood you earlier. I thought you wanted to find the
dates that are not yet in your table. The query above will do the
reverse: find the dates that are (at least once) in the table.
But do remember that a datetime column includes a time part as well. If
you don't ensure that the time part is the same for all entries, the
query above might return the same date several times, since the DISTINCT
will look at the time part as well.
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)