473,466 Members | 1,381 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

parameterized datetime query

Hi,

I want to know how can i query from sql server date field without the time?
i mean if i have the following dates:

8/22/2007 12:00:00 AM
8/22/2007 12:23:00 AM
8/22/2007 04:15:00 PM

and i want to SELECT all the 8/22/2007 dates..
how can i creates such stored procedure and make the query in C#?
Aug 25 '07 #1
3 4046
Sql-specific questions like this should be posted to:

microsoft.public.sqlserver.programming

-HTH

"Jassim Rahma" <jr****@hotmail.comwrote in message
news:61**********************************@microsof t.com...
Hi,

I want to know how can i query from sql server date field without the
time?
i mean if i have the following dates:

8/22/2007 12:00:00 AM
8/22/2007 12:23:00 AM
8/22/2007 04:15:00 PM

and i want to SELECT all the 8/22/2007 dates..
how can i creates such stored procedure and make the query in C#?


Aug 25 '07 #2
"Jassim Rahma" <jr****@hotmail.comwrote in message
news:61**********************************@microsof t.com...
8/22/2007 12:00:00 AM
8/22/2007 12:23:00 AM
8/22/2007 04:15:00 PM

and i want to SELECT all the 8/22/2007 dates..

how can i creates such stored procedure and make the query in C#?

One way to do it (I don't assert that it is optimal) is to select the
date as
convert(datetime,convert(varchar, @TheDate, 101), 101)
This converts the date to a varchar in the format mm/dd/yyyy (dropping
the time part), and back to datetime.

However, a Select done on that expression will not be able to use any
indexes that Sql Server might have on that column. It is therefore better,
although a little more complex, to Select ... Where column Between
@startdate and @enddate, where you would build startdate and enddate in your
C# code to be the desired date at 00:00:00 and the desired date at 23:59:59,
respectively. Your code would be similar to the following:

DateTime dateToSearch = DateTime.Now; //Test value
DateTime dateWithoutTime = dateToSearch.Date;
DateTime startDate = dateWithoutTime;
DateTime endDate = dateWithoutTime.AddSeconds(24*3600-1);
string query = "Select * from TheTable Where TheColumn Between @startDate
And @endDate";
SqlCommand cmd = new SqlCommand(query, connection);
cmd.Parameters.AddWithValue("@startDate", startDate);
cmd.Parameters.AddWithValue("@endDate", endDate);
SqlDataReader rdr = cmd.ExecuteReader(); //For instance

Aug 25 '07 #3
AddSeconds(24*3600-1);
....
Between @startDate And @endDate
Just a very minor addition to a good and complete answer; a much
easier option (safer too, if you need that last second) is to just
AddDays(1), and use "(TheColumn >= @startDate AND TheColumn <
@endDate)". Can also be done at the SQL end by DATEADD.
convert(datetime,convert(varchar, @TheDate, 101), 101)
To strip the time, a more efficient approach (which OK, uses a but
more of the underlying implementation details) is to simply cast to
int and back - or cast to float and take the floor(); the numeric
representation of a datetime is as the fractional number of days into
the epoch, hence midnight is {some integer}.0, midday is {some
integer}.5, etc.

I'll shut up now, as we left C#-ville some time ago ;-p

Marc

Aug 25 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: gary b | last post by:
Hello When I use a PreparedStatement (in jdbc) with the following query: SELECT store_groups_id FROM store_groups WHERE store_groups_id IS NOT NULL AND type = ? ORDER BY group_name
8
by: deko | last post by:
I'm trying to open a Recordset based on a parameterized query. I'm kind of new to parameterized queries, so I'm sure I'm missing something simple. Set qdfs = db.QueryDefs Set qdf =...
2
by: deko | last post by:
Is it possible to build a parameterized query from another parameterized query? I've tried two variations of this and can't seem to get it to work (using DAO). Any suggestions welcome! I...
11
by: anony | last post by:
Hello, I can't figure out why my parameterized query from an ASP.NET page is dropping "special" characters such as accented quotes & apostrophes, the registered trademark symbol, etc. These...
2
by: JSheble | last post by:
After building a parameterized ADO query, is there a method or a statement where you could see the actual query, with the parameterized values included?? -- Using Opera's revolutionary e-mail...
8
by: Roland Hall | last post by:
In Access you use "*" + + "*", + can be replaced with & Calling a parameterized query in Access requires % be used in place of *, however, all that I have read show dynamic SQL passed to Access: ...
1
by: TF | last post by:
This group came through for me last time so here we go again. My page shows paint colors, brand name, product code, etc in a gridview with the background matching the paint color. Several links on...
4
by: =?Utf-8?B?Sm9uIEphY29icw==?= | last post by:
For MS SQL Server... I am used to declaring local variables in my SQL queries... Declare @MyInt int, @MyChar varchar(33) Parameters were idenfitied with a colon... Where ModDate :MyDate But,...
3
by: xlar54 | last post by:
Is there a way to see the exact SQL being generated from a parameterized query? I am using this technique but am getting some strange SQL errors during execution and I would like to see the final...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.