Solved

case expression with date rages

Posted on 2014-01-30
4
242 Views
Last Modified: 2014-04-09
I am trying to use a case expression with date rages, when i use the cast function I keep getting the
“Conversion failed when converting date and/or time from character string” error. I can only query the database not able to create any temp table.





select B.DisplayName ,count(ORDER_RSN)
,CASE
WHEN ORDER_COMPLETE_DTE BETWEEN Cast('2013-08-22'AS Date) AND Cast ('2013-09-21' AS Date) THEN 'SEP_2013'
--WHEN ORDER_COMPLETE_DTE BETWEEN Cast('2013-09-22' AS Date) AND Cast ('2013-10-21' AS date) THEN 'OCT_2013'
--WHEN ORDER_COMPLETE_DTE BETWEEN Cast('2013-10-22' AS Date) AND Cast ('2013-11-21' AS date) THEN 'NOV_2013'
--WHEN ORDER_COMPLETE_DTE BETWEEN Cast('2013-11-22' AS Date) AND Cast ('2013-12-21' AS date) THEN 'DEC_2013'
--WHEN ORDER_COMPLETE_DTE BETWEEN Cast('2013-12-22' AS Date) AND Cast ('2014-01-21' AS date) THEN 'JAN_2014'
ELSE 'Unknown'
END AS Fiscal_Month
FROM [bsWRK].[dbo].[WODM_ICOMP_Orders] A
  Join [Employee].[dbo].[vAllUsers] B
  on A.PERNR = B.PerNr
0
Comment
Question by:onaled777
[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
  • 2
4 Comments
 
LVL 66

Assisted Solution

by:Jim Horn
Jim Horn earned 334 total points
ID: 39821861
>ORDER_COMPLETE_DTE
What's the data type for this column?  If it's anything other than date, based on the above T-SQL the first time it hits a non-date value it's going to throw that error.

Just for kicks and giggles, try this...

SELECT blah, blah, blah,
   CASE ISDATE(ORDER_COMPLETE_DTE)
     WHEN 1 then   -- Insert your big honkin' CASE block here
     WHEN 0 then 'Not a date - need to figure out what to do here
   END,
-- the rest of your query here

Also

WHEN ORDER_COMPLETE_DTE BETWEEN Cast('2013-10-22' AS Date) AND Cast ('2013-11-21' AS date)

Can probably just do ... BETWEEN '2013-10-22' AND '2013-11-21'
0
 

Author Comment

by:onaled777
ID: 39821982
ORDER_COMPLETE_DTE is Date Type. When I run the ISDATE query I get the following error
"Argument data type date is invalid for argument 1 of isdate function"
0
 
LVL 66

Assisted Solution

by:Jim Horn
Jim Horn earned 334 total points
ID: 39822064
>CASE ISDATE(ORDER_COMPLETE_DTE)
>"Argument data type date is invalid for argument 1 of isdate function"
Fascinating.  Some possibilities are...
there is a typo in the ORDER_COMPLETE_DTE column name
that column is not in either of the tables in your FROM clause
Gremlins
>select B.DisplayName ,count(ORDER_RSN)
btw the GROUP BY B.DisplayName, ORDER_COMPLETE_DTE line is missing from the bottom of your query.
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 166 total points
ID: 39822927
Format YYYY-MM-DD is not 100% safe.  Use YYYYMMDD, which always works, no matter the date and/or language settings.  You don't need or want to cast it as date, since varchar has a lower precedence than the column and the column type might change later.  Finally, always use >= and < rather than BETWEEN with dates/datetimes to make sure you don't miss data or include non-matching data.

Finally, ISDATE() would never be needed on a column that's of datatype date or datetime, since it must be that data type to be stored in a column with that data type!  Likewise, you wouldn't need a check to verify that integer columns contained integer values :-) .


select B.DisplayName ,count(ORDER_RSN)
,CASE
WHEN ORDER_COMPLETE_DTE >= 20130822' AND ORDER_COMPLETE_DTE < '20130922' THEN 'SEP_2013'
WHEN ORDER_COMPLETE_DTE >= 20130922' AND ORDER_COMPLETE_DTE < '20131022' THEN 'OCT_2013'
WHEN ORDER_COMPLETE_DTE >= '20131022' AND ORDER_COMPLETE_DTE < '20131122' THEN 'NOV_2013'
--...
ELSE 'Unknown'
END AS Fiscal_Month
FROM [bsWRK].[dbo].[WODM_ICOMP_Orders] A
  Join [Employee].[dbo].[vAllUsers] B
  on A.PERNR = B.PerNr
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

636 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