Solved

case expression with date rages

Posted on 2014-01-30
4
238 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
  • 2
4 Comments
 
LVL 65

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 65

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:
ScottPletcher 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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SRSS - IF statements in Report Builder 3.0 to sum of number of items in each order 8 30
Calculated columns 13 61
TSQL previous 5 23
SQL Server stored proc 2 12
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

911 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

23 Experts available now in Live!

Get 1:1 Help Now