Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 charindex

Author  Topic 

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2013-05-16 : 03:26:29
how can i only get from the column '12345'?
but some of the data has smaller room number.

ROOMCOLUMN
the majestic room 12345
the majestic room 123
the majestic room 1
the majestic room 1459

result should be as below:
ROOMCOLUMN
12345
123
1
1459

waterduck
Aged Yak Warrior

982 Posts

Posted - 2013-05-16 : 03:45:05
DECLARE @foo TABLE(ROOMCOLUMN VARCHAR(50))
INSERT INTO @foo VALUES
('the majestic room 12345'),
('the majestic room 123'),
('the majestic room 1'),
('the majestic room 1459')

SELECT *, STUFF(ROOMCOLUMN, 1, 18, '')
FROM @foo
Go to Top of Page

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2013-05-16 : 03:48:11
i will still get the rest of the data..
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-16 : 04:02:52
[code]
SELECT STUFF(ROOMCOLUMN, 1, PATINDEX('%[0-9]%',ROOMCOLUMN)-1, '')
FROM @foo
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2013-05-16 : 04:19:29
not possible to use charindex or substring?
because some of the data has like this:


the majestic room 12345 reference 10
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-16 : 04:32:10
quote:
Originally posted by peace

not possible to use charindex or substring?
because some of the data has like this:


the majestic room 12345 reference 10


First tell us what all formats data can come. Then we can think of a generic approach to get you the required data

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2013-05-16 : 04:45:52
this is the data.
I only want the room number.

ROOMCOLUMN
the majestic room 12345
the majestic room 123
the majestic room 1
the majestic room 1459
the majestic room 123444 reference 10 block
the majestic room 4535 reference 15 block
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-16 : 05:08:09
assuming no other formats being present, you can use below

SELECT LEFT(STUFF(ROOMCOLUMN, 1, PATINDEX('%[0-9]%',ROOMCOLUMN)-1, ''),CHARINDEX(' ',STUFF(ROOMCOLUMN, 1, PATINDEX('%[0-9]%',ROOMCOLUMN)-1, '')+ ' ')-1)
FROM @foo



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2013-05-16 : 05:14:50
got it.
is working fine.
thanks!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-16 : 05:18:54
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -