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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Select rows from a where clause

Author  Topic 

jamie_pattison
Yak Posting Veteran

65 Posts

Posted - 2010-05-14 : 11:05:24
I have a table which has dates i.e.

2010-05-14 00:00:00.000
2010-05-15 00:00:00.000
2010-05-16 00:00:00.000

Im trying to write a query so if i was to select a date between a period it would return the number of days. So if i select 2010-05-14 to 2010-05-16 - it should return back 3 days. I wrote a query which doesnt do as im expecting and wonder if someone could assist here please?

DECLARE @From as Datetime
DECLARE @To as datetime

SET @From='2010-05-14 00:00:00.000'
SET @To='2010-05-16 00:00:00.000'

SELECT count(LiveTime)
FROM
DateTable
WHERE (@From <= @To)

Ive tried changing the last line around which makes no difference? Any help here would be great.

Thanks

malpashaa
Constraint Violating Yak Guru

264 Posts

Posted - 2010-05-14 : 11:16:03
Use

WHERE Livetime BETWEEN @From AND @To

Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-05-14 : 11:16:11
I'm guessing what you need is this
SELECT count(*)
FROM
DateTable
WHERE LiveTime between 20100514 and 20100516
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-05-14 : 11:17:06
Your where clause has no relation to a column in your table.
Can you give more specific informations?
Table structure, sample data and wanted output in relation to sample data?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

jamie_pattison
Yak Posting Veteran

65 Posts

Posted - 2010-05-14 : 11:36:15
quote:
Use


WHERE Livetime BETWEEN @From AND @To


Ill give this a shot.

quote:

I'm guessing what you need is this

SELECT count(*)
FROM
DateTable
WHERE LiveTime between 20100514 and 20100516


No i need to use the variables. Thanks for trying

quote:
Originally posted by webfred

Your where clause has no relation to a column in your table.
Can you give more specific informations?
Table structure, sample data and wanted output in relation to sample data?


No, you're never too old to Yak'n'Roll if you're too young to die.



This maybe on the correct lines. Its a single table, with the data in the exact format as my original post. Im expecting to return the number of rows within the selected variables (From and To).

Thanks everyone
Go to Top of Page
   

- Advertisement -