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)
 how to select date only

Author  Topic 

jrahma
Starting Member

16 Posts

Posted - 2008-03-06 : 09:49:52
I am developing C# application and storing data in SQL 2005.

I have a datetime field and i want to select all records which has a specefic date regardless of time..

for example:

21/7/2008 12:40:32
21/7/2008 23:45:00

i want to select both because both has the same date

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-03-06 : 09:51:04

select dateadd(day,datediff(day,0,datecol),0) from your_table

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

tfountain
Constraint Violating Yak Guru

491 Posts

Posted - 2008-03-06 : 09:54:33
Well, I was gonna post but it looks like madhivanan beat me to the punch. I use that exact example he posted that we just put it into a function (and use it wisely of course).
Go to Top of Page

jrahma
Starting Member

16 Posts

Posted - 2008-03-06 : 09:57:50
but that will only select a day, what about the month and year?

i want to select all 21/7/2008 (passed from my c#)
Go to Top of Page

elancaster
A very urgent SQL Yakette

1208 Posts

Posted - 2008-03-06 : 09:59:25
what do you mean? you want year only and month only aswell?

Em
Go to Top of Page

tfountain
Constraint Violating Yak Guru

491 Posts

Posted - 2008-03-06 : 10:00:43
Did you attempt to run that example? That simply uses date math to remove the time portion of the datetime/smalldatetime field and leave you with only the date portion.
Go to Top of Page

Jeff Moden
Aged Yak Warrior

652 Posts

Posted - 2008-03-06 : 10:08:48
Still, it's the wrong way to do things in the WHERE clause because it will not allow an index seek to work. The correct way to do this is as follows...

 SELECT whatever
FROM yourtable
WHERE somedatecol >= '20080721'
AND somedatecol < '20080722'


--Jeff Moden
Go to Top of Page

tfountain
Constraint Violating Yak Guru

491 Posts

Posted - 2008-03-06 : 10:26:33
In that case, using the BETWEEN keyword will be of value as well. But honestly, if the underlying data is small and you can guarantee it will remain that way then it's a wash IMO.
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2008-03-06 : 10:33:53
quote:
Originally posted by tfountain

In that case, using the BETWEEN keyword will be of value as well. But honestly, if the underlying data is small and you can guarantee it will remain that way then it's a wash IMO.



I don't see any reason to use something that is bad in a lot of cases when the solution Jeff posted is the best way to do things.




CODO ERGO SUM
Go to Top of Page

tfountain
Constraint Violating Yak Guru

491 Posts

Posted - 2008-03-06 : 12:57:12
quote:
Originally posted by Michael Valentine Jones

quote:
Originally posted by tfountain

In that case, using the BETWEEN keyword will be of value as well. But honestly, if the underlying data is small and you can guarantee it will remain that way then it's a wash IMO.



I don't see any reason to use something that is bad in a lot of cases when the solution Jeff posted is the best way to do things.




CODO ERGO SUM



I didn't say Jeff was wrong either. I was simply suggesting that using the BETWEEN operator would work as well - the assumption was with Jeff's example and that was not implicitly stated. Instead of doing the comparision using the equality operators. That's all.

My second statement holds true and it's not 'bad' by any means. How it's used can be bad. There's a difference. I do realize the prior statements are true as well in terms of not being able to use an index in a WHERE clause - but again, review my prior posts regarding using it wisely. The point here being what's worse - a table scan on a small table when querying normally or not using an index on a small table when using some sort of function around the left side of the condition. It's a wash unless you really care about the extremely small marginal gain there might be.
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2008-03-06 : 14:00:24
quote:
Originally posted by tfountain

quote:
Originally posted by Michael Valentine Jones

quote:
Originally posted by tfountain

In that case, using the BETWEEN keyword will be of value as well. But honestly, if the underlying data is small and you can guarantee it will remain that way then it's a wash IMO.



I don't see any reason to use something that is bad in a lot of cases when the solution Jeff posted is the best way to do things.




CODO ERGO SUM



I didn't say Jeff was wrong either. I was simply suggesting that using the BETWEEN operator would work as well - the assumption was with Jeff's example and that was not implicitly stated. Instead of doing the comparision using the equality operators. That's all.

My second statement holds true and it's not 'bad' by any means. How it's used can be bad. There's a difference. I do realize the prior statements are true as well in terms of not being able to use an index in a WHERE clause - but again, review my prior posts regarding using it wisely. The point here being what's worse - a table scan on a small table when querying normally or not using an index on a small table when using some sort of function around the left side of the condition. It's a wash unless you really care about the extremely small marginal gain there might be.



I still stand by my statement that it is bad, because there is no case where it has any advantage over the solution Jeff suggested, while there are plenty of cases where Jeff's solution has significant advantages over your suggestion.

You might claim that one is shorter to code, but I don't see much advantage there if you look at the actual length of the code:

where dateadd(dd,datediff(dd,0,datecol),0) = '20080721'
where datediff(dd,'20080721',datecol) = 0
where datecol between '20080721' and '20080721 23:59:59.997'

where datecol >= '20080721' and datecol < '20080722'







CODO ERGO SUM
Go to Top of Page

tfountain
Constraint Violating Yak Guru

491 Posts

Posted - 2008-03-06 : 15:03:45
Honestly, I'm not sure of what your argument is. For query purposes, I have this code encapsulated in a function where all I need to do is something like so:

WHERE <db>.dbo.<function>(date) = '1/1/1900'

I'll admit I use this function mainly for removing time off of variables for other uses and but on occasion I will use it in queries. If you know what you're doing and don't use this in the wrong situations, there's no argument - period.

I'll concede simply because arguing over something as trival as this is a waste time for both of us.
Go to Top of Page

cbeganesh
Posting Yak Master

105 Posts

Posted - 2008-03-06 : 15:51:07
select * from ttt
where joindate >= '01/01/2008'+' 00:00:00'
and joindate <= '01/01/2008'+' 23:59:59'
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2008-03-06 : 16:52:43
quote:
Originally posted by tfountain

Honestly, I'm not sure of what your argument is. For query purposes, I have this code encapsulated in a function where all I need to do is something like so:

WHERE <db>.dbo.<function>(date) = '1/1/1900'

I'll admit I use this function mainly for removing time off of variables for other uses and but on occasion I will use it in queries. If you know what you're doing and don't use this in the wrong situations, there's no argument - period.

I'll concede simply because arguing over something as trival as this is a waste time for both of us.



This code has significant disadvantages:
WHERE dbo.UDF_Date_function(date) = '1/1/1900'


1. It cannot use an index, so you will likely get a table scan even if the table has a usable index.
2. Using a UDF on a data column results in an internal cursor to process each row with further performance impact.

In short, there is no advantage to what you are suggesting, and significant disadvantages. It seems that the best argument you can give amounts to "In some cases, it's not too much worse."











CODO ERGO SUM
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2008-03-06 : 16:52:43
quote:
Originally posted by tfountain

Honestly, I'm not sure of what your argument is. For query purposes, I have this code encapsulated in a function where all I need to do is something like so:

WHERE <db>.dbo.<function>(date) = '1/1/1900'

I'll admit I use this function mainly for removing time off of variables for other uses and but on occasion I will use it in queries. If you know what you're doing and don't use this in the wrong situations, there's no argument - period.

I'll concede simply because arguing over something as trival as this is a waste time for both of us.



This code has significant disadvantages:
WHERE dbo.UDF_Date_function(date) = '1/1/1900'


1. It cannot use an index, so you will likely get a table scan even if the table has a usable index.
2. Using a UDF on a data column results in an internal cursor to process each row with further performance impact.

In short, there is no advantage to what you are suggesting, and significant disadvantages. It seems that the best argument you can give amounts to "In some cases, it's not too much worse."











CODO ERGO SUM
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-03-07 : 03:05:05
quote:
Originally posted by cbeganesh

select * from ttt
where joindate >= '01/01/2008'+' 00:00:00'
and joindate <= '01/01/2008'+' 23:59:59'


Can you please read the previous answers fully?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -