Custom Expression in SQL

Discussion in 'SQL Exams' started by Fergal1982, Oct 11, 2007.

  1. Fergal1982

    Fergal1982 Petabyte Poster

    4,196
    172
    211
    Does anyone know how to set up a custom expression using T-SQL?

    I basically have a query that returns a bit column. I need to provide custom output from the query so that, if the value is 1 it returns a set string.

    Does that make any sense?
     
    Certifications: ITIL Foundation; MCTS: Visual Studio Team Foundation Server 2010, Administration
    WIP: None at present
  2. Ossian

    Ossian Bit Poster

    38
    0
    16
    Define a custom function e.g. ProcessBit taking the bit value as a parameter

    e.g.
    CREATE FUNCTION dbo.ProcessBit( @booVal bit)
    RETURNS varchar(20)
    AS
    BEGIN
    IF @booVal --should default to true
    RETURN 'True String'
    ELSE
    RETURN 'False String'

    END


    then

    SELECT ProcessBit(fieldname) AS "Result" FROM tablename

    Note this is not checked code, just a rough example!
     
    Certifications: MCT, MCSE/A, MCDBA, MCDST, MCITP, Sec+
    WIP: Not sure....
  3. Fergal1982

    Fergal1982 Petabyte Poster

    4,196
    172
    211
    Yeah I thought of doing that. Didnt want to use stored procedures if i could avoid it.

    Dont worry too much though, I realised I was being stupid and doing the wrong query, so its a moot point.
     
    Certifications: ITIL Foundation; MCTS: Visual Studio Team Foundation Server 2010, Administration
    WIP: None at present

Share This Page

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.