Solved

What's wrong with this query?

Posted on 2016-10-19
9
53 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
9 Comments
 
LVL 52

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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 52

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 108

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

948 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now