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 2005 Forums
 Transact-SQL (2005)
 select between two dates

Author  Topic 

Saleh
Starting Member

6 Posts

Posted - 2009-01-06 : 11:49:43
hey

i need to select all the records between two dates ,, the data type is DATETIME !

so from what i learn if i need to select between two dates is should write this query

select *
from x
where date between '12/10/2008' and '12/12/2008'

the problem is this message will appear

Msg 242, Level 16, State 3, Line 1
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.


Thnx

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-06 : 12:06:32
[code]select *
from x
where date between '20081012' and '20081212'
[/code]
Go to Top of Page

Saleh
Starting Member

6 Posts

Posted - 2009-01-06 : 12:10:59
my datetime format is ddmmyy
Go to Top of Page

Saleh
Starting Member

6 Posts

Posted - 2009-01-06 : 12:18:05
am sorry what i mean it didn't work also

how can i select only a single day

select * from x where date= $$$ ? in date time format

and thanx
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-06 : 12:22:10
does your table field contain timepart also?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-06 : 12:28:59
quote:
Originally posted by Saleh

am sorry what i mean it didn't work also

how can i select only a single day

select * from x where date= $$$ ? in date time format

and thanx


why it didnt work? did it throw same error?
Go to Top of Page

Saleh
Starting Member

6 Posts

Posted - 2009-01-06 : 12:34:38
yeah my table has also a time

the format 12/01/2008 12:00:00 m


select *
from x
where x ='12/01/2008 12:00:00 m'

Msg 241, Level 16, State 1, Line 1
Conversion failed when converting datetime from character string.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-06 : 12:39:11
quote:
Originally posted by Saleh

yeah my table has also a time

the format 12/01/2008 12:00:00 m


select *
from x
where x ='12/01/2008 12:00:00 m'

Msg 241, Level 16, State 1, Line 1
Conversion failed when converting datetime from character string.




select *
from x
where x ='2008-01-12 12:00:00.000'
Go to Top of Page

Saleh
Starting Member

6 Posts

Posted - 2009-01-06 : 15:13:27
it works ,, but i ask one of my friends he say i can make it with " # "
not with " ' " , do you have any idea ?
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2009-01-06 : 15:18:55
# is used in Access like #01/01/2009#, in SQL Server we use '01/01/2009'

Jim
Go to Top of Page

Saleh
Starting Member

6 Posts

Posted - 2009-01-06 : 15:36:49
thnx's jimf and visakh16
Go to Top of Page
   

- Advertisement -