all groups > sql server programming > october 2007 >
You're in the

sql server programming

group:

A Select query to select records between 2 date and time values


A Select query to select records between 2 date and time values Craig
10/22/2007 11:12:57 PM
sql server programming: hey everyone.

I need a query that will select records from a table based on the date
and times.

Basically, it's to do with different employee shifts, especially the
ones that cross midnight.

How would I construct a select statement to get all shift details that
fall between the periods 23:00 10-Oct-2007 and 07:00 11-Oct-2007?

This has been giving me some grief.

Thanks
Re: A Select query to select records between 2 date and time values Craig
10/22/2007 11:53:18 PM
Hi Uri.

Thanks for that. I'm sorry but I should have mentioned that the Date
and Time fields are seperate fields. E.g., The 'ShiftDetails' table
has a 'ShiftStartDate' field and a seperate 'ShiftStartTime' field.
This is where I think it complicates things.



[quoted text, click to view]
Re: A Select query to select records between 2 date and time values Uri Dimant
10/23/2007 12:00:00 AM
Craig

SELECT * FROM tbl WHERE yourcol >= '20071010 23:00' AND yourcol
<='20071011 07:00'




[quoted text, click to view]

Re: A Select query to select records between 2 date and time values Paddy
10/23/2007 12:07:51 AM
Assumming your columns are datetime format,a

Try

SELECT * FROM tbl WHERE yourDatecol + YourTimeCol >= '20071010
23:00' AND yourcol
<='20071011 07:00'


Paddy


Re: A Select query to select records between 2 date and time values Craig
10/23/2007 12:15:13 AM
Hi Paddy.

My columns are datetime format but I get this error when I try to
select, "You may not add a date type and a time type".

Cheers

[quoted text, click to view]

Re: A Select query to select records between 2 date and time values --CELKO--
10/23/2007 6:10:52 AM
[quoted text, click to view]

Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. If you know how, follow ISO-11179 data element naming
conventions and formatting rules. Sample data is also a good idea,
along with clear specifications. It is very hard to debug code when
you do not let us see it.

This is a common design flaw you need to correct. The root cause of
this attribute splitting is confusing the concept of a field and a
column. Make them into real DATETIME columns which model complete
facts instead of parts of a fact.

Do it in the DDL instead of kludging it in the DML. Unless you do
that, you will never have any data integrity and your DML will run
like glue with the extra overhead of correcting the design flaw on the
fly.


Re: A Select query to select records between 2 date and time values Paddy
10/23/2007 7:45:07 AM
Has someone created bespoke data types on your system?

Standard sql only has datetime or smalldatetime data types.

Paddy
AddThis Social Bookmark Button