Date Time Fields

I have a database with a DateTime field of the following structure 1974-01-18T00:00:00′. How do I query it so that I can extract the dates with a particular day and month but any year?

The datepart function in SQL will retrieve a portion of the date:

create table datesearch(	mydate datetime)goinsert into datesearch values ('01/01/2000')insert into datesearch values ('01/02/2000')insert into datesearch values ('01/03/2000')insert into datesearch values ('02/01/2000')insert into datesearch values ('02/02/2000')insert into datesearch values ('02/03/2000')goselect *from datesearchwhere datepart(dd,mydate) = 2

It will return only the rows where mydate falls on the second day of the month.

However, before writing such a query, you should consider the performance you will get from it. In order to test the performance, I created a sample table and populated it with 500,000 rows:

set nocount ongodrop table datesearchgocreate table datesearch(	id int identity(1,1) not null	constraint pk_datesearch primary key(id),	invoicedate datetime,	clientid  int,	amount numeric (9,2))godeclare @i int, @invoicedate datetime,@clientid int, @amount numeric(9,2)select @i = 0while @i < 500000begin	select @invoicedate = dateadd(dd,cast (rand() * 5000 as int),getdate())	select @clientid = cast(rand() * 500 as int)	select @amount = rand() * 500000	insert into datesearch(invoicedate,clientid,amount)	values			(@invoicedate,@clientid,@amount)	select @i = @i + 1end		gocreate index ak_datesearch_invoicedate on datesearch(invoicedate)

I then used the following two queries to compare the statistics and the showplan:

select count(*)from datesearchwheredatepart(dd,invoicedate) = 20select count(*)from datesearchwhereinvoicedate between '10/01/2000' and '12/01/2000'

The query that used datepart performed a table scan while the second query used the index on invoicedate. If you find that you are suffering a performance hit, you might choose to add another column to your table to represent just the day of the month (or whatever other value) you are searching on and index it. This is different than adding a computed column because in version 7 you can't index on a computed column.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.