Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
mAdMaLuDaWg
Starting Member
42 Posts |
Posted - 2006-05-12 : 05:28:44
|
Hello All,I have a stored procedure as follows:ALTER PROCEDURE dbo.spGetUpdatedChats @lastUpdate DateTime OUTPUT ASDeclare @XMLTime as int Select UserName,ChatText,(Convert(varchar(20),ChatDate,101)+' ' +(SELECT dbo.FixHour(Convert(varchar(20),ChatDate,108)))) as ChDate from Chats where Hidden='N' and ChatDate>@lastUpdate order by ChatDate DESC FOR XML AUTO ; Select @lastUpdate=Max(ChatDate) from ChatsRETURN The problem with the code is that the stored procedure always seems to be doing a ChatDate>=@lastUpdate instead of a > than comparison. For example, if I execute the sp with a parameter of '5/12/2006 2:35:37 PM', the resultset will contain all rows >= a date of 5/12/2006 2:35:37 PM.Sample resultset:<Chats UserName="TEST" ChatText="Test2" ChDate="05/12/2006 2:37:00 PM"/> <Chats UserName="TEST" ChatText="test" ChDate="05/12/2006 2:35:37 PM"/>Can someone tell me whats wrong?Oh and just in case, here is the code for my UDF (sucks that there isn't a CONVERT parameter that outputs in 'mm/dd/yyyy hh:mm:ss AM/PM')ALTER FUNCTION dbo.FixHour(@Time varchar(20))RETURNS varchar(20)ASBEGINDeclare @hour intDeclare @minsec varchar(10)Declare @AMPM varchar(2)Declare @cHour intset @minsec=Substring(@Time,3,len(@Time))set @hour=Cast(Substring(@Time,1,2) as int) if @hour=12 set @cHour=0 else if @hour>12 set @cHour=@hour-12 else set @cHour=@hour if @hour<12 set @AMPM='AM'else set @AMPM='PM' return cast(@cHour as varchar(2))+@minsec+' '+@AMPMEND |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-05-12 : 08:19:02
|
You mean like this?---------------------------------------------CREATE FUNCTION dbo.fnMyOwnDateFormat( @WhatDate DATETIME)RETURNS VARCHAR(22)ASBEGIN RETURN REPLACE(CONVERT(VARCHAR, @WhatDate, 101) + LEFT(RIGHT(CONVERT(VARCHAR, @WhatDate, 109), 14), 8) + ' ' + RIGHT(CONVERT(VARCHAR, @WhatDate, 109), 2), ' ', ' ')END---------------------------------------------and then---------------------------------------------ALTER PROCEDURE dbo.spGetUpdatedChats( @LastUpdate DateTime OUTPUT)ASSET NOCOUNT ONSELECT UserName, ChatText, dbo.fnMyOwnDateFormat(ChatDate) ChDateFROM ChatsWHERE Hidden = 'N' AND ChatDate > @LastUpdateORDER BY ChatDate DESCFOR XML AUTOSELECT @LastUpdate = MAX(ChatDate)FROM Chats---------------------------------------------Or... Directly---------------------------------------------ALTER PROCEDURE dbo.spGetUpdatedChats( @LastUpdate DateTime OUTPUT)ASSET NOCOUNT ONSELECT UserName, ChatText, REPLACE(CONVERT(VARCHAR, ChatDate, 101) + LEFT(RIGHT(CONVERT(VARCHAR, ChatDate, 109), 14), 8) + ' ' + RIGHT(CONVERT(VARCHAR, ChatDate, 109), 2), ' ', ' ') ChDateFROM ChatsWHERE Hidden = 'N' AND ChatDate > @LastUpdateORDER BY ChatDate DESCFOR XML AUTOSELECT @LastUpdate = MAX(ChatDate)FROM Chats |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-05-12 : 08:21:54
|
Also, you could try to rewriteAND ChatDate > @LastUpdatetoAND CONVERT(DATETIME, ChatDate) > @LastUpdate |
 |
|
mAdMaLuDaWg
Starting Member
42 Posts |
Posted - 2006-05-12 : 09:56:37
|
Thanks for the nifty little script to convert the date in the right format... ChatDate is already a DateTime type so converting it does no good, this is really puzzling me, I've been pulling my hair out trying to figure out whats wrong... as a temporary fix, I just did this: ... and Cast(ChatDate as varchar(20))>Cast(@lastUpdate as varchar(20))and it seems to be working, I'd still like to know why comparing the dates in the datetime format doesn't seem to work. |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-12 : 10:00:39
|
can you post some sample data ? KH |
 |
|
mAdMaLuDaWg
Starting Member
42 Posts |
Posted - 2006-05-12 : 10:05:49
|
Sure...1 User1 Hello N 5/7/2006 3:42:00 PM2 User2 Hello N 5/7/2006 3:43:00 PM3 User1 TEST N 5/7/2006 3:45:00 PM4 User2 TEST N 5/7/2006 3:48:00 PM5 User1 TEST N 5/7/2006 4:17:40 PM6 User2 TEST N 5/7/2006 4:19:10 PM7 User1 TEST N 5/8/2006 10:25:28 AM8 User2 TEST N 5/8/2006 4:38:55 PM9 User1 TEST N 5/8/2006 4:41:30 PM10 User2 TEST N 5/10/2006 3:52:58 PM11 User1 TEST N 5/10/2006 3:53:20 PM12 User2 TEST N 5/10/2006 3:53:32 PM13 User1 TEST N 5/10/2006 3:53:45 PM14 User3 TEST N 5/11/2006 11:32:00 AM15 User1 TEST N 5/11/2006 11:35:00 AM16 User2 TEST N 5/11/2006 11:40:00 AM17 User5 TEST N 5/11/2006 3:41:17 PM29 TEST TEST N 5/12/2006 1:36:04 PM30 TEST TEST N 5/12/2006 2:32:18 PM31 User5 Yep! N 5/12/2006 2:34:40 PM32 User6 Nope N 5/12/2006 2:35:04 PM33 TEST Nope N 5/12/2006 2:35:37 PMBTW, I should mention that this is SQL Server 2005 Express |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-12 : 10:18:06
|
Can you try this code ? Did it gives you the result you want ?declare @lastUpdate datetimeselect @lastUpdate = '5/12/2006 2:35:37 PM'select *from yourtablewhere ChatDate > @lastUpdate And what does FixHour do ? Just strip off the minutes ? KH |
 |
|
mAdMaLuDaWg
Starting Member
42 Posts |
Posted - 2006-05-12 : 10:26:15
|
Still no good khtan. Yeah, fixhour just converts the date into this format: "5/7/2006 3:42:00 PM". If you use XML auto with the query and just select the date, it converts it into a weird format so I had to reformat it but Peso's REPLACE(CONVERT(VARCHAR, ChatDate, 101) + LEFT(RIGHT(CONVERT(VARCHAR, ChatDate, 109), 14), 8) + ' ' + RIGHT(CONVERT(VARCHAR, ChatDate, 109), 2), ' ', ' ') ChDate does the same trick. |
 |
|
mAdMaLuDaWg
Starting Member
42 Posts |
Posted - 2006-05-13 : 10:40:02
|
I haven't figured out the problem yet and casting the comparison to varchar didn't seem to work to well either so I tried splitting up the date using datepart but that didn't work so this I just added one second to the ChatDate using dateAdd in the conditional clause and thats the best I could do until I figure out whats going on:DateAdd(s,1,ChatDate)>@lastUpdate |
 |
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2006-05-13 : 13:00:39
|
If you column is datetime, the values will be more precise than your sample output. ie:"2006-05-13 12:56:52.250" rather than 5/13/2006 12:56 PMI suspect you have dates that appear like 5/13/2006 12:56 PM when formattedbut are really greater than that: 2006-05-13 12:56:52.250Be One with the OptimizerTG |
 |
|
mAdMaLuDaWg
Starting Member
42 Posts |
Posted - 2006-05-13 : 13:13:11
|
Ahh yes, that seems like the only logical explanation TG. When I do a plain query, it just shows up to the seconds not milliseconds (I'm using Visual Studio 2005's IDE for querying and SQL Server 2005 Express). However, when I do a DatePart(ms,ChatDate) it does indeed give me milliseconds. So I'll just cull the milliseconds from my app's insert logic and I should be all clear...Thanks a billion TG! |
 |
|
|
|
|
|
|