|
-
Dec 13th, 2010, 02:14 PM
#1
[RESOLVED] Select between 2 "dates"
Hello guys,
I have a DB table where the date is represented in 6 separate integer fields/columns (year, month, day, hour, minute, second). Don't ask me why and please do not tell me to change the column types... It's something I have no control over. Now my question is, how do I create a query to select all records between 2 dates in this table?
I've tried this but it doesn't return the right records.
Code:
"Select * from tablename where (yr >= @year1 and month >= @month1 and day >= @day1)
and (yr <= @year2 and month <= @month2 and day <= @day2)
Any help is greatly appreciated.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Dec 13th, 2010, 02:39 PM
#2
Re: Select between 2 "dates"
What about converting to string (varchar) and then Convert to DateTime \
CONVERT(DATETIME(CONVERT(VARCHAR(2),month) + '/' + Convert(VARCHAR(2),day) + '/' + Convert(Varchar(4),Year) )
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Dec 13th, 2010, 08:38 PM
#3
Re: Select between 2 "dates"
sql Code:
SELECT * FROM TableName WHERE (yr > @year1 OR (yr = @year1 AND (month > @month1 OR (month = @month1 AND day >= @day1) AND (yr < @year2 OR (yr = @year2 AND (month < @month2 OR (month = @month2 AND day <= @day2)
-
Dec 14th, 2010, 03:50 PM
#4
Re: [RESOLVED] Select between 2 "dates"
@JMC: Thanks, JMC. That did the trick.
@Gary: your suggestion probably works in most cases but the problem is the DB I'm working with (there are some records that that the "day" field has no data and thus it default to zero - so problems arise when it tries to convert "12/00/2010" to a date Using JMC's query, I was able to pull these records and fix them as needed. Thanks anyway.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|