all groups > sql server programming > august 2007 >
You're in the

sql server programming

group:

how to query date in stored procedure?


Re: how to query date in stored procedure? Shiju Samuel
8/25/2007 10:02:26 AM
sql server programming:
SELECT * from table1 where datecolumn >= '8/22/2007' and <
'8/23/2007'

Thanks
Shiju Samuel

[quoted text, click to view]

Re: how to query date in stored procedure? Shiju Samuel
8/25/2007 11:57:26 AM
So What is the issue? Post your schema with sample data.

I guess you need to query something like this
Select * from table1 where @inputdate between startdate and enddate +
1

Thanks
Shiju Samuel


[quoted text, click to view]

Re: how to query date in stored procedure? Dan Guzman
8/25/2007 12:00:08 PM
[quoted text, click to view]

Try something like:

CREATE PROCEDURE dbo.MySearchProc
@DateParameter datetime
AS
SELECT MyData
FROM dbo.MyTable
WHERE
MyDateColumn >= @DateParameter AND
MyDateColumn < DATEADD(day, 1, @DateParameter)
GO

EXEC dbo.MySearchProc @DateParameter = '20070822'
GO

--
Hope this helps.

Dan Guzman
SQL Server MVP

[quoted text, click to view]
Re: how to query date in stored procedure? Dan Guzman
8/25/2007 3:40:22 PM
I'm not sure what you mean when you say it's not working from your C# code.
Perhaps the issue is that the specified DateTime value includes time. One
method to remove the time in your C# code is to construct a new DataTime
without the start time from the selected start date:

DateTime startDate = new DateTime(
dateTimePicker1.Value.Year,
dateTimePicker1.Value.Month,
dateTimePicker1.Value.Day);

DateTime endDate = null;
switch (cboRange.Text)
{
case "WEEK":
endDate = startDate.AddDays(7);
break;
case "MONTH":
endDate = startDate.AddMonths(1);
break;
default:
endDate = startDate.AddDays(0);
break;
}
sql_command.Parameters.Add("@endtime", SqlDbType.DateTime).Value =
endDate;


One method to remove times in the proc code is:

SET @starttime = DATEADD(day, 0, DATEDIFF(day, 0, @starttime))
SET @endtime = DATEADD(day, 0, DATEDIFF(day, 0, @endtime))

Also do not prefix stored procedure names with "sp_". That prefix is
reserved for system stored procedures and should not be used for user
objects. See the Books Online for more information.

If you still have problems, post your stored procedure.

--
Hope this helps.

Dan Guzman
SQL Server MVP

[quoted text, click to view]
how to query date in stored procedure? Jassim Rahma
8/25/2007 7:46:00 PM
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?

Re: how to query date in stored procedure? Jassim Rahma
8/25/2007 9:11:09 PM
it's working but not when i use this is C#. why?
sql_command.Parameters.Add("@endtime", SqlDbType.DateTime).Value =
dateSchedule.Value.AddDays(7);



[quoted text, click to view]
Re: how to query date in stored procedure? Jassim Rahma
8/25/2007 9:25:11 PM
well, just to explain more, i ave two fields, starttime and endtime and i
want to query dates within that range.

The starttime the a datetimepicker control in windows form and the endtime
is selected from a dropdownlist contaiins (one day, one week, one month) so
I am assiging the endtime value as .AddDays.

if one day:
sql_command.Parameters.Add("@endtime", SqlDbType.DateTime).Value =
dateSchedule.Value;

if one week:
sql_command.Parameters.Add("@endtime", SqlDbType.DateTime).Value =
dateSchedule.Value.AddDays(7);

if one month:
sql_command.Parameters.Add("@endtime", SqlDbType.DateTime).Value =
dateSchedule.Value.AddDays(31);


[quoted text, click to view]
Re: how to query date in stored procedure? Jassim Rahma
8/25/2007 10:05:30 PM
this is my full C# code followed by the database structure:

private void getScheduleList()
{
data_table = new DataTable();
// sql_connection = new SqlConnection("Data
Source=.\\SQLEXPRESS;initial catalog=shefa;integrated security=true");
sql_connection = new SqlConnection(public_var.sql_connection);

sql_command = new SqlCommand("sp_get_staff_schedule",
sql_connection);
sql_command.CommandType = CommandType.StoredProcedure;
sql_command.Parameters.Add("@id_number",
SqlDbType.VarChar).Value = cboDoctor.Text.Substring(0, 9);

sql_command.Parameters.Add("@starttime",
SqlDbType.DateTime).Value = dateSchedule.Value;

if (cboRange.Text == "DAY")
sql_command.Parameters.Add("@endtime",
SqlDbType.DateTime).Value = dateSchedule.Value;
else if (cboRange.Text == "WEEK")
sql_command.Parameters.Add("@endtime",
SqlDbType.DateTime).Value = dateSchedule.Value.AddDays(7);
else if (cboRange.Text == "MONTH")
sql_command.Parameters.Add("@endtime",
SqlDbType.DateTime).Value =
dateSchedule.Value.AddDays(31).ToShortDateString();
else
sql_command.Parameters.Add("@endtime",
SqlDbType.DateTime).Value = dateSchedule.Value;

data_adapter = new SqlDataAdapter(sql_command);
data_adapter.Fill(data_table);
dataSchedule.DataSource = data_table;

dataSchedule.Columns[0].HeaderText = "Type";
dataSchedule.Columns[0].DefaultCellStyle.Alignment =
DataGridViewContentAlignment.TopLeft;
dataSchedule.Columns[0].Width = 180;

dataSchedule.Columns[1].HeaderText = "Staff Name";
dataSchedule.Columns[1].DefaultCellStyle.Alignment =
DataGridViewContentAlignment.TopLeft;
dataSchedule.Columns[1].Width = 180;

dataSchedule.Columns[2].HeaderText = "From";
dataSchedule.Columns[2].DefaultCellStyle.Alignment =
DataGridViewContentAlignment.TopCenter;
dataSchedule.Columns[2].DefaultCellStyle.Format = "ddd dd MMM
yyyy HH:mm";
dataSchedule.Columns[2].Width = 130;

dataSchedule.Columns[3].HeaderText = "To";
dataSchedule.Columns[3].DefaultCellStyle.Alignment =
DataGridViewContentAlignment.TopCenter;
dataSchedule.Columns[3].DefaultCellStyle.Format = "ddd dd MMM
yyyy HH:mm";
dataSchedule.Columns[3].Width = 130;
}


DATABASE STRUCTURE:
[id] [int] IDENTITY(1,1) NOT NULL,
[id_number] [varchar](50) NULL,
[company_reg_number] [int] NULL,
[resourceid] [int] NULL,
[status] [int] NULL,
[subject] [varchar](50) NULL,
[description] [text] NULL,
[label] [int] NULL,
[starttime] [datetime] NULL,
[endtime] [datetime] NULL,
[schedule_type] [varchar](255) NULL,
[location] [varchar](50) NULL,
[allday] [bit] NULL,
[eventtype] [int] NULL,
[recurrenceinfo] [text] NULL,
[reminderinfo] [text] NULL,
[staff_file_no] [int] NULL,







[quoted text, click to view]
AddThis Social Bookmark Button