user1591668
user1591668

Reputation: 2893

SQL Server : how can I ignore the last 5 characters of a varchar

I am new to SQL and using SQL Server 2012 and I am trying to ignore the last 5 characters of a string... I have this simple query

Select MarketIdentifier from Markets

I have tried this

select SUBSTR(TRIM(MarketIdentifier), 1, LENGTH(TRIM(MarketIdentifier))-2) AS "MarketID" 
from Markets

but I get the error

Msg 195, Level 15, State 10, Line 4
'TRIM' is not a recognized built-in function name.

The MarketIdentifier has around 20 characters and I just want the first 15 characters. Any suggestions would be great

Upvotes: 1

Views: 2336

Answers (3)

evanb629
evanb629

Reputation: 1

In your PHP code just SELECT your query and use the trim() function for however many characters you want. This should work.

Upvotes: 0

Anusha Subashini
Anusha Subashini

Reputation: 397

SELECT RIGHT(MarketIdentifier, LEN(MarketIdentifier) - 5) AS MyTrimmedColumn  AS "MarketID" from Markets

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1269693

For the first 15, you would use the left() function:

select left(MarketIdentifier, 15) 
from Markets;

Your question is slightly more complicated, though. To remove the last five characters, you can do:

select left(MarketIdentifier, len(left(MarketIdentifier, 15) ) - 5) 
from Markets;

Upvotes: 3

Related Questions