Solved

SELECT statement with CASE and CAST

Posted on 2014-09-18
3
230 Views
Last Modified: 2014-09-22
i am trying to return a string value when a timestamp field value is null.  below is my select statement.  I am getting an error message on the data type.  Can I use the cast() as varchar to resolve?

select
first_name,
last_name,
case when table.event_id = 5 and table.timestamp isnull then 'n/a' else table.timestamp end as ad_timestamp

from XXXXXX
0
Comment
Question by:szadroga
  • 2
3 Comments
 
LVL 45

Expert Comment

by:Kdo
ID: 40330359
All possible values returned in the CASE construct have to be the same datatype.

Cast the datetime/timestamp to any character (string) type and you'll be fine.


Good Luck!
Kent
0
 

Author Comment

by:szadroga
ID: 40330364
where do I perform the cast() in my syntax?  I cannot get it to work
0
 
LVL 45

Accepted Solution

by:
Kdo earned 500 total points
ID: 40330370
select
first_name,
last_name,
case when table.event_id = 5 and table.timestamp isnull then 'n/a'
else cast (table.timestamp as varchar (20)) end as ad_timestamp
from XXXXXX
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
How to increase the row limit in Jasper Server.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

12 Experts available now in Live!

Get 1:1 Help Now