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 |
|
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.0002010-05-15 00:00:00.0002010-05-16 00:00:00.000Im 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 DatetimeDECLARE @To as datetimeSET @From='2010-05-14 00:00:00.000'SET @To='2010-05-16 00:00:00.000'SELECT count(LiveTime)FROMDateTableWHERE (@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
|
UseWHERE Livetime BETWEEN @From AND @To |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-05-14 : 11:16:11
|
I'm guessing what you need is thisSELECT count(*)FROMDateTableWHERE LiveTime between 20100514 and 20100516 |
 |
|
|
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. |
 |
|
|
jamie_pattison
Yak Posting Veteran
65 Posts |
Posted - 2010-05-14 : 11:36:15
|
quote: UseWHERE Livetime BETWEEN @From AND @To
Ill give this a shot.quote: I'm guessing what you need is thisSELECT count(*)FROMDateTableWHERE LiveTime between 20100514 and 20100516
No i need to use the variables. Thanks for tryingquote: 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 |
 |
|
|
|
|
|