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
 General SQL Server Forums
 New to SQL Server Programming
 Modify query to add time to datetime

Author  Topic 

Apples
Posting Yak Master

146 Posts

Posted - 2010-08-25 : 14:49:11
I have a page where a user can create an event. Here is my table:

-------------------------------
tblEvents
-------------------------------
EventID | Name | DateStart
-------------------------------


This is my query for inserting an event:

INSERT INTO tblEvents
(
Name,
DateStart
)
VALUES
(
@Name,
@DateStart
)


On the page, the user enters the Name in a textbox and enters a DateStart in a textbox (which is in the format 01/01/2010), and the values are passed in as parameters to the query.

However, I need to modify my page so that I can set the time for the event as well. Currently, DateStart has the time set to midnight, but I need to have the user set that. There will be 3 dropdowns: one for the hour, one for the minute, and one for AM/PM.

How can I change my query to accept the time as well? Is there anything like:

INSERT INTO tblEvents
(
Name,
DateStart
)
VALUES
(
@Name,
DATE(@DateStart,@Hour,@Min,@AMPM)
)

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-08-25 : 15:01:09
From your application, pull everything together into a valid date/time string and then pass that to SQL. Do not send it in parts.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Apples
Posting Yak Master

146 Posts

Posted - 2010-08-25 : 16:25:12
Thank you, I will try that.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-08-27 : 09:10:48
Also send dates in unambiguous format
http://beyondrelational.com/blogs/madhivanan/archive/2010/06/03/understanding-datetime-column-part-ii.aspx

Madhivanan

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

- Advertisement -