adding a prefix to a column in an SQL view

  • Hi all,

    Is it possible to add a prefix to a returned result in a view in sql.

    eg

    select productId

    from Table1

    returns

    id1

    id2

    id3

    I would like to put a set value(prefix) to the returned result eg http://www.john.com/

    returning the following

    http://www.john.com/id1

    http://www.john.com/id2

    http://www.john.com/id3

    is this possible ?

    thanks very much

    John.

  • Is ProductID a string type or an integer?



    MCSE: Data Platform
    MCSE: Business Intelligence
    Follow me on Twitter: @WazzTheBadger
    LinkedIn Profile: Simon Osborne

  • Product ID is an integer but im trying to add a string to it. I can change it to a varchar if needed as there isnt that many products so speed is not an issue

  • DECLARE @table1 TABLE

    (

    ProductId INT

    )

    INSERT INTO @table1

    SELECT 1

    UNION ALL SELECT 2

    UNION ALL SELECT 3

    SELECT

    'www.john.com/' + convert(varchar(50),productid)

    FROM

    @table1

    Use the CONVERT() function or CAST()



    MCSE: Data Platform
    MCSE: Business Intelligence
    Follow me on Twitter: @WazzTheBadger
    LinkedIn Profile: Simon Osborne

  • Thanks very much, i had this working in the past but couldnt remember how I did it or find an answer.

    Solved.:-)

  • Hi dallibabs,

    try this query.

    select 'www.john/'+Convert(varchar,productId) as id from Table1

    --chalam

  • chalam87 (4/18/2013)


    Hi dallibabs,

    try this query.

    select 'www.john/'+Convert(varchar,productId) as id from Table1

    Remember to specify a length in you convert() for the varchar. Otherwise you will end up with some strange truncation errors.



    MCSE: Data Platform
    MCSE: Business Intelligence
    Follow me on Twitter: @WazzTheBadger
    LinkedIn Profile: Simon Osborne

  • Thanks everyone for you input

    @s_osborne2 i will bear this in mind

    thanks again

    John.

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply