How can I select the first 100 characters in SQL Server?

前端 未结 5 826
灰色年华
灰色年华 2021-01-03 18:25

I want to truncate a column to a max of 100 characters. How do you do this in SQL Server?

相关标签:
5条回答
  • 2021-01-03 18:54
    SELECT SUBSTR(COLUMN_NAME, 1, LENGTH) FROM TABLENAME where LENGTH(COLUMN_NAME) > LENGTH
    

    Ex:

    SELECT SUBSTR(DESCRIPTION,1,100) FROM STOREDETAILS where LENGTH(DESCRIPTION)>100
    

    For those records, with length less than 100, the actual value would be shown.

    Otherwise, some databases induce blank characters in the resultant records.

    0 讨论(0)
  • 2021-01-03 18:58

    SUBSTRING(myColumn, 1, 100)

    See the docs: http://msdn.microsoft.com/en-us/library/ms187748.aspx

    0 讨论(0)
  • 2021-01-03 19:02

    You can also use the LEFT() function.

    LEFT(col, 100)
    
    0 讨论(0)
  • 2021-01-03 19:07

    substring is the method: SUBSTRING ( value_expression ,start_expression , length_expression ) from the help.

    0 讨论(0)
  • 2021-01-03 19:12

    Try this:

     SELECT LEFT (your_column, 100) FROM your_table 
    

    Edit:

    you can also try something like this:

      SELECT LEFT (your_column, LEN(your_column)-5) FROM your_table 
    

    for say if you want to trim the last 5 characters from a record.

    0 讨论(0)
提交回复
热议问题