Character conversion failed

This query is doing 99% of what I want but group by date. When I try to format the date to the way I wan it grouped it throws the error below. In the case statement below, I removed the code LEFT(d.Thedate,7) in order to return results. If I were to add that code back to this query I get the same error below. I'm stuck!

Msg 241, Level 16, State 1, Line 13
Conversion failed when converting date and/or time from character string.


DECLARE @ReportMode INT,
        @startDate  DATETIME,
        @endDate    DATETIME,
        @agencyID   VARCHAR(100),
        @divisionID VARCHAR(10);

SET @ReportMode = 2;
SET @startDate = '2009-01-01';
SET @endDate = '2009-12-31';
SET @divisionID = '5';
SET @agencyID = '0';

SELECT a.[id]       AS agency_id,
       a.agency_name,
       COUNT(ti.id) AS tran_total,
       reportdate
FROM   (SELECT a.[ID],
               a.agency_name,
               CASE
                 WHEN @ReportMode  = 3 THEN LEFT(d.thedate, 4)
                 WHEN @ReportMode >= 2 d.thedate
                 WHEN @ReportMode >= 1 THEN LEFT(d.thedate, 9)
               END AS reportdate
        FROM   agencies AS a,
               (SELECT d AS thedate
                FROM   (SELECT DISTINCT tidate.dreceived AS d
                        FROM   transactions_incoming tidate
                        WHERE  tidate.dreceived BETWEEN @startDate AND @endDate) AS x) AS d
        WHERE  a.[id] IN ( CASE
                             WHEN @agencyID = '0' THEN a.[ID]
                             ELSE (SELECT orderid
                                   FROM   Splitorderids(@agencyID) AS agencylist)
                           END )
               AND a.[agency_division_id] IN ( CASE
                                                 WHEN @divisionID = '0' THEN a.[agency_division_id]
                                                 ELSE (SELECT orderid
                                                       FROM   Splitorderids(@divisionID) AS divisionlist)
                                               END )) AS a
       LEFT OUTER JOIN (SELECT *
                        FROM   transactions_incoming) AS ti
         ON a.id = ti.agency_id
            AND ti.dreceived = a.reportdate
GROUP  BY a.id,
          a.agency_name,
          reportdate
ORDER  BY reportdate,
          a.id;

Open in new window

jgoodaleAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rajkumar GsSoftware EngineerCommented:
Try change

LEFT(d.Thedate,7)

to

LEFT(LTRIM(CAST(d.Thedate AS VARCHAR(20))),7)

Raj
0
jgoodaleAuthor Commented:
Msg 241, Level 16, State 1, Line 13
Conversion failed when converting date and/or time from character string.
0
Rajkumar GsSoftware EngineerCommented:
What result you meant to display by LEFT(d.Thedate,7) ?

Raj
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Rajkumar GsSoftware EngineerCommented:

Since I am from mobile, can't read code-snipplet fully

Raj
0
jgoodaleAuthor Commented:
Same result

Msg 241, Level 16, State 1, Line 13
Conversion failed when converting date and/or time from character string.

Here is an example of the dataset that returns of no formatting is used on the column

55      Downey PD      0      2009-01-01 00:00:00.000
55      Downey PD      18      2009-01-02 00:00:00.000
55      Downey PD      0      2009-01-03 00:00:00.000
55      Downey PD      0      2009-01-04 00:00:00.000
0
jgoodaleAuthor Commented:
DECLARE @ReportMode INT,
        @startDate  DATETIME,
        @endDate    DATETIME,
        @agencyID   VARCHAR(100),
        @divisionID VARCHAR(10);

SET @ReportMode = 2;
SET @startDate = '2009-01-01';
SET @endDate = '2009-12-31';
SET @divisionID = '5';
SET @agencyID = '0';

SELECT a.[id]       AS agency_id,
       a.agency_name,
       COUNT(ti.id) AS tran_total,
       reportdate
FROM   (SELECT a.[ID],
               a.agency_name,
               CASE
                 WHEN @ReportMode  = 3 THEN LEFT(d.thedate, 4)
                 WHEN @ReportMode >= 2 d.thedate
                 WHEN @ReportMode >= 1 THEN LEFT(d.thedate, 9)
               END AS reportdate
        FROM   agencies AS a,
               (SELECT d AS thedate
                FROM   (SELECT DISTINCT tidate.dreceived AS d
                        FROM   transactions_incoming tidate
                        WHERE  tidate.dreceived BETWEEN @startDate AND @endDate) AS x) AS d
        WHERE  a.[id] IN ( CASE
                             WHEN @agencyID = '0' THEN a.[ID]
                             ELSE (SELECT orderid
                                   FROM   Splitorderids(@agencyID) AS agencylist)
                           END )
               AND a.[agency_division_id] IN ( CASE
                                                 WHEN @divisionID = '0' THEN a.[agency_division_id]
                                                 ELSE (SELECT orderid
                                                       FROM   Splitorderids(@divisionID) AS divisionlist)
                                               END )) AS a
       LEFT OUTER JOIN (SELECT *
                        FROM   transactions_incoming) AS ti
         ON a.id = ti.agency_id
            AND ti.dreceived = a.reportdate
GROUP  BY a.id,
          a.agency_name,
          reportdate
ORDER  BY reportdate,
          a.id;
0
Rajkumar GsSoftware EngineerCommented:
About

  ...CASE
                 WHEN @ReportMode  = 3 THEN LEFT(d.thedate, 4)
                 WHEN @ReportMode >= 2 d.thedate
                 WHEN @ReportMode >= 1 THEN LEFT(d.thedate, 9)
               END AS reportdate...

It's logic ?

If you want to format date, removing time part , try

CONVERT(VARCHAR(10), urDateField, 102)

Raj
0
jgoodaleAuthor Commented:
Still getting a conversion error when I changed the code to read:

 WHEN @ReportMode >= 2 THEN Left(CONVERT(VARCHAR(10), d.thedate, 102),7)
0
Rajkumar GsSoftware EngineerCommented:
>> When I try to format the date to the way I wan it grouped it throws the error below.

Let me know what format you want. I think it would be better so that I can correct it.
I mean by eg:- like 01/Jan/2010 or something like that

As per your query, for each reportmode, you need different formats, right ? Tell me the format by example for each mode.

Raj
0
jgoodaleAuthor Commented:
there are 3 formats, Day, Month and Year:

2010-04-01
2010-04
2010
0
Rajkumar GsSoftware EngineerCommented:
Instead of LEFT function, use this in the query

2010-04-01  --> CONVERT(VARCHAR(10), d.thedate, 102)
2010-04  --> CONVERT(VARCHAR(7), d.thedate, 102)
2010    --> YEAR(d.thedate)

Hope this helps
Raj
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jgoodaleAuthor Commented:
I ended up having to use a different solution since I couldn't avoid the error. Thanks for your help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.