locked
Help needed for datetime datetype RRS feed

  • Question

  •  

    Hi,

     

    I have declared a column datatype as smalldatetime. when I insert data it is storing date  and time.

     

    If I do select convert(char(20),columnA,110) this will give date value without time. I tried to use same convert function to update existing data from datetime to date but it is not updating.

     

     

    How to strip the time from the date values ?

    Wednesday, September 3, 2008 4:51 PM

Answers

  • It will not.  In SQL Server 2005 there is no such thing as a "Date datatype"; as you said, you are using a "smalldatetime" and time is inherently a part of the data.  The data may well be set according to what your update says; however, whenever you execute this update statement SQL Server internally sets the time to zero -- that is 00:00:00.000 and stores the data as both date and time with the time part being zero.  You ought to give a look at the write up for the DATETIME datatype in books online.

    Wednesday, September 3, 2008 6:33 PM

All replies

  • It sounds to me like you might be experiencing the expected behavior.  Can you tell us how you are viewing the results please?

    Wednesday, September 3, 2008 5:00 PM
  • If I do this

     

    select columnA, convert(varchar(10),UcolumnA,101)
     from TABLEA

     

    I will be getting below results
    7/23/2008 12:00:00.000 AM                     07/23/2008

     

     

     

    If I use this update statement , I am expecting that it will convert datetime to date as I got the results from above select statement but it is not updating .

     

    update TABLEA

    set UAT=substring(COLUMNA,1, 12)
    from TABLEA.

     

     

     

     

    Wednesday, September 3, 2008 5:26 PM
  • It will not.  In SQL Server 2005 there is no such thing as a "Date datatype"; as you said, you are using a "smalldatetime" and time is inherently a part of the data.  The data may well be set according to what your update says; however, whenever you execute this update statement SQL Server internally sets the time to zero -- that is 00:00:00.000 and stores the data as both date and time with the time part being zero.  You ought to give a look at the write up for the DATETIME datatype in books online.

    Wednesday, September 3, 2008 6:33 PM
  • use convert function

    try this

     

    update TABLEA

    set UAT= CONVERT(VARCHAR,COLUMNA,101)

    from TABLEA.

     

     

    Wednesday, September 3, 2008 9:21 PM
  • Kent's answer is the correct one on this post.  Unless you alter the table definition to a column type other than datetime, or smalldatetime, the data will contain the time portion as stored in the database.  If you don't want that, then you can store the data as a varchar(10), but then you lose the ability to compare values as dates should be compared.  They instead are going to be compared as varchar strings, which isn't ideal, or appropriate.

     

    Consider the following script:

     

    Code Snippet

    create table test

    (

    rowid int identity primary key,

    datetimefield datetime)

    insert into test select dateadd(dd, -1, getdate())

    insert into test select dateadd(dd, -2, getdate())

    insert into test select dateadd(dd, -3, getdate())

    insert into test select dateadd(dd, -4, getdate())

    insert into test select dateadd(dd, -5, getdate())

    insert into test select dateadd(dd, -6, getdate())

    insert into test select dateadd(dd, -7, getdate())

    insert into test select dateadd(dd, -8, getdate())

    select * from test

    alter table test

    add varcharfield varchar(10),

    kalmansfield varchar(10)

    update test

    set varcharfield = convert(varchar(10), datetimefield, 101),

    kalmansfield = convert(varchar(10), datetimefield, 111)

    declare @startdatetime datetime, @enddatetime datetime,

    @startvarchar varchar(10), @endvarchar varchar(10)

    select @startdatetime = convert(varchar(10), dateadd(dd, -5, getdate()), 101),

    @enddatetime = convert(varchar(10), dateadd(dd, -2, getdate()), 101),

    @startvarchar = convert(varchar(10), dateadd(dd, -5, getdate()), 111),

    @endvarchar = convert(varchar(10), dateadd(dd, -2, getdate()), 111)

     

    select *

    from test

    where datetimefield >= @startdatetime

    and datetimefield < @enddatetime

    select *

    from test

    where kalmansfield >= @startvarchar

    and kalmansfield < @endvarchar

    select @startvarchar = convert(varchar(10), dateadd(dd, -5, getdate()), 101),

    @endvarchar = convert(varchar(10), dateadd(dd, -2, getdate()), 101)

    select *

    from test

    where kalmansfield >= @startvarchar

    and kalmansfield < @endvarchar

    select *

    from test

    where varcharfield >= @startvarchar

    and varcharfield < @endvarchar

     

    drop table test

     

     

    So long as you are always using the same convert switch, it works, but make one mistake and a simple one at that, and you results are wrong, and you probably won't catch it, at least not immediately, as has been my experience in more than one occasion reviewing report code that was using varchar(10) in a temp table to store date fields.  The reports were always wrong, in some cases it took over 2 years for someone to actually notice. 

     

    Leave the data in datetime, and control display as that, display.  That is my recommendation.

    Thursday, September 4, 2008 12:07 AM
  •  

    I have already tried update with convert function but as someone told that time is inherent with date and we cant seperate them . The update was not doing anything . I learned that only way is to change them to varchar isntead of datetime.

     

     

    Thursday, September 4, 2008 1:55 PM