Solved

What's wrong with this query?

Posted on 2016-10-19
9
76 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 57

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 50 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 57

Assisted Solution

by:Julian Hansen
Julian Hansen earned 100 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 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 50 total points
ID: 41850898
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 300 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

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.
This article discusses how to implement server side field validation and display customized error messages to the client.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

696 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