Solved

What's wrong with this query?

Posted on 2016-10-19
9
50 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 51

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 51

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 82

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

708 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

10 Experts available now in Live!

Get 1:1 Help Now