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 2008 Forums
 Transact-SQL (2008)
 If/Else with Substring......

Author  Topic 

qman
Constraint Violating Yak Guru

442 Posts

Posted - 2011-08-23 : 20:01:13
I have a single column table which contains strung together data.
I am trying to write a SQL statement which will substring the data in a certain format if the first character is a numeric value.
If the first character is a space or alpha, substring a different way.

Can anyone help me out with this?

if columnA is numeric
select substring(columnA, 2, 5) as secondspot from mytable
if columnA is a space or alpha
select substring(columnA,1, 4 ) as firstspot from mytable

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2011-08-23 : 22:45:19
[code]
select case when columnA like '[0-9]%' then substring(columnA, 2, 5) else substring(columnA,1, 4 ) end
from mytable[/code]

Harsh Athalye
http://www.letsgeek.net/
Go to Top of Page
   

- Advertisement -