Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8415711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:31:27+00:00 2026-06-10T01:31:27+00:00

Using SQL Server 2008 R2, SP2 The docs says that datetime2 takes 6, 7

  • 0

Using SQL Server 2008 R2, SP2
The docs says that datetime2 takes 6, 7 or 8 bytes depending witch precision you use

I need to store a large amount of data in binary form (concatenated values) and I love the idea of using only 6 bytes for each datetime, however when I try:

declare @_dt_p0 datetime2(0) = '2012-05-18 11:22:33'
select  CONVERT(varbinary, @_dt_p0), LEN(CONVERT(varbinary, @_dt_p0))

declare @_dt_p4 datetime2(4) = '2012-05-18 11:22:33'
select  CONVERT(varbinary, @_dt_p4), LEN(CONVERT(varbinary, @_dt_p4))

declare @_dt_p7 datetime2(7) = '2012-05-18 11:22:33'
select  CONVERT(varbinary, @_dt_p7), LEN(CONVERT(varbinary, @_dt_p7))

It’s clearly taking one extra byte, what I’m doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T01:31:29+00:00Added an answer on June 10, 2026 at 1:31 am

    I don’t think I can explain why the length / datalength of a varbinary conversion is 7 instead of 6 (Mikael later found that the convert to varbinary adds the precision as an extra byte), but I don’t know why you think that’s a valid test anyway. I can confirm that 6 bytes are stored on the page when you are using an actual column (though null overhead for the row will be different depending on whether the column is nullable). How can I prove this?

    USE tempdb;
    GO
    
    CREATE TABLE dbo.x
    (
     d1 DATETIME2(0)  NULL, 
     v1 VARBINARY(32) NULL,
     d2 DATETIME2(0)  NOT NULL, 
     v2 VARBINARY(32) NOT NULL
    );
    
    declare @d datetime2(0) = '2012-05-18 11:22:33';
    
    INSERT dbo.x(d1, v1, d2, v2)
    SELECT @d, CONVERT(VARBINARY(32), @d), @d, CONVERT(VARBINARY(32), @d);
    
    SELECT DATALENGTH(d1), DATALENGTH(v1), 
           DATALENGTH(d2), DATALENGTH(v2) FROM dbo.x;
    

    Results:

    6    7    6    7
    

    So, the datetime2 columns are 6 bytes, but the varbinary columns are 7 bytes. Regardless of nullability. We can look closer by actually inspecting the page. Let’s find all the pages in the heap for this table:

    DBCC IND('tempdb', 'dbo.x', 0);
    

    Partial results on my system (yours will be different):

    PagePID  PageType
    283      10
    311      1
    

    So now let’s look at Page 311:

    DBCC TRACEON(3604, -1);
    DBCC PAGE(2, 1, 311, 3);
    

    And we can see that the datetime2 columns indeed occupy 6 bytes on the page:

    Slot 0 Column 1 Offset 0x4 Length 6 Length (physical) 6
    
    d1 = 2012-05-18 11:22:33            
    
    v1 = [Binary data] Slot 0 Column 2 Offset 0x19 Length 7 Length (physical) 7
    v1 = 0x00f99f00b0350b               
    
    Slot 0 Column 3 Offset 0xa Length 6 Length (physical) 6
    
    d2 = 2012-05-18 11:22:33            
    
    v2 = [Binary data] Slot 0 Column 4 Offset 0x20 Length 7 Length (physical) 7
    v2 = 0x00f99f00b0350b              
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using SQL Server 2008 R2 and my stored procedure takes in a
Using: SQL Server 2008 R2 I'd like to write a query that will select
using sql server 2008 I would like to take a table that has 11
I am using SQL Server 2008. I want to write a query that gives
I'm using SQL Server 2008 and I have a view that looks like this:
I'm using SQL Server 2008 Express, and I have a stored procedure that do
Using sql server 2008 I am getting and invalid precision value error in the
Using SQL Server 2008, I have a requirement that email addresses in my user
Using SQL Server 2008, I want to calculate the timespan, in seconds, that has
In a stored procedure (using SQL Server 2008 R2 SP2) is it possible to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.