Tuesday, February 14, 2012

Date an Time conversion problem

Well here is the problem iam trying to evaluate a expresion and return a string but when i run my code it always return where my expresion is false where it should return true. here is my code.

BEGIN
DECLARE @.datefin_flag char(13), @.strip datetime
select @.strip = getdate()
--select convert(char(10),@.strip,120)
select @.datefin_flag = dateend FROM mattstest WHERE convert(char(10),datebegin,120) <= convert(char(10),@.strip,120) and convert(char(10),dateend,120) >= convert(char(10),@.strip,120)
--select @.datefin_flag
--UPDATE dateflagevent SET flagevent = getdate() FROM dateflagevent
IF (@.datefin_flag = @.strip)
BEGIN
print 'Run'
END
ELSE
print 'You cant run this'
END

Now here is the my table data:

datebegin datefin
-------- --------
2004-12-25 00:00:00.000 2005-01-25 00:00:00.000
2004-11-25 00:00:00.000 2004-12-24 00:00:00.000
2005-02-25 00:00:00.000 2005-03-25 00:00:00.000

I think that the problem is the date and time they are the same but not in the right format its like saying 2004-01-25 is equal to janv 25 2005 how do i correct this.your problem might be you are doing a >= comparison on character data due to your convert functions|||Can you show me in code.|||I am going to be brief because I am about to go home and I am tired of goofing off at work on this forum today but I think your problem is that you want to compare two dates but your are converting your date values to character strings.

convert(char(10),datebegin,120) <= convert(char(10),@.strip,120) and convert(char(10),dateend,120) >= convert(char(10),@.strip,120)

I believe when you do this (I have not double checked so flame me all you want smart guys) you are actually comparing the unicode value of the 2 strings you just created.|||Correct, except that format 120 is "yyyy-mm-dd", so that even as character data it should sort correctly. (I'll need to double-check that in the morning...)

I think your problem is that you are assigning the value getdate() to @.strip, and getdate() includes both a date AND a time value. So later when you compare it to the value selected from your table (dateend? datefin?), it is only going to match if run at precisely midnight.

Try this assignment:
select @.strip = CONVERT(CHAR(10), getdate(), 120)|||I think it realy is the time associated with the date but how do i strip it out or how do i evaluate my expression ?|||select @.strip = CONVERT(CHAR(10), getdate(), 120).

select @.strip = CONVERT(CHAR(10), getdate(), 120).

select @.strip = CONVERT(CHAR(10), getdate(), 120).

select @.strip = CONVERT(CHAR(10), getdate(), 120).

select @.strip = CONVERT(CHAR(10), getdate(), 120).

select @.strip = CONVERT(CHAR(10), getdate(), 120).

select @.strip = CONVERT(CHAR(10), getdate(), 120).

select @.strip = CONVERT(CHAR(10), getdate(), 120).

select @.strip = CONVERT(CHAR(10), getdate(), 120).

select @.strip = CONVERT(CHAR(10), getdate(), 120).

select @.strip = CONVERT(CHAR(10), getdate(), 120).

select @.strip = CONVERT(CHAR(10), getdate(), 120).

select @.strip = CONVERT(CHAR(10), getdate(), 120).

select @.strip = CONVERT(CHAR(10), getdate(), 120).

select @.strip = CONVERT(CHAR(10), getdate(), 120).

select @.strip = CONVERT(CHAR(10), getdate(), 120).|||instead of converting datebegin and dateend to strings (which will mean that any indexes on those columns will be ignored, you get a table scan), why not do the query based on datetime comparisons, based on the current date which you can obtain by stripping the time portion out of getdate() while leaving the result as a datetime value

specifically,
-- strip time component from today, but leave as datetime
select @.strip = dateadd(d,datediff(d,0,getdate()),0)

-- search based on today's date
select @.datefin_flag = dateend
from mattstest
where datebegin <= @.strip
and dateend >= @.strip|||thanx dude worked perfect|||select @.strip = dateadd(d,datediff(d,0,getdate()),0).

select @.strip = dateadd(d,datediff(d,0,getdate()),0).

select @.strip = dateadd(d,datediff(d,0,getdate()),0).

select @.strip = dateadd(d,datediff(d,0,getdate()),0).

select @.strip = dateadd(d,datediff(d,0,getdate()),0).

select @.strip = dateadd(d,datediff(d,0,getdate()),0).
.
.
.

No comments:

Post a Comment