51

I have table with data 1/1 to 1/20 in one column. I want the value 1 to 20 i.e value after '/'(front slash) is updated into other column in same table in SQL Server.

Example:

Column has value 1/1,1/2,1/3...1/20
new Column value 1,2,3,..20

That is, I want to update this new column.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
SHEKHAR SHETE
  • 5,964
  • 15
  • 85
  • 143

8 Answers8

117

Try this:

UPDATE YourTable
SET Col2 = RIGHT(Col1,LEN(Col1)-CHARINDEX('/',Col1))
Lamak
  • 69,480
  • 12
  • 108
  • 116
14

Please find the below query also split the string with delimeter.

Select Substring(@String1,0,CharIndex(@delimeter,@String1))
Pedram
  • 6,256
  • 10
  • 65
  • 87
user5151121
  • 141
  • 1
  • 2
2

From: http://www.sql-server-helper.com/error-messages/msg-536.aspx

To use function LEFT if not all data is in the form '1/12' you need this in the second line above:

Set Col2 = LEFT(Col1, ISNULL(NULLIF(CHARINDEX('/', Col1) - 1, -1), LEN(Col1)))
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Miguel
  • 81
  • 1
  • 8
  • I guess some one didn't understand what I meant. Your data: 1/1; 1/2; 2/1; 2/2; 2/3; 3; 4/1; You might have an error in "3" due to the missing sub-part. – Miguel Feb 12 '15 at 15:19
2
SELECT SUBSTRING(ParentBGBU,0,CHARINDEX('-',ParentBGBU,0)) FROM dbo.tblHCMMaster;
FelixSFD
  • 6,052
  • 10
  • 43
  • 117
Ankur Shah
  • 467
  • 5
  • 3
2

I know this question is specific to sql server, but I'm using postgresql and came across this question, so for anybody else in a similar situation, there is the split_part(string text, delimiter text, field int) function.

mbarkhau
  • 8,190
  • 4
  • 30
  • 34
1

Maybe something like this:

First some test data:

DECLARE @tbl TABLE(Column1 VARCHAR(100))

INSERT INTO @tbl
SELECT '1/1' UNION ALL
SELECT '1/20' UNION ALL
SELECT '1/2'

Then like this:

SELECT
    SUBSTRING(tbl.Column1,CHARINDEX('/',tbl.Column1)+1,LEN(tbl.Column1))
FROM
    @tbl AS tbl
Arion
  • 31,011
  • 10
  • 70
  • 88
-1
SELECT emp.LoginID, emp.JobTitle, emp.BirthDate, emp.ModifiedDate  , 
      CASE  WHEN emp.JobTitle  NOT LIKE '%Document Control%'  THEN emp.JobTitle
            ELSE SUBSTRING(emp.JobTitle,CHARINDEX('Document Control',emp.JobTitle),LEN('Document Control'))
      END 
      ,emp.gender,emp.MaritalStatus
FROM   HumanResources.Employee [emp]
WHERE  JobTitle LIKE '[C-F]%'
Taryn
  • 242,637
  • 56
  • 362
  • 405
-1

Use CHARINDEX. Perhaps make user function. If you use this split often.
I would create this function:

CREATE FUNCTION [dbo].[Split]
(
    @String VARCHAR(max),
    @Delimiter varCHAR(1)
)
RETURNS TABLE 
AS
RETURN 
(
    WITH Split(stpos,endpos) 
    AS(
        SELECT 0 AS stpos, CHARINDEX(@Delimiter,@String) AS endpos
        UNION ALL
        SELECT endpos+1, CHARINDEX(@Delimiter,@String,endpos+1)
            FROM Split
            WHERE endpos > 0
    )
    SELECT 'INT_COLUMN' = ROW_NUMBER() OVER (ORDER BY (SELECT 1)),
        'STRING_COLUMN' = SUBSTRING(@String,stpos,COALESCE(NULLIF(endpos,0),LEN(@String)+1)-stpos)
    FROM Split
)
GO
jAC
  • 5,195
  • 6
  • 40
  • 55