Set time to default value as zero

  • I am having a stored procedure that inserts data in a table. There is a date column in which I am taking the time using getdate() function and inserting the time in that date column. It saves date and time in that column.

    For e.g. 17/07/2009 14:23:10

    What I want is to store date and time, but time should always default to zero.

    For e.g. 17/07/2009 00:00:00

    Please tell me how to do this.

  • satishp.mst (7/17/2009)


    What I want is to store date and time, but time should always default to zero.

    For e.g. 17/07/2009 00:00:00

    quote]

    Hi,

    only option you must convert the datetime and insert it to the table

    like

    create table #temp

    (

    slno int,

    date1 datetime

    )

    insert into #temp

    select 1,getdate()

    union all

    select 2,convert(varchar(15),getdate(),101)

    or

    Table like

    create table #temp_TABLE

    (

    slno int,

    date1 datetime,

    Dayonly as cast(convert(varchar(15),date1,101)as datetime)

    )

    insert into #temp_TABLE

    select 1,getdate()

  • Based on arun's sample data there's another option (which is usually faster):

    create table #temp

    (

    slno int,

    date1 datetime

    )

    insert into #temp

    select 1,getdate()

    union all

    select 2,DATEADD(dd,0,DATEDIFF(dd,0,getdate()))

    For a performance comparison of the various date conversion functions please see one of Gails articles [/url].

    For a list of several date functions please see one of Lynn's blogs.



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • Let me come right out and say it... don't use CONVERT to strip the time... it's at least ten times slower than just about any other method.

    So far as testing goes, here's a bit of testing I did a while back...

    First, my standard million row test table...

    --===== Create and populate a 1,000,000 row test table.

    -- Column "RowNum" has a range of 1 to 100,000 unique numbers

    -- Column "SomeInt" has a range of 1 to 50,000 non-unique numbers

    -- Column "SomeLetters2" has a range of "AA" to "ZZ" non-unique 2 character strings

    -- Column "SomeMoney has a range of 0.0000 to 99.9999 non-unique numbers

    -- Column "SomeDate" has a range of >=01/01/2000 and <01/01/2010 non-unique date/times

    -- Column "SomeCSV" contains 'Part01,Part02,Part03,Part04,Part05,Part06,Part07,Part08,Part09,Part10'

    -- for all rows.

    -- Column "SomeHex12" contains 12 random hex characters (ie, 0-9,A-F)

    -- Jeff Moden

     

    SELECT TOP 1000000

    SomeID = IDENTITY(INT,1,1),

    SomeInt = ABS(CHECKSUM(NEWID()))%50000+1,

    SomeLetters2 = CHAR(ABS(CHECKSUM(NEWID()))%26+65)

    + CHAR(ABS(CHECKSUM(NEWID()))%26+65),

    SomeCSV = CAST('Part01,Part02,Part03,Part04,Part05,Part06,Part07,Part08,Part09,Part10' AS VARCHAR(80)),

    SomeMoney = CAST(ABS(CHECKSUM(NEWID()))%10000 /100.0 AS MONEY),

    SomeDate = CAST(RAND(CHECKSUM(NEWID()))*3653.0+36524.0 AS DATETIME),

    SomeHex12 = RIGHT(NEWID(),12)

    INTO dbo.JBMTest

    FROM Master.dbo.SysColumns t1,

    Master.dbo.SysColumns t2 --Lack of join criteria makes this a CROSS-JOIN

     

    --===== A table is not properly formed unless a Primary Key has been assigned

    -- Takes about 1 second to execute.

    ALTER TABLE dbo.JBMTest

    ADD PRIMARY KEY CLUSTERED (SomeID)

    And, now the test code which does the conversions into a "bit bucket" variable to take the display out of the picture...

    DECLARE @BitBucketDATETIME DATETIME

     

    PRINT '========== BASELINE =========='

    SET STATISTICS TIME ON

    SELECT @BitBucketDATETIME = SomeDate

    FROM dbo.JBMTest WITH (NOLOCK)

    SET STATISTICS TIME OFF

    PRINT REPLICATE('=',80)

     

    PRINT '========== DATEADD/DATEDIFF 1 =========='

    SET STATISTICS TIME ON

    SELECT @BitBucketDATETIME = DATEADD(d, 0, DATEDIFF(d, 0, SomeDate))

    FROM dbo.JBMTest WITH (NOLOCK)

    SET STATISTICS TIME OFF

    PRINT REPLICATE('=',80)

     

    PRINT '========== DATEADD/DATEDIFF 2 =========='

    SET STATISTICS TIME ON

    SELECT @BitBucketDATETIME = DATEADD(dd,DATEDIFF(dd,0,SomeDate),0)

    FROM dbo.JBMTest WITH (NOLOCK)

    SET STATISTICS TIME OFF

    PRINT REPLICATE('=',80)

     

    PRINT '========== DATEDIFF Implicit =========='

    SET STATISTICS TIME ON

    SELECT @BitBucketDATETIME = DATEDIFF(d, 0, SomeDate)

    FROM dbo.JBMTest WITH (NOLOCK)

    SET STATISTICS TIME OFF

    PRINT REPLICATE('=',80)

     

    PRINT '========== CONVERT =========='

    SET STATISTICS TIME ON

    SELECT @BitBucketDATETIME = CONVERT(DATETIME,CONVERT(VARCHAR,SomeDate,100))

    FROM dbo.JBMTest WITH (NOLOCK)

    SET STATISTICS TIME OFF

    PRINT REPLICATE('=',80)

     

    PRINT '========== CONVERT Implicit=========='

    SET STATISTICS TIME ON

    SELECT @BitBucketDATETIME = CONVERT(VARCHAR,SomeDate,100)

    FROM dbo.JBMTest WITH (NOLOCK)

    SET STATISTICS TIME OFF

    PRINT REPLICATE('=',80)

     

    PRINT '===== FLOOR ====='

    SET STATISTICS TIME ON

    SELECT @BitBucketDATETIME = CAST(FLOOR(CONVERT(FLOAT, SomeDate)) AS DATETIME)

    FROM dbo.JBMTest WITH (NOLOCK)

    SET STATISTICS TIME OFF

    PRINT REPLICATE('=',80)

     

    PRINT '===== FLOOR Implicit ====='

    SET STATISTICS TIME ON

    SELECT @BitBucketDATETIME = FLOOR(CONVERT(FLOAT, SomeDate))

    FROM dbo.JBMTest WITH (NOLOCK)

    SET STATISTICS TIME OFF

    PRINT REPLICATE('=',80)

     

    PRINT '===== ROUNDING 1 ====='

    SET STATISTICS TIME ON

    SELECT @BitBucketDATETIME = CAST(CAST(SomeDate - 0.50000004 AS INT) AS DATETIME)

    FROM dbo.JBMTest WITH (NOLOCK)

    SET STATISTICS TIME OFF

    PRINT REPLICATE('=',80)

     

    PRINT '===== ROUNDING 1 Implicit ====='

    SET STATISTICS TIME ON

    SELECT @BitBucketDATETIME = CAST(SomeDate - 0.50000004 AS INT)

    FROM dbo.JBMTest WITH (NOLOCK)

    SET STATISTICS TIME OFF

    PRINT REPLICATE('=',80)

     

    PRINT '===== ROUNDING 2 ======'

    SET STATISTICS TIME ON

    SELECT @BitBucketDATETIME = CAST(ROUND(CAST(SomeDate AS FLOAT),0,1) AS DATETIME)

    FROM dbo.JBMTest WITH (NOLOCK)

    SET STATISTICS TIME OFF

    PRINT REPLICATE('=',80)

     

    PRINT '===== ROUNDING 2 Implicit ======'

    SET STATISTICS TIME ON

    SELECT @BitBucketDATETIME = ROUND(CAST(SomeDate AS FLOAT),0,1)

    FROM dbo.JBMTest WITH (NOLOCK)

    SET STATISTICS TIME OFF

    PRINT REPLICATE('=',80)

    And, here're the results on my 7 year old desktop box...

    [font="Courier New"]========== BASELINE ==========

    SQL Server Execution Times:

       CPU time = 0 ms,  elapsed time = 0 ms.

    SQL Server Execution Times:

       CPU time = 547 ms,  elapsed time = 547 ms.

    ================================================================================

    ========== DATEADD/DATEDIFF 1 ==========

    SQL Server Execution Times:

       CPU time = 0 ms,  elapsed time = 0 ms.

    SQL Server Execution Times:

       CPU time = 984 ms,  elapsed time = 987 ms.

    ================================================================================

    ========== DATEADD/DATEDIFF 2 ==========

    SQL Server Execution Times:

       CPU time = 0 ms,  elapsed time = 0 ms.

    SQL Server Execution Times:

       CPU time = 886 ms,  elapsed time = 886 ms.

    ================================================================================

    ========== DATEDIFF Implicit ==========

    SQL Server Execution Times:

       CPU time = 0 ms,  elapsed time = 0 ms.

    SQL Server Execution Times:

       CPU time = 734 ms,  elapsed time = 740 ms.

    ================================================================================

    ========== CONVERT ==========

    SQL Server Execution Times:

       CPU time = 0 ms,  elapsed time = 0 ms.

    SQL Server Execution Times:

       CPU time = 9969 ms,  elapsed time = 9977 ms.

    ================================================================================

    ========== CONVERT Implicit==========

    SQL Server Execution Times:

       CPU time = 0 ms,  elapsed time = 0 ms.

    SQL Server Execution Times:

       CPU time = 10094 ms,  elapsed time = 10156 ms.

    ================================================================================

    ===== FLOOR =====

    SQL Server Execution Times:

       CPU time = 0 ms,  elapsed time = 0 ms.

    SQL Server Execution Times:

       CPU time = 1896 ms,  elapsed time = 1896 ms.

    ================================================================================

    ===== FLOOR Implicit =====

    SQL Server Execution Times:

       CPU time = 0 ms,  elapsed time = 0 ms.

    SQL Server Execution Times:

       CPU time = 1859 ms,  elapsed time = 1861 ms.

    ================================================================================

    ===== ROUNDING 1 =====

    SQL Server Execution Times:

       CPU time = 0 ms,  elapsed time = 0 ms.

    SQL Server Execution Times:

       CPU time = 719 ms,  elapsed time = 725 ms.

    ================================================================================

    ===== ROUNDING 1 Implicit =====

    SQL Server Execution Times:

       CPU time = 0 ms,  elapsed time = 0 ms.

    SQL Server Execution Times:

       CPU time = 700 ms,  elapsed time = 700 ms.

    ================================================================================

    ===== ROUNDING 2 ======

    SQL Server Execution Times:

       CPU time = 0 ms,  elapsed time = 0 ms.

    SQL Server Execution Times:

       CPU time = 3032 ms,  elapsed time = 3037 ms.

    ================================================================================

    ===== ROUNDING 2 Implicit ======

    SQL Server Execution Times:

       CPU time = 0 ms,  elapsed time = 0 ms.

    SQL Server Execution Times:

       CPU time = 3057 ms,  elapsed time = 3057 ms.

    ================================================================================

    [/font]

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply