Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

What's wrong with this query?

Posted on 2016-10-19
9
Medium Priority
?
83 Views
Last Modified: 2016-10-27
Here's how this query appears in PHP:

function queuedCount($queuedate=''){
  $sql="select count(*) num from acdcallqueue
             where cast(created as date) = "
            .($queuedate==''?"cast(getdate() as date)":"'$queuedate'");
    return pconnectdb($sql);
}

Open in new window


I want to run this query in MSSQL Studio. I wrote it like this:

select count(*) num from [PCAR_Data].[dbo].[ACDCallQueue]
where cast(created as date) = ".(2016-08-29 03:40:36.033==''?" cast(2016-08-29 03:40:36.033() as date)":"'2016-08-29 03:40:36.033'")

I'm getting a message that says, "Incorrect syntax near 'cast'. Msg 105, Level 15, State 1, Line 3 Unclosed quotation mark after the character string ')'.

Not sure what that means or how to fix it.

Thoughts?
0
Comment
Question by:brucegust
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 59

Expert Comment

by:Julian Hansen
ID: 41850580
function queuedCount($queuedate=''){
  $sql="select count(*) num from acdcallqueue
             where cast(created as date) = "
            .($queuedate==''?"cast(getdate() as date)":"'$queuedate'");
echo "Query: {$sql}<br/>";
    return pconnectdb($sql);
}

Open in new window


Copy query and paste here.
1
 

Author Comment

by:brucegust
ID: 41850590
 select count(*) num from [PCAR_Data].[dbo].[ACDCallQueue]
 where cast(created as date) = ".(2016-08-29 03:40:36.033==''?" cast(2016-08-29 03:40:36.033() as date)":"'2016-08-29 03:40:36.033'")

Open in new window

0
 
LVL 66

Assisted Solution

by:Jim Horn
Jim Horn earned 200 total points
ID: 41850621
>where cast(created as date) = ".(2016-08-29 03:40:36.033==''?" cast(2016-08-29 03:40:36.033() as date)":"'2016-08-29 03:40:36.033'")
For starters, the date datatype does not contain a time component (datetime does), so the WHERE expression comparing it to a value with a time component will never evaluate to TRUE.

Also I'm unfamilar with PHP so please tell us what ? and : represents.
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 59

Assisted Solution

by:Julian Hansen
Julian Hansen earned 400 total points
ID: 41850642
If that is what the result from this line is
echo "Query: {$sql}<br/>";

Open in new window

Then I am surprised.

We need to see the actual Query that is going to the server. The snippet you posted contains PHP code in it.

Can you just echo the query out and then copy and past the echo'd value.
0
 
LVL 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 200 total points
ID: 41850898
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1200 total points
ID: 41853831
This will help you visualize the query string.
https://iconoun.com/demo/temp_brucegust.php
<?php // demo/temp_brucegust.php
/**
 * https://www.experts-exchange.com/questions/28977482/What's-wrong-with-this-query.html
 */
error_reporting(E_ALL);
echo '<pre>';


function queuedCount($queuedate=''){
    $sql="select count(*) num from acdcallqueue
         where cast(created as date) = "
         .($queuedate==''?"cast(getdate() as date)":"'$queuedate'");

    // JUST RETURN THE SQL STRING SO WE CAN SEE WHAT IT CONTAINS
    // return pconnectdb($sql);
    return $sql;
}

echo PHP_EOL . queuedCount();
echo PHP_EOL . queuedCount('XYZ');

Open in new window

Outputs
select count(*) num from acdcallqueue
         where cast(created as date) = cast(getdate() as date)
select count(*) num from acdcallqueue
         where cast(created as date) = 'XYZ'

Open in new window

0
 

Author Closing Comment

by:brucegust
ID: 41862394
Thank you, guys! I got it done!
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question