Solved

SELECT statement with CASE and CAST

Posted on 2014-09-18
3
238 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:Kent Olsen
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:
Kent Olsen 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

685 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