datetime as TEXT

james_lankford

Well-known Member
Joined
Jan 11, 2009
Messages
1,223
We are using a database that is on sql server.
We didn't make the database.
It is part of a product we bought.
We have no rights to change any part of design in the database, or create tables or anything else.
I can link to the sql server tables through MS Access and run queries.

One of the tables in the sql server database stores a date time value as TEXT in this format:

yyyymmddhhmmsshh

so 2:34:55.01 pm of Feb 28 2011 would look like

'2011022814345501'

I'm currently doing my queries like this

Code:
...
where 
(
  datetime_column Between '2010120100000000' And '2010123199999999'

)
to try and get all values for Dec 2010

does anybody see any problem with this ?
It seems to be working, but of course I can't possibly test for all possible date and time values

I'm worried about text not sorting the way I think it will.
Does anybody foresee any problems that may arise with this ?

I was thinking of doing it like this
Code:
...
where 
(
  cdbl(datetime_column) Between 2010120100000000 And 2010123199999999
)

that gives the same results as the first way, but like I said I can't possibly test for all possible date and time values
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
If you're worried about the text not acting like a date you could create two functions, one to extract the date and another to extract the time from the string. Or, just one to convert the string to a date, whichever approach works best. Then you could create new columns in your queries using those functions.

hth,

Rich
 
Upvote 0
James,

That logic *seems* like it should work to me. I have done similar queries using date as text with Access databases only, and they worked fine.

As long as all the text values are exactly the same format and length, I don't see a problem with it.
 
Upvote 0
I think also working would be this (looks more normal to human eyes):

Code:
where 
(
  datetime_column >= '20101201' 
  AND
  datetime_column < '20110101'
)

Note that this format isn't (or need not) be so strange. It's close to (or in) ISO-8601 standard format. Some databases use this as the standard format for date storage (e.g., SqlLite). I think its great for international use. Text sorting will put the dates in order correctly since the years are first, then months, then days ... and so in with the least important information at the (right) end of the string.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,550
Messages
6,179,459
Members
452,915
Latest member
hannnahheileen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top