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
 SQL Server 2012 Forums
 Transact-SQL (2012)
 How to only extra part of a column data?

Author  Topic 

yguyon
Starting Member

11 Posts

Posted - 2015-01-14 : 15:07:39
I have this field in one of the column DBMP-1861571 and I would like to write a querry that only extract DBMP in a select statement...

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2015-01-14 : 15:15:29
select LEFT(columnname, 4) as aliasname
from your table


Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

yguyon
Starting Member

11 Posts

Posted - 2015-01-14 : 15:23:13
Great but what if the number is bigger and I want to truncate from the -

quote:
Originally posted by tkizer

select LEFT(columnname, 4) as aliasname
from your table


Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2015-01-14 : 15:37:09
select left(columnname, charindex('-', columnname)-1) as aliasname
from yourtable

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page
   

- Advertisement -