473,405 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Convert date to number

Hi

Can anyone help me how to convert date to number.

If date is 04-15-2019, I want to store it as 20190415 in number format.

I have used =Format ([DateField],"yyyymmdd") and tried to store if as string but even this doesn't seem to work.

Any help is appreciated.
Dec 9 '19 #1

✓ answered by ADezii

To the best of my knowledge, you cannot Set the Default Value of a Field in a Table to a Formatted Expression involving another Field in the same Table. That being said, and as stated by cactusdata, you can generate this Number within a Query, an example of which would be:
Expand|Select|Wrap|Line Numbers
  1. SELECT Table1.ID1, Table1.DateField, Table1.SerialNum, Table1.CustNo, 
  2. Format$([DateField],"yyyymmdd") & Format([SerialNum],"00") AS DateConv
  3. FROM Table1;
Expand|Select|Wrap|Line Numbers
  1. ID1    DateField    SerialNum    CustNo    DateConv
  2. 1      12/10/2019       2        12345     2019121002
  3.  

8 7227
ADezii
8,834 Expert 8TB
You can coerce the Formatted Expression into a LONG, as in:
Expand|Select|Wrap|Line Numbers
  1. Dim strTheDate As String
  2. Dim lngTheDate As Long
  3.  
  4. strTheDate = "4-15-2019"
  5.  
  6. lngTheDate = CLng(Format("4-15-2019", "yyyymmdd"))
  7.  
Dec 9 '19 #2
I have tried using =CLng(Format[DateField], "yyyymmdd")) in default value of table properties. However error shows up. The error message Is-The database engine does not recognize either the field 'DateField' in a validation expression, or the default value in table 'Table1'.

Can you give me any advice.
Dec 9 '19 #3
cactusdata
214 Expert 128KB
Expand|Select|Wrap|Line Numbers
  1. Format([DateField],"yyyymmdd")
will work, even for Null values, so if "it doesn't work" (whatever that means), something else is going on.

To convert to a number, use Val:

Expand|Select|Wrap|Line Numbers
  1. Val(Format([DateField],"yyyymmdd"))
That said, you should always store dates as DateTime and, when retrieved and for display, format for the current purpose.
Dec 9 '19 #4
NeoPa
32,556 Expert Mod 16PB
CactusData is spot-on. If you have the problems as described then they are not connected with your question. It's hard to help with details when you don't share the details. Val() or CLng() should both work fine for what you're describing. What else you're doing wrong is outside of the scope of this discussion - especially as you don't share what else you're doing.
Dec 10 '19 #5
I wish to create a database to enter transactions at my shop.
Each customer is assigned a unique serial number for a transaction. Uptill now I used manual register for entries. Everyday serial number starts from one. To give each transaction a unique identification number I thought of combining date with serial number.

Example a customer assigned serial number 02 today would be given a unique serial number 2019121002. This could be tracked even a year later.
So i wanted to convert date to number and add it to serial number.

However the database engine can't recognise the field. I have attached the table structure and error msg pics.

Thanks.
Attached Images
File Type: jpg Screen pic 1.jpg (213.9 KB, 240 views)
File Type: jpg Screen 2.jpg (187.4 KB, 220 views)
Dec 10 '19 #6
cactusdata
214 Expert 128KB
Don't ever use calculated fields. It's a dead end.

Write a query for this.
Dec 10 '19 #7
ADezii
8,834 Expert 8TB
To the best of my knowledge, you cannot Set the Default Value of a Field in a Table to a Formatted Expression involving another Field in the same Table. That being said, and as stated by cactusdata, you can generate this Number within a Query, an example of which would be:
Expand|Select|Wrap|Line Numbers
  1. SELECT Table1.ID1, Table1.DateField, Table1.SerialNum, Table1.CustNo, 
  2. Format$([DateField],"yyyymmdd") & Format([SerialNum],"00") AS DateConv
  3. FROM Table1;
Expand|Select|Wrap|Line Numbers
  1. ID1    DateField    SerialNum    CustNo    DateConv
  2. 1      12/10/2019       2        12345     2019121002
  3.  
Dec 10 '19 #8
twinnyfo
3,653 Expert Mod 2GB
OR..... Instead of insisting that the field is a numerical value, you could have it as short text of 13 characters. Then your serial number number could be: "2019-12-10-02".

Conversion back to dates is simple:
Expand|Select|Wrap|Line Numbers
  1. dtOrderDate = cDate(Left([SerialNumber], 10))
It just depends upon what info you need to track in this Serial Number.
Dec 10 '19 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: sumeet | last post by:
how do i convert a yyyymmdd format to yyyy,mm,dd format. also i want to know how do i get the number of days between two dates which r in the format yyyymmdd. how to convert date into unix time...
3
by: galsaba | last post by:
What would be the function to convert date to the number of date for example, 2/14/05 is 45, becuse this is the 45th day of the year. What would be the function? Where can I find info on the web...
7
by: whatluo | last post by:
Hi, all I'm now working on a program which will convert dec number to hex and oct and bin respectively, I've checked the clc but with no luck, so can anybody give me a hit how to make this done...
3
by: sparkle | last post by:
Hi, Does anybody know how to convert date to ticks from a dateTimePicker? What I'm using now isn't working. I'm trying to use a dateTimePicker to set an appointment in Outlook with a...
5
by: Totto | last post by:
Hi, Is it possible to convert date to dd mon yyyy hh:mi(24h) Thanks Totto
19
by: VK | last post by:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/ b495b4898808fde0> is more than one month old - this may pose problem for posting over some news servers. This is why I'm...
15
by: Lyosha | last post by:
Converting binary to base 10 is easy: 255 Converting base 10 number to hex or octal is easy: '0144' '0x64' Is there an *easy* way to convert a number to binary?
17
KalariaNitya
by: KalariaNitya | last post by:
Hello All, I want to convert Date to Hindu Calender. For e.g. today is 12th sep'2008 so convert it to bhadarvo sud baras. can anybody help me how to do that? thanking u in advanced..
4
subedimite
by: subedimite | last post by:
Here I am with another question being a very new VBA user. I have this scenario to work with: I would think this is fairly common routine of work for VBA. I have a string of hex contents as...
2
by: Irfan iffi | last post by:
how to convert date of bith in (dd-mon-yyyy) this formate ? plz kindly help me How to convert date of birth in in years in oracle form 6i
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.