Do not use on any
shared computer
May 17, 2008 10:46am pdt
02.29.2008 at 10:48AM PST, ID: 23204667
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

MySQL datetime insert into SQL Server datetime using PHP
Tags: PHP
We have a MySQL v5 database that sits behind a website on a web server.

A PHP script is used to take records and insert them into an SQL Server 2000 database that resides on another server. The PHP script uses the free TDS driver.

We are having a problem with one field which is a datetime field. The datetime field does not insert into the SQL Server database correctly, even though the corresponding SQL Server field is also datetime.

We get NULL inserts for this field.

Can you help? Thanks.

Start your free trial to view this solution
Question Stats
Zone: Microsoft
Question Asked By: Annu
Solution Provided By: angelIII
Participating Experts: 4
Solution Grade: A
Views: 555
Translate:
Loading Advertisement...
02.29.2008 at 10:51AM PST, ID: 21016304

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.29.2008 at 11:01AM PST, ID: 21016400

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.29.2008 at 11:09AM PST, ID: 21016487

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.29.2008 at 12:21PM PST, ID: 21017165

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.29.2008 at 03:01PM PST, ID: 21018445

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.29.2008 at 03:55PM PST, ID: 21018764

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.29.2008 at 11:06PM PST, ID: 21020476

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
02.29.2008 at 10:51AM PST, ID: 21016304

Rank: Wizard

I know what's wrong with the posted query...No wait, I don't!
 
02.29.2008 at 11:01AM PST, ID: 21016400

Rank: Genius

can you please clarify your problem?
you speak about MySQL, MSSQL, and queries, but nothing really clear...
 
02.29.2008 at 11:09AM PST, ID: 21016487

Rank: Guru

MySQL returns date information in a particular format, while MSSQL is expecting in a different, particular format.

According to MSSQL 2000 BOL (sorry...a little outdated, but should be relevant):

-----------------------------------------
SQL Server recognizes date and time data enclosed in single quotation marks (') in these formats:

Alphabetic date formats (for example, 'April 15, 1998')

Numeric date formats (for example, '4/15/1998', 'April 15, 1998')

Unseparated string formats (for example, '19981207', 'December 12, 1998')
-----------------------------------------

Below I have shown an example of how MySQL interprets date inputs, and how you can expect it to look in a query using a simple field select.  I would recommend you select from the MySQL table in such a way as to create the proper format that MSSQL demands.  See the DAY(), MONTH(), and YEAR() functions in MySQL to parse the date, and use CONCAT() to build a new string.

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
mysql> CREATE TABLE `table1` (
    ->   `field1` int(11) unsigned NOT NULL auto_increment,
    ->   `field2` datetime default NULL,
    ->   PRIMARY KEY  (`field1`)
    -> ) ENGINE=MyISAM;
Query OK, 0 rows affected (0.01 sec)
 
mysql> insert into table1 (field2) values ('2008-06-01'),('06-02-2008'),('03-06-2008'),('4/6/2008'),('6/5/2008');
Query OK, 5 rows affected, 4 warnings (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 4
 
mysql> select * from table1;
+--------+---------------------+
| field1 | field2              |
+--------+---------------------+
|      1 | 2008-06-01 00:00:00 |
|      2 | 0000-00-00 00:00:00 |
|      3 | 0000-00-00 00:00:00 |
|      4 | 0000-00-00 00:00:00 |
|      5 | 0000-00-00 00:00:00 |
+--------+---------------------+
5 rows in set (0.00 sec)
Open in New Window
 
02.29.2008 at 12:21PM PST, ID: 21017165

Rank: Master

that's because the datetime fields are done differently.

after getting the date, using php convert the format to the format of the MSSQL datetime
 
02.29.2008 at 03:01PM PST, ID: 21018445
Thanks for all the responses.  Here is more information on the problem:

We already translate the date into a format acceptable to MSSQL.  For example, we put a record in with todays date, the php script outputs:

INSERT INTO thetable datetimecompleted values('29/02/2008 15:34:23')

which is then run using query command.

This results in a new row with NULL in the datetimecompleted column.

If I copy and paste this same line in to the MSSQL query editor gui thingy and then execute it, it works, i.e. it correctly enters the date.
 
02.29.2008 at 03:55PM PST, ID: 21018764

Rank: Wizard

Try the month before the day:
INSERT INTO thetable datetimecompleted values('02/29/2008 15:34:23')
 
02.29.2008 at 11:06PM PST, ID: 21020476

Rank: Genius

>This results in a new row with NULL in the datetimecompleted column.
I cannot believe that, unless you have a trigger on the table..

in MySQL, if the value is not in the correct format or an out-of-range value, it will silently put 0000-00-00 instead.
in MS SQL, if the value is not in the correct format or an out-of-range value, it will raise an error (and the insert will fail/rollback).

note:
INSERT INTO thetable datetimecompleted values('29/02/2008 15:34:23')
uses implicit data type conversion, which is BAD behaviour.

better use this explicit data type conversion method:
INSERT INTO thetable (datetimecompleted) SELECT CONVERT(datetime, '29/02/2008 15:34:23', 103)
Accepted Solution
 
 
03.03.2008 at 02:17AM PST, ID: 21030188
<Grading Comments>
You have written
INSERT INTO thetable (datetimecompleted) SELECT CONVERT(datetime, '29/02/2008 15:34:23', 103)
I think it should be
INSERT INTO thetable (datetimecompleted) VALUES CONVERT(datetime, '29/02/2008 15:34:23', 103)
</Grading Comments>

if you use VALUES, then you needed additional () around the values:
INSERT INTO thetable (datetimecompleted) VALUES (CONVERT(datetime, '29/02/2008 15:34:23', 103) )

anyhow, SELECT will also work.
 
 
 
20080206-EE-VQP-25 / EE_QW_2_20070628