Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Silly Date Formatting Question

Status
Not open for further replies.

AccessUser22

Technical User
Jan 23, 2003
168
US
Hello,

I have a field in a query that I want to either show a dashed value '---' or a date. So I use this:

CASE WHEN [RegStatus] = 4 THEN '---' ELSE [Step7Date] END AS Step7

Problem is, it tells me "Conversion of the char data type to date time has resulted in an out of range value". If I set it up like this:

CASE WHEN [RegStatus] = 4 THEN '---' ELSE CAST([Step7Date] as nvarchar) END AS Step7

the date gets formatted as Apr 20, 2002 12:00:00 AM. What do I need to do if I want the date to be formatted as '4/20/02'?
 
This should do it (not tested)

Code:
CASE WHEN [RegStatus] = 4 THEN '---' ELSE convert(varchar(10), [Step7Date], [b]101[/b]) END AS Step7

If SQL Server encounters two data types in the same expression (without any casting for compatibility) it follows what is called 'Data Type Precedence'. So as you found, you need to convert your Step7Date to varchar. Format 101 will give you the display format you need.

Here is some info on data type precedence:



Hope it helps,

Alex


Ignorance of certain subjects is a great part of wisdom
 
You could try convert(varchar(10),step7Date,101)

Or .. do it in the front end.
 
Code:
CASE WHEN [RegStatus] = 4
     THEN '---'
     ELSE CONVERT(varchar(10), [Step7Date], 101)
     END AS Step7


Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
MVP VFP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top