afternoon everyone,

I am new to using MSSQL and need help with SUBSTRING function.

i have a strings like below

s2-Total Solids - Std Method
V1E-RVT4/2.5@20degC Initial
V1Y-LVT2/60@20deg Initial
A2-Alkalinity - ISO Method(%)

I would like to only get the data before the first "-" IE

s2
V1E
V1Y
A2

so that i can link two tables in a sql view, would anyone know how i would complete this

can it be done like so?

RTRIM(LEFT (QCTEST.TESTNAME, 10))

Recommended Answers

All 8 Replies

try this

SELECT SUBSTRING(QCTEST.TESTNAME, 1, CHARINDEX('-',CTEST.TESTNAME)-1)
FROM
....

thats, i get invalid lenght parameter passed to the substring function

here is my code

SUBSTRING(TestNoName, 1, CHARINDEX('-', TestNoName) - 1)

thanks for your help

does this work ?

select CHARINDEX('-', TestNoName), CHARINDEX('-', TestNoName)-1 
from
.....

now get invalild or missing expression

Select SUBSTRING(TESTNAME,0,CHARINDEX('-',TESTNAME))
--Parameters Of SUBSTRING:   Expression,from,until
--From is passed as 0, because you need first part of the string.
--CHARINDEX is used to get the Index of '-' in TESTNAME

no joy

i get no output

see pictue. thanks in advanced.

See the comments putted on the attached image.

You need to write the query like this:

SELECT SUBSTRING(TestNoName,0,CHARINDEX('-',TestNoName))

thanks dude, took weeks for this. thanks so so much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.