Using CASE to convert NULL to convert NULL to a varchar with datetime and varchar field

Posted on 2007-08-07
Last Modified: 2010-04-15
I'm obviously novice at SQL.  I am creating a stored procedure and I am creating a tempory table and using this statement as one of the insert values.

(case when Checks.ditronicsSettledate = NULL then CONVERT(VARCHAR(11),'NA')
 else (SELECT CONVERT(VARCHAR(11),Checks.ditronicsSettleDate, 101) AS [MM/DD/YYYY])

what I am trying to do is if a Checks.dtironicsSettledate is NULL I want to store the value NA , else I want to store the date in the character field.

Chekcs.ditronicsSettledate is a datetime field. and the field I am using this statement to insert into is a varchar(11) field.

The sp compiles fine but when I run it , NULL is not replaced by NA. I tried the statement with out the CONVERT(varchar(11),NA) as just Checks.ditronicsSettledate = NULL than 'NA' but I recieved errors regarding not being able to convert and int to a varchar.

Question by:svasilakos
    LVL 75

    Expert Comment

    by:Aneesh Retnakaran
    (case when Checks.ditronicsSettledate IS NULL then 'NA'
     else (SELECT CONVERT(VARCHAR(11),Checks.ditronicsSettleDate, 101) AS [MM/DD/YYYY])


    Author Comment

    Of course not THEN. I must be tired. Thank you very, very much
    LVL 42

    Expert Comment

    Definately cannot use  somevalue = NULL.   That will always test false because NULL is not equal to anything. Try

    (case when Checks.ditronicsSettledate IS NULL then
        CONVERT(VARCHAR(10),Checks.ditronicsSettleDate, 101)
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    you don't need CASE:

    COALESCE( CONVERT(VARCHAR(11),Checks.ditronicsSettleDate, 101) ,'NA') AS [MM/DD/YYYY],
    LVL 5

    Expert Comment

    Try this one
    (SELECT CONVERT(VARCHAR(11), ISNULL(Checks.ditronicsSettleDate, 'NA'), 101) AS [MM/DD/YYYY])

    I think this should work.

    LVL 42

    Accepted Solution

    Agree, COALESCE or ISNULL is preferable to CASE.  Please disregard my proposed solution.

    Featured Post

    PRTG Network Monitor: Intuitive Network Monitoring

    Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

    Join & Write a Comment

    Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
    Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
    Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
    This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

    754 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

    19 Experts available now in Live!

    Get 1:1 Help Now