You must Sign In to post a response.
  • Category: SQL Server

    Sql query to replace hyphen

    Hi Friends,

    Need query to replace a hyphen if exists in first position in description col and another to check if hyphen alone exists then replace it with blank space.

    ex: -Test123 - in this case, need to remove hyphen
    - (only hyphen) - in this case, need to include null

    Thanks in Advance
  • #648065
    hi

    REPLACE("111-3456", '-', '')

    Regards
    Sagar pawar
    Software developer

  • #648067
    Hello,

    Syntax:
    REPLACE ( string_expression , string_pattern , string_replacement )

    please check below query:

    select replace(TRIM(name), '-', '') as new_name from <table>

    Regards
    Mr.Rajesh
    Software Engg.

  • #648077
    Hi,

    check below query that is used to replace hypen from string



    select replace('-test','-',' ' )

    output: test



    is this what you required or anything else is required.

    With Regards
    Kushal

  • #648428
    Please check this code which removes first hyphen and also
    put null if only hyphens are there in the column


    CREATE TABLE #Test
    (
    Data VARCHAR(255)
    )

    INSERT INTO #Test
    VALUES('-Alwyn')
    INSERT INTO #Test
    VALUES('---')
    INSERT INTO #Test
    VALUES('Durai')

    SELECT CASE WHEN LEN(REPLACE(Data, '-', '')) = 0
    THEN NULL
    ELSE CASE WHEN LEFT(Data, 1) = '-' THEN RIGHT(Data, LEN(Data)-1) ELSE Data END END
    FROM #Test

    DROP TABLE #Test

    Please mark this as Answer, if this helps

    Regards,
    Alwyn Duraisingh.M 
    << Database Administrator >>
    Jesus saves! The rest of us better make backups...

  • #648930
    update TableName
    set ColumnName= Substring(ColumnName,2,LEN(ColumnName)-1)
    where ColumnName Like '-%'

    --
    Thanks & Regards,

    Priyank Saxena
    -----------------------------------
    I would love to change the world,
    but they won't give me the source code !!


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.