Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Classic ASP Professional For advanced coder questions in ASP 3. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old March 29th, 2005, 01:06 PM
Authorized User
 
Join Date: Jan 2005
Location: , , .
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Default Trying to insert a date into MySQL from asp

Hey everyone. Trying to use a MySQL database for the first time...

It likes dates to be formated into the following YYYY-MM-DD and as far as I know there is no way to change it...although I could be wrong.

Regardless, it doesn't matter to me how it is stored or even displayed later on. What I need to know is how to use asp to get the date formatted this way so the database will accept it.

here is a sample of what I am trying to do.

set newupdate = server.createobject("adodb.recordset")

    sql = "INSERT INTO HomeUpdates (tDate,UpdateString) VALUES ('"&date()&"','"&comment&"')"

    set newupdate = myconn.execute(sql)

there are no errors but it puts 0000-00-00 in the tdate field.

any ideas?
  #2 (permalink)  
Old April 25th, 2005, 10:41 AM
Registered User
 
Join Date: Apr 2005
Location: Vicenza, , Italy.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to im-imran
Default

You must have to convert first your date format from vb to php

vb is like mon/date/year
php is year/mon/date

so do like this

str1 = month(Date)
str2 = day(date)
str3 = year(date)
datestrs = str3 & "-" & str1 & "-" & str1

sql = "INSERT INTO HomeUpdates (tDate,UpdateString) VALUES ('"& datestrs &"','"&comment&"')"


Thats all

Dates are always prob i am still havin prob with dates to sort them when i am making a hitcounter with mysql

  #3 (permalink)  
Old June 13th, 2007, 12:01 PM
Registered User
 
Join Date: Nov 2006
Location: Bristol, , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I came across this problem recently too. My solution:


Dim dateStr As String

dateStr = DateTime.Now.Year & "-"
dateStr &= DateTime.Now.Month & "-"
dateStr &= DateTime.Now.Day & " "
dateStr &= DateTime.Now.Hour & ":"
dateStr &= DateTime.Now.Minute & ":"
dateStr &= DateTime.Now.Second

strSQL = "INSERT INTO users (userJoinDate)"
strSQL &= " VALUES ("
strSQL &= "'" & dateStr & "')"
  #4 (permalink)  
Old February 19th, 2008, 03:28 PM
Authorized User
Points: 483, Level: 7
Points: 483, Level: 7 Points: 483, Level: 7 Points: 483, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2007
Location: Edgewood, NY, USA.
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok, half way there now...

I used this code:
str1 = month(Date)
str2 = day(date)
str3 = year(date)
datestrs = str3 & "-" & str1 & "-" & str2

works fine except, I need the date from my sql database to get called in. This above code calls in the "current" or "now" date. i want to format the date that already resides in the database. any suggestions?

  #5 (permalink)  
Old February 19th, 2008, 05:38 PM
Friend of Wrox
Points: 6,664, Level: 34
Points: 6,664, Level: 34 Points: 6,664, Level: 34 Points: 6,664, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

FYI the following is incorrect:

;;;You must have to convert first your date format from vb to php
;;;vb is like mon/date/year
;;;php is year/mon/date

This question seems very similar to this post of yours:
http://p2p.wrox.com/topic.asp?TOPIC_ID=69130

You have been lucky enough to have been given advise from one of the Wrox MVP's. The link he gave you from classicasp.aspfaq.com is a very good one, it has all your answers.

IMO you should ALWAYS wrap functions around dates being inserted into any DB and wrap functions around ALL dates rendered on web pages. This is the function I use to ensure whwn inserting a date into a DB it ALWAYS gets turned into American date format (mm/dd/yyyy)

  FUNCTION amDate(varDate)
    IF DatabaseType = iDT_SQLServer THEN
      IF isNull(varDate) OR Trim(varDate) = "" OR varDate = "Null" THEN
        amDate = "Null"
      ELSE
        amDate = "'" & Month(DateValue(varDate)) & "/" & Day(DateValue(varDate)) & "/" & Year(DateValue(varDate)) & " " & TimeValue(varDate) & "'"
      END IF
    ELSE
      IF isNull(varDate) OR Trim(varDate) = "" OR varDate = "Null" THEN
        amDate = "Null"
      ELSE
        amDate = "'" & Day(DateValue(varDate)) & "/" & Month(DateValue(varDate)) & "/" & Year(DateValue(varDate)) & " " & TimeValue(varDate) & "'"
      END IF
    END IF
  END FUNCTION

Note - you may not want the:

IF DatabaseType = iDT_SQLServer THEN

condition, im sure you can work it out from here...

Wind is your friend
Matt
www.elitemarquees.com.au
  #6 (permalink)  
Old May 5th, 2008, 11:48 AM
Authorized User
Points: 483, Level: 7
Points: 483, Level: 7 Points: 483, Level: 7 Points: 483, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2007
Location: Edgewood, NY, USA.
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am still confused how to change below:
<%
str1 = month(Date)
str2 = day(date)
str3 = year(date)
datestrs = str3 & "-" & str1 & "-" & str2
%>

to call in the date from my database from the field called "Date_Entered"

I See the the text "Date" calls in the CURRENT Date. i dont want that!

  #7 (permalink)  
Old May 5th, 2008, 12:00 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

First off, this post is off topic. The OP wanted to know how to insert dates into MySQL, not return a formatted date from MySQL.

This will work:

SELECT Date_Format(Date_Entered, '%d-%m-%y')
FROM table

The column returned will be dd-mm-yyyy. (Switch the formatting flags to get a different type of format such as mm-dd-yyyy)

-Doug

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
  #8 (permalink)  
Old May 5th, 2008, 01:26 PM
Authorized User
Points: 483, Level: 7
Points: 483, Level: 7 Points: 483, Level: 7 Points: 483, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2007
Location: Edgewood, NY, USA.
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is mr sql statement:
sSQL="SELECT *, DATE_FORMAT('Date_Entered', '%Y %M, %D') FROM unsolved ORDER BY Date_Entered ASC"

it doesnt do anything to the date Date still 5/5/2008
??

  #9 (permalink)  
Old May 5th, 2008, 01:42 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Per the offical MySQL documentation that should work: http://dev.mysql.com/doc/refman/5.0/...functions.html

Something I do notice is that you are passing Date_Entered in as a string literal and not a column reference: sSQL="SELECT *, DATE_FORMAT('Date_Entered', '%Y %M, %D') FROM unsolved ORDER BY Date_Entered ASC"

It has been awhile since I have used MySQL but I am pretty sure this is incorrect.

-Doug

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP insert into sql date problem markd Classic ASP Databases 1 February 18th, 2006 10:43 AM
trigger to insert current date on insert kev_79 SQL Server 2000 3 January 23rd, 2006 05:58 PM
MySQL DB insert ASP problem crmpicco Classic ASP Databases 4 March 10th, 2005 10:32 AM
Time & date insert and display in myql/asp page karib MySQL 3 June 30th, 2004 05:29 AM
insert system date. no date and time jimmy Access VBA 6 November 20th, 2003 01:11 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.