SQL Server Performance

show user defined value not actually in table

Discussion in 'SQL Server 2005 General Developer Questions' started by litu.deb, Apr 19, 2007.

  1. litu.deb Member


    the foloowing table is emp table

    name salary

    X 10000

    Y 20000

    Z 2000



    SELECT 'The SAL is VERY LESS' + CAST(SAL AS varchar(12)) as salary
    FROM EMP
    WHERE SAL <5000
    GO

    RESULT:

    salary

    The SAL is VERY LESS2000

    BUT I WANT TO SHOW ONLY 'The SAL is VERY LESS' INSTEAD OF 'The SAL is VERY LESS2000'
  2. Adriaan New Member

  3. litu.deb Member

    the statement will create a new column.
    But I don't want any new column be created.
    I want to show the original column as it is.
  4. Adriaan New Member

    You seem to be confused about the very basics of T-SQL.

    SELECT does not create any columns. You are just selecting data from a table, and returning a string for each row where SAL<5000.

    Please educate yourself on the basics, before posting questions.
  5. litu.deb Member

    sorry ..Actually I have written in wrong way.
    I should say that ur sql statement showing new column name.

    But I don't want to show any new column.I want to show the
    different attribute value from original value


    quote:Originally posted by Adriaan

    You seem to be confused about the very basics of T-SQL.

    SELECT does not create any columns. You are just selecting data from a table, and returning a string for each row where SAL<5000.

    Please educate yourself on the basics, before posting questions.
  6. Adriaan New Member

    Then add an alias for the expression:

    SELECT 'The SAL is VERY LESS' AS Salary
    FROM EMP
    WHERE SAL <5000

    Note that you'll get the same line for each row in EMP where SAL<5000.

  7. litu.deb Member

    I think I m not clear to u.
    let me clear.

    supoose in the following emp table .

    name sal
    x 60,000
    y 10,000

    here sal as varchar(12).
    I want result like this

    name sal
    x 60,000
    y very less salary

    don't want to show any new extra column


    quote:Originally posted by Adriaan

    Then add an alias for the expression:

    SELECT 'The SAL is VERY LESS' AS Salary
    FROM EMP
    WHERE SAL <5000

    Note that you'll get the same line for each row in EMP where SAL<5000.


  8. Adriaan New Member

    Then use CASE, which can evaluate the salary:

    SELECT name, CASE WHEN SAL<5000 'Very less salary' ELSE CAST(SAL AS VARCHAR(100)) END AS Salary
    FROM EMP

    If you put the comparison in the WHERE clause, then you see only rows where SAL<5000.
  9. litu.deb Member

    showing the following error msg:

    Server: Msg 170, Level 15, State 1, Line 1
    Line 1: Incorrect syntax near 'Very less salary'.


    quote:Originally posted by Adriaan

    Then use CASE, which can evaluate the salary:

    SELECT name, CASE WHEN SAL<5000 'Very less salary' ELSE CAST(SAL AS VARCHAR(100)) END AS Salary
    FROM EMP

    If you put the comparison in the WHERE clause, then you see only rows where SAL<5000.
  10. Adriaan New Member

    Read BOL for CASE ...<br /><br />My query was missing the THEN keyword - mistakes happen.[<img src='/community/emoticons/emotion-2.gif' alt=':D' />]
  11. litu.deb Member

    Thanks a lot for your help..really appreciate yor guidance.<br /><br />thanks once again.<br /><br />regds,<br />litu deb<br /><br /><blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote"><i>Originally posted by Adriaan</i><br /><br />Read BOL for CASE ...<br /><br />My query was missing the THEN keyword - mistakes happen.[<img src='/community/emoticons/emotion-2.gif' alt=':D' />]<br /><hr height="1" noshade id="quote"></font id="quote"></blockquote id="quote">

Share This Page