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

  • SEARCH
  • Home
  • 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 8668285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:10:32+00:00 2026-06-12T18:10:32+00:00

I have a table with the following structure: CREATE TABLE MyTable ( ID int

  • 0

I have a table with the following structure:

CREATE TABLE MyTable (
   ID int identity,
   Whatever varchar(100),
   MyTime time(2) NOT NULL,
   MyDate date NOT NULL,
   MyDateTime AS (DATEADD(DAY, DATEDIFF(DAY, '19000101', [MyDate]), 
                          CAST([MyDate] AS DATETIME2(2))))
)

The computed column adds date and time into a single datetime2 field.

Most queries against the table have one or more of the following clauses:

... WHERE MyDate < @filter1 and MyDate > @filter2
... ORDER BY MyDate, MyTime
... ORDER BY MyDateTime

In a nutshell, date is usually used for filtering, and full datetime is used for sorting.

Now for questions:

  1. What is the best way to set indices on those 3 date-time columns? 2 separate on date and time or maybe 1 on date and 1 on composite datetime, or something else? Quite a lot of inserts and updates occur on this table, and I’d like to avoid over-indexing.

  2. As I wrote this question, I noticed the long and kind of ugly computed column definition. I picked it up from somewhere a while ago and forgot to investigate if there’s a simpler way of doing it. Is there any easier way of combining a date and time2 into a datetime2? Simple addition does not work, and I’m not sure if I should avoid casting to varchar, combining and casting back.

  • 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-12T18:10:33+00:00Added an answer on June 12, 2026 at 6:10 pm

    Having date and time in two separate columns may seem peculiar but if you have queries that use only the date (and/or especially only the time part), I think it’s a valid decision. You can create an index on date only or on time or on (date, whatever), etc.

    What I don’t understand is why you also have the computed datetime column as well. There s no reason to store this value, too. It can easily be calculated when needed.

    And if you need to order by datetime, you can use ORDER BY MyDate, MyTime. With an index on (MyDate, MyTime) this should be ok. Range datetime queries would also be using that index.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following table structure: CREATE TABLE [Report].[MesReport]( [MesReportID] [int] IDENTITY(1,1) NOT NULL,
I have the following table structure CREATE TABLE `table` ( `id` int(11) NOT NULL
I have following table structure: CREATE TABLE pilot_groups ( id INT PK, name VARCHAR(50),
I have the following table structure: CREATE TABLE a ( a_id int(10) unsigned NOT
I have the following database structure: CREATE TABLE `author` ( `id` int(10) unsigned NOT
I have DB2 table having following structure CREATE TABLE DUMMY ( ID CHARACTER(10) NOT
Lets say I have the following MySQL structure: CREATE TABLE `domains` ( `id` INT(10)
I have the following table structure in my database: create table Encargado( ID int
I have the following structure SET SQL_MODE=NO_AUTO_VALUE_ON_ZERO; CREATE TABLE IF NOT EXISTS `sis_param_tax` (
I have the following table structure: EVENT_ID(INT) EVENT_NAME(VARCHAR) EVENT_DATE(DATETIME) EVENT_OWNER(INT) I need to add

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.