tags:

views:

54

answers:

4

Using SQL Server 2005

Table1

Date

20090501
20090502
20090503
20090504
20090505

...,

I want to find the day compare with date, then i want to skip the date where the day = sunday.

Expected Output

20090501
20090502
20090504
20090505

..,

So 20090503 is skipped, because it is Sunday.

How to make a query?

+3  A: 

Lots of ways; here's one;

SELECT column
FROM Table
WHERE DATENAME(dw, column) <> 'Sunday'
Stuart Ainsworth
nicely getting around the problem of which day is Sunday :-) Is this going to work on localized versions (e.g. German, French) of SQL Server, though??
marc_s
It will, if you supply the appropriate name :) My French is rusty, but I think it's Dimanche. Just as an aside, I normally store the datename as part of my calendar tables to make it indexable.
Stuart Ainsworth
He did say he didn't want Sunday.
Jeff O
A: 

Fully localised version (thanks to everyone for pointing that out):

SELECT *
FROM MyTable
WHERE DATEPART(weekday, MyColumn) <> 8 - @@DATEFIRST
Christian Hayter
For US English only. For British English (and other languages), Sunday == 7.
Chris J
Fixed, see edited answer.
Christian Hayter
+1  A: 

Okay, I have no idea what you mean by "date compare with date", but to skip sunday you can use

SELECT    Column
FROM      Table
WHERE     DatePart(weekday, Column) <> 7

You have to check what you have set for DATEFIRST though, because this is important for what DATAPART(weekday) returns for sunday. On an english SQL server, the standard is to return 7, so if you use an english server and haven't changed anything, than it should work.

Maximilian Mayerl
And that's "British English" and not "US English". For US English, Sunday == 1.
Chris J
A: 

Well, you can get it into a DateTime format, then once you do that, you could do something like this for a where clause.

WHERE DATEPART(dw, DateColumn) <> 7

This assumes that your SQL Server is configured for english by default.

Mitchel Sellers
And that's "British English" and not "US English". For US English, Sunday == 1.
Chris J
Chris - Interesting must go check setting on our servers...
Mitchel Sellers