What does this error mean?

I start things off by creating a temporary table like this:
$sql_50 = "CREATE TABLE #tmp_Results
      (
            id      int IDENTITY,
            str_ProjectName varchar(150),
            str_Tool varchar(150),
            str_FullName varchar(150),
            str_ProjectType varchar(150),
            str_Description varchar(max),
            str_TargetCompletionDate varchar(50),
            dec_PercentageCompleted decimal(10,2),
            str_ProjectId varchar(150),
            int_ProjectRank int
      )";

Then I run this query:

insert into #tmp_Results (
str_ProjectName,
str_Tool,
str_FullName,
str_Description,
str_ProjectType,
str_TargetCompletionDate,
dec_PercentageCompleted,
str_ProjectId,
 int_ProjectRank)
 VALUES (
 'SA Pipeline Workbook',
 'SA Portal (MVC)',
 ' ',
 'Automate the SA Pipeline Report that is currently in xls form. Data currently pulled from multiple datasources including NETWorkflow.',
 'New Feature',
 '',
 '',
 '140618b',
 '999')

...and I get this error:

Error converting data type varchar to numeric.

Pretty sure it has something to do with my decimal field, but I don't know what or how to fix it.

Thoughts?
Bruce GustPHP DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Data DudeCommented:
At first glance...
 Third line from the bottom, decimal data types can't hold empty strings --> '' <-- , so change that to a NULL.

 the '999' should be 999 without the quote marks, as you're inserting into an int field, but I'm guessing implicit conversion would be performed and this would not result in an error.  
Bruce GustPHP DeveloperAuthor Commented:
Jim, I think you've nailed it, but how do I substitute NULL on the fly?

Here's what I'm doing:

foreach($result_array as $project)
		{
			if($project['dec_PercentageCompleted']=="")
			{
				$dec_Percentage=NULL;
			}
			else
			{
				$dec_Percentage=$project['dec_PercentageCompleted'];
			}
		$sql_11="insert into #tmp_Results 
		(str_ProjectName, str_Tool, str_FullName, str_Description, str_ProjectType, str_TargetCompletionDate, dec_PercentageCompleted, str_ProjectId, int_ProjectRank)
		VALUES
		('$project[str_ProjectName]',
		'$project[str_Tool]', 
		'$project[str_Full_Name_D]',
		'$project[str_Description]',
		'$project[str_ProjectType]',
		'$project[str_TargetCompletionDate]',
		'$dec_Percentage',
		'$project[str_ProjectId]', 		
		'$project[int_ProjectRank]')";
		$mssql_stmt_11=$mssql_pdo->query($sql_11);
		//echo $sql_11.'<br>';
			if(!$mssql_stmt_11)
			{
				echo "no query";
			}
		}

Open in new window


I'm trying to accommodate that situation where there's no value, yet even when I throw in "NULL," it doesn't seem to do anything.

Thoughts?
Jim HornMicrosoft SQL Server Data DudeCommented:
I'm not familiar with the coding language you're using (what is it?), so on that point I'll step back to encourage other experts to respond.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Bruce GustPHP DeveloperAuthor Commented:
PHP
Marco GasiFreelancerCommented:
You could just set the default value in the database as 0,00. This way it should just set the value to default when you're pasing Null or empty string
Brian TaoSenior Business Solutions ConsultantCommented:
Changed lines: #5, #20 and #22
foreach($result_array as $project)
		{
			if($project['dec_PercentageCompleted']=="")
			{
				$dec_Percentage="NULL";  // <=== in PHP, the NULL should be a string
			}
			else
			{
				$dec_Percentage=$project['dec_PercentageCompleted'];
			}
		$sql_11="insert into #tmp_Results 
		(str_ProjectName, str_Tool, str_FullName, str_Description, str_ProjectType, str_TargetCompletionDate, dec_PercentageCompleted, str_ProjectId, int_ProjectRank)
		VALUES
		('$project[str_ProjectName]',
		'$project[str_Tool]', 
		'$project[str_Full_Name_D]',
		'$project[str_Description]',
		'$project[str_ProjectType]',
		'$project[str_TargetCompletionDate]',
		$dec_Percentage,
		'$project[str_ProjectId]', 		
		$project[int_ProjectRank])";  // <=== removed the single quotes around $dec_Percentage and $project[int_ProjectRank]
		$mssql_stmt_11=$mssql_pdo->query($sql_11);
		//echo $sql_11.'<br>';
			if(!$mssql_stmt_11)
			{
				echo "no query";
			}
		}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott PletcherSenior DBACommented:
I suggest trying code below, allowing SQL to do the work of replacing the blank with a NULL:

...
    VALUES(
...
      '$project[str_TargetCompletionDate]',
      NULLIF('$dec_Percentage', ''),
      '$project[str_ProjectId]',
...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.