Jump to content

[SQL] Manipulate outcoming data

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
SterAllures

SterAllures

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 203 posts
Hiya CC'ers,

I'm currently working on SQL Server 2005 reporting services. So this means I want to present data to people in an interesting and easy readable way.

But I've got a little problem and already searched but I don't think I enter the write tags when I search so I hope you guys can help me.

My problem is that I have a database in which I got different tables and one of those tables is called Persons.
In the person table there is a row called sex(male/female) now it doesnt say male or female but 0 or 1.

Well when I want to pull this data out and present it I want it to say like 0 = male and 1 = female. but how can I change this outcoming integer to a string?
I Don't want to change the Database!
so in the database it still says 0 or 1.

I really hope someone can help me!

Thanks In Advance!
//SterAllures
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Use the Case keyword as part of your select query.
SELECT CASE sex
                 WHEN 0 THEN "MALE"
                 WHEN 1 THEN "FEMALE"
               AS gender
FROM persons

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
SterAllures

SterAllures

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 203 posts
Thanks for your fast reply!

I'll try it later today and let you know if it worked!! but thanks for the effort already!
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#4
SterAllures

SterAllures

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 203 posts
Well I've tried the following
SELECT CASE s.sex
           WHEN 0 THEN "Male"
           WHEN 1 THEN "Female"
      As Gender

FROM staff s

It gave me the following error: Incorrect syntax near the keyword 'As'

They ment the As in the sentence "As Gender"

I've also tried to do 'Female' and even '0' but it didn't work at all.

Any solution? Great thing by the way the CASE statement =)
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#5
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
I thought CASE statements need the word END at the end. Try this:

SELECT CASE s.sex
           WHEN 0 THEN "Male"
           WHEN 1 THEN "Female"
           END
      As Gender

FROM staff s

Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#6
SterAllures

SterAllures

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 203 posts
Wow that was with outta doubt the fastest reply ever :P.

and I tried it and it gave an error but another one so that was a good thing, so after changing the " into ' it worked!!!

I now have the following!

SELECT CASE s.sex
           WHEN 0 THEN 'Male'
           WHEN 1 THEN 'Female'
END
           As "Gender"

FROM staff s

Ow ya btw I forgot the table name was staff and not persons :P
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#7
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
I have witnessed (and posted) much faster responses - mainly in the Games (previously Lounge) areas.

I just copied your code and made the change, hence not picking up on the error. You can't blame me. ;)

Glad it works.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#8
SterAllures

SterAllures

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 203 posts
I'm not blaming you I'm happy you helped me out with this problem so I can get on. well than I change my statement into. That is with outta doubt the most fastest reply I ever got on a thread of mine :P.

But really Thanks a lot Xav and WingedPanther for your fast help!
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#9
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Lol, you're welcome. +rep as necessary. :D
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums