Formula to convert text/string to date format

sunnyzwei

New Member
Joined
Sep 14, 2023
Messages
3
Office Version
  1. 365
Hello!

I'm trying to add a formula that will compare whatever the current date is to a date field. However the date field is just a string/text value.

the date field is stored like this 20230914 and it's just recognized as text so I need it to be a date like this 9-14-2023

I've tried format([date field],"mm-dd-yyyy") >date() but I get an overflow error

I know the answer is simple but just can't figure out what it should be. :)
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
and i'm trying to do this in the design view of the query for criteria
1694725477019.png
 
Upvote 0
The FORMAT function converts date/numeric fields to text values, not the other way around.
You first need to convert the Text value to a Date field. Then you can do the comparison.

You can use the DATESERIAL function to do this, i.e.
Rich (BB code):
DATESERIAL(LEFT([BBNDTEF],4),MID([BBNDTEF],5,2),RIGHT([BBNDTEF],2))
I would do that in a Calculated field, and then apply the criteria to that calculated field of:
Rich (BB code):
<DATE()
 
Upvote 0
I'd just take the date() function and format it as yyyymmdd and leave the database field as is

BBNDTEF < Format(Date, "yyyymmdd")
While that should work in this case, just note that this method won't always work.

In this instance, you are comparing a string to a string. It should work if the format is "yyyymmdd", but most other date formats will NOT work (i.e. if the order is not YEAR -> MONTH -> DATE or if either the month or date does not use leading zeroes).

So it would work for formats like:
yyyymmdd
yyyy-mm-dd
yyyy/mm/dd
yyyy.mm.dd

but it would not work for most other formats.
 
Upvote 0
I don't know about MS Access, but I think i've read that in MS SQL Server if you have an index on field and then do a formula on that field the index isn't used

so in this case if BBNDTEF was indexed and you used format or dateserial on something on it then the index is ignored
but if you leave it as is and instead put the formula on the date() then the index is used
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,021
Latest member
pingpong7117

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