Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7879793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:59:05+00:00 2026-06-03T03:59:05+00:00

I am using PHP to query some data on SQL Server 2008 R2 and

  • 0

I am using PHP to query some data on SQL Server 2008 R2 and receiving the following errors:

  • PHP Warning: mssql_query(): message: Conversion failed when converting date and/or time from character string. (severity 16) in /var/www/html/BSC_Entry.php on line 14
  • PHP Warning: mssql_query(): General SQL Server error: Check messages from the SQL Server (severity 16) in /var/www/html/BSC_Entry.php on line 14

Here’s the code block:

  3 <?php // Initialise all database actions
  4     //IP of server
  5     $server = 'x.x.x.x';
  6 
  7     // Connection to MSSQL - possibly use password file
  8     $link = mssql_connect($server, 'user', 'pass');
  9     if (!$link) {
 10         die('Something went wrong while connecting to MSSQL');
 11     }
 12 
 13     // Declare query to return BSC_Name, BSC_Owner and  
 14     $qBSCInfo = mssql_query('SELECT * FROM dbo.BSC_Info;', $link);
 15 
 16 ?>

Initially, I was passing the SQL below as a parameter to mssql_query, but after receiving the errors, created the database view above ‘BSC_Info’ (with appropriate permissions) in case the query was too complex for mssql_query to handle:

SELECT DISTINCT 
    dbo.BSC.BSC_ID,
    dbo.BSC.BSC_Name, 
    dbo.BSC.BSC_Owner, 
    DATEDIFF(M, MAX(CONVERT(DATETIME, LEFT(dbo.BSCDataSet.DatePeriod, 4) 
                      + RIGHT(dbo.BSCDataSet.DatePeriod, 2) + '01')), CONVERT(DATETIME, LEFT(CONVERT(VARCHAR, GETDATE(), 120), 4) + RIGHT(LEFT(CONVERT(VARCHAR, GETDATE(), 
                      120), 7), 2) + '01')) AS Periods_to_Current
FROM dbo.BSC 
LEFT OUTER JOIN dbo.BSCDataSet 
ON dbo.BSC.BSC_ID = dbo.BSCDataSet.BSC_ID
GROUP BY dbo.BSC.BSC_ID, dbo.BSC.BSC_Name, dbo.BSC.BSC_Owner

To clarify, the query works in SQL Server Management Studio returning some fields from a table along with the difference (in months) between the current date and an earlier date (stored in the database as a VARCHAR – YYYYMM format). In order to prevent any issues with partial months, I’ve set the compared dates to the first day of the month. I am sure there’s a more graceful way of doing this, but I have very little SQL Server experience, or PHP for that matter!

Data types are:

  • BSC_ID – numeric(5,0)
  • BSC_Name – varchar(50)
  • BSC_Owner – varchar(50)
  • Periods_to_Current – int

Any help would be much appreciated. Cheers all!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:59:07+00:00Added an answer on June 3, 2026 at 3:59 am

    So there were a number of issues with this code:

    • php.ini file needed the mssql setting: mssql.datetimeconvert = 0
    • Any CONVERT-ed date fields needed the date format 121
    • the original code tried to perform conversions, date arithmetic and MAX() functions on a potentially NULL date field – oops

    I have now amended the code to a more appropriate (and working):

    SELECT 
      dbo.BSC.BSC_ID,
      dbo.BSC.BSC_Name, 
      dbo.BSC.BSC_Owner, 
      CASE maxview.maxDate 
        WHEN NULL THEN NULL 
        ELSE DATEDIFF(M,CONVERT(DATETIME, LEFT(maxview.maxdate, 4)+ RIGHT(maxview.maxdate, 2) + '01', 121),GETDATE()) 
      END 
    FROM dbo.BSC 
    LEFT OUTER JOIN (SELECT BSC_ID, MAX(dbo.BSCDataSet.DatePeriod) as maxDate 
    FROM dbo.BSCDataSet GROUP BY BSC_ID) maxview 
    ON dbo.BSC.BSC_ID = maxview.BSC_ID;
    

    Hopefully this will help someone out!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to use MS SQL Server 2008 to store data of PHP CAKE
I'm using PHP in order to redirect some search query. There is some example
using php, i have a sql query which works fine, dateentry only has the
Im using PHP 5.3 on a linux server and I m trying to query
What's the ASP.NET equivalent of this PHP code? $db = new mysqli(/*some data*/); $db->query('INSERT
I have a PHP Code, that uses a web service to query some data.
I am using simple PHP query to fetch rows from the database, there are
I am using php code to query to a database and the results will
i am trying to pass date to mysql query using php, but i am
I am trying to run a mysql insert query using php exec with parameters.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.