Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Simply Query - Help Please

Author  Topic 

Paddy78
Starting Member

2 Posts

Posted - 2011-10-13 : 07:11:47
Hi,

I am using a CASE statement in a simple SQL Query as follows:

CASE WHEN a.Column1 = 'X' THEN a.Column2
ELSE 'N/A'
END AS 'Column Name',

Column2 is a Date, so if Column1 = 'X' the date should be displayed. If Column1 != 'X' then I would like to display 'N/A' as text.

With my limited SQL skill, I understand that I need to convert the result into text, but I just can't get my head around how to do this?

Please could you help me?

Cheers,

Paddy

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-13 : 07:15:02
[code]
........
CASE WHEN a.Column1 = 'X' THEN CAST(varchar(30),a.Column2,121)
ELSE 'N/A'
END AS 'Column Name'
..
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Grifter
Constraint Violating Yak Guru

274 Posts

Posted - 2011-10-13 : 07:16:04
quote:
Originally posted by Paddy78

Hi,

I am using a CASE statement in a simple SQL Query as follows:

CASE WHEN a.Column1 = 'X' THEN a.Column2
ELSE 'N/A'
END AS 'Column Name',

Column2 is a Date, so if Column1 = 'X' the date should be displayed. If Column1 != 'X' then I would like to display 'N/A' as text.

With my limited SQL skill, I understand that I need to convert the result into text, but I just can't get my head around how to do this?

Please could you help me?

Cheers,

Paddy



Hi
I think you need to use

cast(a.column2 AS string) AS YourDate
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-13 : 07:19:22
there's no string datatype in t-sql it should be varchar/char

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Grifter
Constraint Violating Yak Guru

274 Posts

Posted - 2011-10-13 : 08:41:21
quote:
Originally posted by visakh16

there's no string datatype in t-sql it should be varchar/char

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





So it is! Thanks for correction.

G
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-13 : 10:08:46
quote:
Originally posted by Grifter

quote:
Originally posted by visakh16

there's no string datatype in t-sql it should be varchar/char

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





So it is! Thanks for correction.

G


wc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Paddy78
Starting Member

2 Posts

Posted - 2011-10-13 : 10:48:23
And thank you both for your replies!

Worked like a charm!
Go to Top of Page
   

- Advertisement -