How to build an string alias in T-SQL

Last post 05-15-2007 8:16 AM by JRICE. 6 replies.

Sort Posts:

  • How to build an string alias in T-SQL

    05-15-2007, 7:52 AM
    • Participant
      1,635 point Participant
    • JohanNL
    • Member since 03-28-2003, 8:58 AM
    • Posts 1,196

    I have the following stored procedure:

    SELECT

    SERVER_NAME,

    SERVICE_PORT,

    SERVER_NAME + ',' + SERVICE_PORT

    AS SERVER_AND_PORT

    FROM

    DEF_SERVICE_SETTINGS

    I want the 3rd column to be in format: SERVER_NAME + "," + SERVICE_PORT

    But SQL gives error. Sees "," as column.

    How can I fix this?

    Johan Theunissen
    MCPD, MCSE, MCTS BizTalk 2006

    ==============================

    Please mark the most helpful reply/replies as "Answer".
  • Re: How to build an string alias in T-SQL

    05-15-2007, 7:58 AM
    • Contributor
      3,086 point Contributor
    • Girijesh
    • Member since 10-27-2005, 12:04 PM
    • India
    • Posts 652
    Which database and language you are using?
  • Re: How to build an string alias in T-SQL

    05-15-2007, 8:02 AM
    • Participant
      1,635 point Participant
    • JohanNL
    • Member since 03-28-2003, 8:58 AM
    • Posts 1,196

    SQL-server

    Column SERVER_NAME is varchar type
    Column SERVICE_PORT is int type

    Johan Theunissen
    MCPD, MCSE, MCTS BizTalk 2006

    ==============================

    Please mark the most helpful reply/replies as "Answer".
  • Re: How to build an string alias in T-SQL

    05-15-2007, 8:05 AM
    • Participant
      1,635 point Participant
    • JohanNL
    • Member since 03-28-2003, 8:58 AM
    • Posts 1,196

    My code works when both type of columns are of varchar, but the SERVICE_PORT column is of int type.

    Can I cast the int type to a varchar type in some way?

    Johan Theunissen
    MCPD, MCSE, MCTS BizTalk 2006

    ==============================

    Please mark the most helpful reply/replies as "Answer".
  • Re: How to build an string alias in T-SQL

    05-15-2007, 8:10 AM
    Answer
    • Participant
      1,635 point Participant
    • JohanNL
    • Member since 03-28-2003, 8:58 AM
    • Posts 1,196

    Found it:

     SERVER_NAME + ',' + CAST(SERVICE_TCP_PORT As varchar(1000)) AS SERVER_AND_PORT

    Johan Theunissen
    MCPD, MCSE, MCTS BizTalk 2006

    ==============================

    Please mark the most helpful reply/replies as "Answer".
  • Re: How to build an string alias in T-SQL

    05-15-2007, 8:16 AM
    • Contributor
      3,086 point Contributor
    • Girijesh
    • Member since 10-27-2005, 12:04 PM
    • India
    • Posts 652

    Yes you have to Cast.

    SERVER_NAME + ',' + Cast(SERVICE_PORT as varchar) AS SERVER_AND_PORT

     

  • Re: How to build an string alias in T-SQL

    05-15-2007, 8:16 AM
    • Contributor
      2,176 point Contributor
    • JRICE
    • Member since 05-10-2006, 8:52 PM
    • Lebanon-Beirut
    • Posts 667

    convert(varchar(50), SERVICE_PORT)

     

     

    HTH

    Please remember to "Mark As Answer" if this post answered your question!

    Bilal Shouman - MCAD.NET

    |My Blog|
    ---------------------------------


Page 1 of 1 (7 items)