0

Trying to change NULLs in a date field into blank results but it keeps returning a date. I'm doing it in a case when as i need to meet the following criteria:

CASE WHEN closed = 'y' THEN Date ELSE '' END

I've tried casting as varchar and other things it either throws a error or does nothing!

Yuriy Galanter
  • 38,833
  • 15
  • 69
  • 136
GPH
  • 1,817
  • 3
  • 29
  • 50
  • Are you really trying to return a character value? As in, what is happening to the date that is returned - is it just being displayed or something else that is equivalent to a character value? – Joe May 29 '13 at 15:05
  • There is no such thing as a blank date. You either have NULL or a date value. Where did you try casting it as a varchar? – BlargleMonster May 29 '13 at 15:05
  • CONVERT, not cast, is probably your issue I'd guess. – Joe May 29 '13 at 15:06

2 Answers2

1

You should convert it to a varchar:

CASE WHEN closed = 'y' THEN LEFT(CONVERT(VARCHAR, Date, 120), 10) ELSE '' END

See How to convert DateTime to VarChar for more information.

Community
  • 1
  • 1
Joe
  • 62,789
  • 6
  • 49
  • 67
1

A column in your result can only contain one type, it can't be both a date and a text field. You could convert your date to a string providing you no longer need it to be a date like so:

CASE WHEN closed = 'y' 
THEN CAST(Date AS VARCHAR) 
ELSE '' END
Dave Sexton
  • 10,768
  • 3
  • 42
  • 56