Solved

CASE STATEMENT WHEN DATE IS NULL

Posted on 2014-04-02
6
1,706 Views
Last Modified: 2014-04-02
Pretty new to SQL and I’m attempting to write this CASE statement:
,CASE
      WHEN e_sum.ASSURANCE_BUNDLE_TYPE_ID = 5 THEN 'N/A'
      WHEN e_sum.ASSURANCE_BUNDLE_TYPE_ID = 3 AND e_sum.ESTIMATED_START_DATE IS NULL THEN 'Awaiting'
      ELSE e_sum.ESTIMATED_START_DATE
      END AS [Estimated Start Date Case]

But get this error: Conversion failed when converting date and/or time from character string.


How can I correct this?
0
Comment
Question by:DallasTR
6 Comments
 
LVL 45

Expert Comment

by:Kdo
ID: 39972464
Hi Dallas,

The problem isn't the NULL, per se.  It's that the CASE statement tries to return different data types.

All of the possible returned values must be of the same type.  It looks like you're trying to return a string OR a date.  Modify the structure so that you always return a string or always return a date.


Good Luck,
Kent
0
 
LVL 22

Expert Comment

by:plusone3055
ID: 39972465
\
0
 
LVL 40

Accepted Solution

by:
Sharath earned 250 total points
ID: 39972487
Try this.
CASE
      WHEN e_sum.ASSURANCE_BUNDLE_TYPE_ID = 5 THEN 'N/A'
      WHEN e_sum.ASSURANCE_BUNDLE_TYPE_ID = 3 AND e_sum.ESTIMATED_START_DATE IS NULL THEN 'Awaiting'
      ELSE CONVERT(varchar(30),e_sum.ESTIMATED_START_DATE)
      END AS [Estimated Start Date Case]

Open in new window

0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:DallasTR
ID: 39972488
Understand, but given I am trying to return two different data types it would stand to reason that I'd have to convert the date to a string.  How would this be done for this case statement?
0
 

Author Comment

by:DallasTR
ID: 39972498
Awe see Sharath gave an example...I'll give it a try.
0
 

Author Closing Comment

by:DallasTR
ID: 39972529
This worked perfectly - thanks
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

9 Experts available now in Live!

Get 1:1 Help Now