Solved

What's wrong with this query?

Posted on 2016-10-19
9
70 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 56

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 65

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 56

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
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.

749 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