locked
To insert only date and time RRS feed

  • Question

  • User-132829 posted

    Dear Friends,

    I am in software development. I want to insert two fields through textboxes.

    I want to insert only date(like 3/22/2010) in one text box and to insert time(10:30(am) or 22:20(pm) in another text box into sql server.

    How to do this?

    Thanks in advance!

    Thursday, March 22, 2012 5:58 AM

Answers

  • User-2082239438 posted

    You can take different variable for both date and time and then merge it both. Refer below code.

    declare @time as time
    declare
    @dateTime as datetime

    set @dateTime = '2011-06-25 08:40:00.000'
    set @time = '02:40:00.000'

    set @dateTime = @dateTime + @time
    print @dateTime

    Above one is not proper.

    In Date column, you must have to pass date only..check below

    declare @time as time
    declare @dateTime as datetime
    
    set @dateTime = '2011-06-25'
    set @time = '02:40:00.000'
    
    set @dateTime = @dateTime + @time
    
    SELECT @dateTime
    
    --output
    --(No column name)
    --2011-06-25 02:40:00.000
    
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 22, 2012 6:37 AM

All replies

  • User-2082239438 posted

    Dear Friends,

    I am in software development. I want to insert two fields through textboxes.

    I want to insert only date(like 3/22/2010) in one text box and to insert time(10:30(am) or 22:20(pm) in another text box into sql server.

    How to do this?

    Thanks in advance!

    if you are using sql server 2008 and above, than you will need to use DATE & TIME Column to store value for both textboxes.

    Otherwise you will need to use DATETIME column and whenever you will want to show data, than you will need to convert datetime column's value to date & time value.

    Thursday, March 22, 2012 6:01 AM
  • User-132829 posted

    In database table i created two fields as 

    Date, Time

    I want to insert date in first text box and time in another text box...

    so in my .cs page i did as follows.

    DateTime date, time;

    date = DateTime.Parse(txtDate.Text);
    time = DateTime.Parse(txtTime.Text);

    connectionopen...

    cmd.CommandText =Insert into database_table values("+date+","+time+");

    cmd.ExecuteNonQuery();

    connectionclosed..

    i am here declared as DateTime datatype for date and time...

    so that two having same format(3/22/2010 10:20:22: AM) but i want to insert 3/22/2012 in date textbox and 10:20:22 AM in another text box....

    This is what i required exactly...

    How friends?

    Thursday, March 22, 2012 6:11 AM
  • User-2082239438 posted
                //specify column in insert 
                //use parameter to insert records
                cmd.CommandText = "Insert into database_table(Date,Time) values(@date,@time)";
    
                //parameter
                cmd.Parameters.AddWithValue("@date", textbox1.Text);
                cmd.Parameters.AddWithValue("@time", textbox2.Text);
                
                //execute query
                cmd.ExecuteNonQuery();
    Thursday, March 22, 2012 6:21 AM
  • User-1011137159 posted

    You can take different variable for both date and time and then merge it both. Refer below code.

    declare @time as time
    declare @dateTime as datetime
    
    set @dateTime = '2011-06-25 08:40:00.000'
    set @time = '02:40:00.000'
    
    set @dateTime = @dateTime + @time
    print @dateTime
    Thursday, March 22, 2012 6:33 AM
  • User-2082239438 posted

    You can take different variable for both date and time and then merge it both. Refer below code.

    declare @time as time
    declare
    @dateTime as datetime

    set @dateTime = '2011-06-25 08:40:00.000'
    set @time = '02:40:00.000'

    set @dateTime = @dateTime + @time
    print @dateTime

    Above one is not proper.

    In Date column, you must have to pass date only..check below

    declare @time as time
    declare @dateTime as datetime
    
    set @dateTime = '2011-06-25'
    set @time = '02:40:00.000'
    
    set @dateTime = @dateTime + @time
    
    SELECT @dateTime
    
    --output
    --(No column name)
    --2011-06-25 02:40:00.000
    
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 22, 2012 6:37 AM
  • User-132829 posted

    Thanks friends....

    Thursday, March 22, 2012 7:04 AM