SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Views -between operator in where clause


Views -between operator in where clause

Author
Message
var05
var05
SSC-Enthusiastic
SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)

Group: General Forum Members
Points: 183 Visits: 437
How to specify a where clause in a view with between operator???

I have a view which shows empname and sal data and week

Results displayed by the view are

select empname,sal,week
from view test

EmpName Sal Week
A 100 07/02/2012
A 200 07/02/2011
B 500 07/02/2012
B 1000 07/02/2011
C 200 07/09/2012
C 300 07/23/2011
D 100 07/12/2010


Query 1

select empname,sal,week
from view test
where week='07/02/2012'

Results Displayed:
A 100 07/02/2012
B 500 07/02/2012

Query 2:
select empname,sal,week
from view test
where week='07/09/2012'
Result
C 200 07/09/2012

Query 3 :

select empname,sal,week
from view test
where week between '07/02/2012' and '07/16/2012'

the results to be displayed are:

A 100 07/02/2012
B 500 07/02/2012
C 200 07/09/2012


But it displays all years data for the month 07 between dates 02 and 16..

A 100 07/02/2012
A 200 07/02/2011
B 500 07/02/2012
B 1000 07/02/2011
C 200 07/09/2012
D 100 07/12/2010

any help on this?
Gazareth
Gazareth
SSCarpal Tunnel
SSCarpal Tunnel (4K reputation)SSCarpal Tunnel (4K reputation)SSCarpal Tunnel (4K reputation)SSCarpal Tunnel (4K reputation)SSCarpal Tunnel (4K reputation)SSCarpal Tunnel (4K reputation)SSCarpal Tunnel (4K reputation)SSCarpal Tunnel (4K reputation)

Group: General Forum Members
Points: 4017 Visits: 5828
Hi, what's the datatype of the Week field?
Thanks
GilaMonster
GilaMonster
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87531 Visits: 45272
var05 (12/5/2012)
any help on this?


Yeah, don't store dates as varchars, store them as datetime data types and then the comparisons will be date-based, not string based.

I strongly recommend fixing the data types. Any other form of workaround will be slow, painful or both.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


var05
var05
SSC-Enthusiastic
SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)SSC-Enthusiastic (183 reputation)

Group: General Forum Members
Points: 183 Visits: 437
Thanks for ur reply..

Week is a calcualted feild from date and I had changed the string to date and it works fine. Thanks!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search