473,378 Members | 1,549 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,378 software developers and data experts.

date formatting

I am trying to display dates in a spreadsheet, but the dates need to be in a
format that will allow them to be sorted in Excel. The datatype in the SQL
Server database is datetime. In this case, I need to display the date only,
not the time. But I don't want to change the datatype in the database
because the time is used in other places.

So what I am doing is pulling it out of the database, then modifying it in
ASP/VBScript by using the datevalue function. This results in values such as
3/31/2006, 4/3/2006, and 4/14/2006. The problem is, the ones with the
single digit date (4/3/2006 in the sample data I just listed) messes up
Excel's sorting capabilities. How can I force the dates to display in a
2-digit date format? It would probably be good to do the same for the month.

BTW, I am assuming that making dates and months into 2 digits will resolve
this issue, but that's all it is: an assumption. Any input on that would be
appreciated.

Aug 7 '06 #1
7 3029
You have many options, depending on your database.

The way I do this is to write a SQL query that formats the date before I
even get it into the recordset. If you are using ADO, don't open the
recordset directly from a table (with adoCmdTable), use SQL="SELECT ... FROM
TABLE;" (with adoCmdText). You can use the formatting functions in your
database to format it anyway you like!

I use Access databases, so I can use the VBA functions, which are the same
as in Excel. For example...

"SELECT Format(Now(),"yymmdd") AS SpecialDate FROM Dual;"

"Dual" is a dummy table I always include in my databases. It contains one
line of nothing. I believe I got the idea from Oracle databases where Dual
in a built-in dummy table.

What you can do would be something like this depending on the functions
available in your database's SQL implementation:

"SELECT Format([TheirDate],"dd-mm-yyyy") AS [MyDate], etc FROM
[TheirTable];"

I don't even try to format this in VBScript. It has no useful formatting
commands like VB6, VBA, and, I presume, VB.Net.

A more advanced way to do this (if you write your data into Excel via a
client VBScript written to the browser by an ASP server VBScript) is just to
write it into the cell with the Excel VBA function wrapped around it like
this:

Response.Write "<SCRIPT LANGUAGE=""VBScript"">" & vbCrLf
...
Response.Write "wb.Activesheet.Range(""A1"").Formula = ""=Format(""" &
rsMyRecordset("TheirDate") & """,""dd-mm-yyyy"")" & vbCrLf
...
Response.Write "</SCRIPT>" & vbCrLf

However, writing scripts with scripts can be really confusing.

Good Luck.

— Jim

--
James W. (Jim) Rodgers, P.E., is a Senior Partner with General Consulting
Engineers, LLC, in Atlanta, Georgia.
"Middletree" wrote:
I am trying to display dates in a spreadsheet, but the dates need to be in a
format that will allow them to be sorted in Excel. The datatype in the SQL
Server database is datetime. In this case, I need to display the date only,
not the time. But I don't want to change the datatype in the database
because the time is used in other places.

So what I am doing is pulling it out of the database, then modifying it in
ASP/VBScript by using the datevalue function. This results in values such as
3/31/2006, 4/3/2006, and 4/14/2006. The problem is, the ones with the
single digit date (4/3/2006 in the sample data I just listed) messes up
Excel's sorting capabilities. How can I force the dates to display in a
2-digit date format? It would probably be good to do the same for the month.

BTW, I am assuming that making dates and months into 2 digits will resolve
this issue, but that's all it is: an assumption. Any input on that would be
appreciated.

Aug 7 '06 #2
I don't know any way to say this without seeming like an idiot, but I have
been staring at your post for a long time now, and still don't understand
any of it.
"Jim Rodgers" <Ji********@discussions.microsoft.comwrote in message
news:BD**********************************@microsof t.com...
You have many options, depending on your database.

The way I do this is to write a SQL query that formats the date before I
even get it into the recordset. If you are using ADO, don't open the
recordset directly from a table (with adoCmdTable), use SQL="SELECT ...
FROM
TABLE;" (with adoCmdText). You can use the formatting functions in your
database to format it anyway you like!

I use Access databases, so I can use the VBA functions, which are the same
as in Excel. For example...

"SELECT Format(Now(),"yymmdd") AS SpecialDate FROM Dual;"

"Dual" is a dummy table I always include in my databases. It contains one
line of nothing. I believe I got the idea from Oracle databases where
Dual
in a built-in dummy table.

What you can do would be something like this depending on the functions
available in your database's SQL implementation:

"SELECT Format([TheirDate],"dd-mm-yyyy") AS [MyDate], etc FROM
[TheirTable];"

I don't even try to format this in VBScript. It has no useful formatting
commands like VB6, VBA, and, I presume, VB.Net.

A more advanced way to do this (if you write your data into Excel via a
client VBScript written to the browser by an ASP server VBScript) is just
to
write it into the cell with the Excel VBA function wrapped around it like
this:

Response.Write "<SCRIPT LANGUAGE=""VBScript"">" & vbCrLf
...
Response.Write "wb.Activesheet.Range(""A1"").Formula = ""=Format(""" &
rsMyRecordset("TheirDate") & """,""dd-mm-yyyy"")" & vbCrLf
...
Response.Write "</SCRIPT>" & vbCrLf

However, writing scripts with scripts can be really confusing.

Good Luck.

- Jim

--
James W. (Jim) Rodgers, P.E., is a Senior Partner with General Consulting
Engineers, LLC, in Atlanta, Georgia.
"Middletree" wrote:
>I am trying to display dates in a spreadsheet, but the dates need to be
in a
format that will allow them to be sorted in Excel. The datatype in the
SQL
Server database is datetime. In this case, I need to display the date
only,
not the time. But I don't want to change the datatype in the database
because the time is used in other places.

So what I am doing is pulling it out of the database, then modifying it
in
ASP/VBScript by using the datevalue function. This results in values such
as
3/31/2006, 4/3/2006, and 4/14/2006. The problem is, the ones with the
single digit date (4/3/2006 in the sample data I just listed) messes up
Excel's sorting capabilities. How can I force the dates to display in a
2-digit date format? It would probably be good to do the same for the
month.

BTW, I am assuming that making dates and months into 2 digits will
resolve
this issue, but that's all it is: an assumption. Any input on that would
be
appreciated.


Aug 7 '06 #3
I think I have narrowed down the problem, but not sure how to fix it.

I am producing a spreadsheet like the one linked at
http://www.middletree.net/get.htm

You'll note that the column called Open date has dates, but each one is
padded with 2 spaces before the characters begin. I am using Trim. Have no
idea why it doesn't get rid of those spaces.
Aug 7 '06 #4
Middletree wrote:
You'll note that the column called Open date has dates, but each one
is padded with 2 spaces before the characters begin. I am using Trim.
Have no idea why it doesn't get rid of those spaces.
This is why (from your code):

<td valign=top width=10 align=left>&nbsp;
10/12/2005
</td>

You have 6 whitespace characters between the end of the tag opening and the
beginning of the date. I recommend you change it to this:

<td valign=top width=10 align=left>10/12/2005</td>
I have also found it useful to use some of Excel's styles when I want to
prevent coercion:

td, th { mso-number-format:"\@"; }
.Number { mso-number-format:General; }
.Fixed { mso-number-format:Fixed; }


--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms.
Aug 7 '06 #5
Unbelievable. HTML 101.

Oh well. This wasn't a waste of a thread, and I was unaware of the Excel
styles. I'll have to check into it.

thanks


Aug 8 '06 #6
Middletree wrote:
Unbelievable. HTML 101.
I recall going through a similar problem myself a few years ago. Excel likes
to guess your data type, so you precede everything with &nbsp; to force
plain text. Much later, you find out that you don't want simply plain text.

My advice is to create a sample Excel spreadsheet with the data formats you
want, then save as html and dissect. That will give you your style
definitions.

--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms.
Aug 8 '06 #7
My advice is to create a sample Excel spreadsheet with the data formats
you want, then save as html and dissect. That will give you your style
definitions.
Good idea. Thanks.
Aug 8 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ed | last post by:
Hi there, My problem is the following: When I assign a custom formatted Date & Time to a Date variable it loses it’s formatting. Ex. 2005-06-07 15:46 now when I assign this to a variable of...
4
by: Paul | last post by:
Hi, Everything went fine getting a short date format out of SQL into my DataGrid with this: <%# DataBinder.Eval(Container.DataItem, "Created", "{0:d}")%> Then I got too fancy in SQL and...
4
by: dhnriverside | last post by:
Hi peeps I have a datepicker control that's providing dates in the format dd/mm/yyyy (UK). I want to convert this to "yyyy-mm-dd" to store as a text field in my database (had lots of problems...
12
by: Rob T | last post by:
I'm storing a date/time into a SQL table of type datetime. I need it to be precise so the value is stored to the 1000th of a second. ie "insert into myTable mydate values ('08/05/2005...
2
by: johndcal | last post by:
Hello All, I have a date value that I pull from a .csv file. After reading the file and storing the values in an array the value of the date could be found in $array, for example....
2
by: David Veeneman | last post by:
How does one format a date column in a GridView control? I had assumed that the DataFormat string would do it, but MSDN only shows numeric formatting codes. Can dates be formatted using that...
3
by: Jim in Arizona | last post by:
I have a gridview that's being populated from an access db query. The problem I'm having is that the date/time fields in access that are populating the gridview are showing both date and time, when...
21
by: Darin | last post by:
I have an applicatoin that works 100% perfect when running on a machine setup for English (United States), but when I change it to Spanish (Mexico), the dates start giving me fits. THe reason is...
7
by: creative1 | last post by:
Hello everyone. I am experiencing a strange problem that I can't fix on my own. I think I need expert's suggestions for this. The problem is: I want to print account statement (or any other...
9
by: Martin | last post by:
I'm retrieving some records from a database. One of the fields contains a date/time. I would like to format it as I send it out to the table in the displayed page. Can some one please tell me...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.