You must Sign In to post a response.
  • Category: SQL Server

    Which datatype use for date and time insert in table

    which datatype use for date and time in insert query in table.
    like
    create table anil(int id ,name varchar(50),date_of_birth)
  • #749345
    Please find answer below,

    CREATE TABLE anil
    (
    int ID NOT NULL,
    name VARCHAR(50) NULL,
    date_of_birth DATETIME NULL
    )

    go

    INSERT INTO anil
    VALUES (1,
    'anil',
    '2014-08-30 19:05:00')

    go

  • #749352
    Hi,
    You can use following data types:
    1. date
    2. datetime
    3. smalldatetime

    Thanks,
    Ram Prasad

  • #749355
    Hi
    Anil Try this Query Your Sql server


    CREATE TABLE [dbo].[Table_1](
    [Id] [int] NOT NULL,
    [name] [varchar](50) NULL,
    [date_of_birth] [datetime] NULL,
    CONSTRAINT [PK_Table_1] PRIMARY KEY CLUSTERED
    (
    [Id] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]


    also check my attached png file.

    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

    Delete Attachment

  • #749358
    Hi Anil,

    You can use either of the following-
    Datetime
    date

    And your create table query will be-

    create table anil(
    int id ,
    name varchar(50),
    date_of_birth datetime null
    )

    And now insert the value as per your need -like either using direct insert into the table or using stored procedure.
    Hope this will help you !!

    Thanks,
    Ashutosh Jha
    http://tricksroad.com

  • #749391
    Thanx all of you

  • #749395
    But only use only date not time so what am to do

  • #749396
    hi
    Anil

    try this Script working for me in sQL sever 2008 r2




    SET ANSI_NULLS ON
    GO

    SET QUOTED_IDENTIFIER ON
    GO

    SET ANSI_PADDING ON
    GO

    CREATE TABLE [dbo].[Table_3](
    [Id] [int] NOT NULL,
    [name] [varchar](50) NULL,
    [date_of_birth] [date] NULL,
    CONSTRAINT [PK_Table_1] PRIMARY KEY CLUSTERED
    (
    [Id] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]

    GO

    SET ANSI_PADDING OFF
    GO


    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #749397
    hi
    anil

    check this attachment image

    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

    Delete Attachment

  • #749626
    run this script in your sql server
    You should take DateTime column for birthdate.
    It provides ease in calculations and manioulations of date and time.

    create table DemoData
    (
    id int,
    name varchar(50),
    date_of_birth datetime null

    )

    insert into DemoData(id,name,date_of_birth) values (1,'Nirav Prabtani','06/07/1991')

    select * from DemoData

    Regards,
    Nirav Prabtani (Senior Web Developer)
    Email : niravjprabtani@gmail.com
    blog : niravprabtani.blogspot.in

    Delete Attachment

  • #749747
    Hi,

    My suggestion is better to use DateTime DataType to store date as well as Time in to your database.

    Hope this will help you...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/


  • Sign In to post your comments