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 |
|
Eclipse17
Starting Member
1 Post |
Posted - 2007-10-17 : 20:50:21
|
| Hi,I have a table that looks similar to this. How would i write a query that will show me the price of the product on mondays? ThanksDate Price16/10/2007 16.7515/10/2007 16.2512/10/2007 15.711/10/2007 15.6410/10/2007 15.1709/10/2007 14.905/10/2007 14.7504/10/2007 14.5203/10/2007 14.8402/10/2007 14.901/10/2007 14.8428/09/2007 15.3 |
|
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-10-17 : 21:35:50
|
| perhaps there's a better way but check out the DATENAME function in books online, meanwhile.Dinakar Nethi************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-10-17 : 23:55:32
|
| DATEPART(Weekday, MyDate) might be faster (than DATENAME - i.e. its probably only doing numeric work, no string conversion).Kristen |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-18 : 01:31:37
|
Datepart function seems to be relying on SET DATEFIRST setting whereas datename do not. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-10-18 : 02:01:16
|
| Just do Modulo 7 then maybe? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-18 : 02:33:12
|
[code]DECLARE @Sample TABLE (dt DATETIME, Price MONEY)SET DATEFORMAT DMYINSERT @SampleSELECT '16/10/2007', 16.75 UNION ALLSELECT '15/10/2007', 16.25 UNION ALLSELECT '12/10/2007', 15.7 UNION ALLSELECT '11/10/2007', 15.64 UNION ALLSELECT '10/10/2007', 15.17 UNION ALLSELECT '09/10/2007', 14.9 UNION ALLSELECT '05/10/2007', 14.75 UNION ALLSELECT '04/10/2007', 14.52 UNION ALLSELECT '03/10/2007', 14.84 UNION ALLSELECT '02/10/2007', 14.9 UNION ALLSELECT '01/10/2007', 14.84 UNION ALLSELECT '28/09/2007', 15.3-- DatepartSELECT dt, PriceFROM @SampleWHERE DATEPART(WEEKDAY, dt) = 2-- DatenameSELECT dt, PriceFROM @SampleWHERE DATENAME(WEEKDAY, dt) = 'Monday'-- Modulo 7SELECT dt, PriceFROM @SampleWHERE DATEDIFF(DAY, 0, dt) % 7 = 0[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-10-18 : 03:10:59
|
| WouldWHERE CONVERT(int, dt) % 7 = 0be "safe" ? And any quicker than the DATEDIFF?Kristen |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-10-18 : 05:22:39
|
quote: Originally posted by Kristen WouldWHERE CONVERT(int, dt) % 7 = 0be "safe" ? And any quicker than the DATEDIFF?Kristen
select convert(int, getdate()),datediff(day,0,getdate())MadhivananFailing to plan is Planning to fail |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-18 : 05:26:43
|
I think CONVERT approach will have problem with AM/PM. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|
|
|
|
|