SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 replace only some underscores
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sravan.here
Starting Member

4 Posts

Posted - 02/21/2014 :  15:58:51  Show Profile  Reply with Quote
I have a column with value

AAA_ZZZZ_7890_10_28_2014_123456.jpg

I need to replace the middle underscores so that it displays it as date i.e.

AAA_ZZZZ_7890_10-28-2014_123456.jpg

Can some one please suggest a simple update query for this.

The Number of Underscores would be same for all the values in the column but the length will vary for example some can have

AAA_q10WRQ_001_05_15_2014_12.jpg

TG
Flowing Fount of Yak Knowledge

USA
6062 Posts

Posted - 02/21/2014 :  16:07:42  Show Profile  Reply with Quote
Here's one ugly way:

select stuff(stuff(s, idx+3, 1, '-'), idx+6, 1, '-')
from   (
       select s, patindex('%_[0-1][0-9]_[0-3][0-9]_20[0-2][0-9]_%', s) idx
       from   (
              select 'AAAxZZZZ_7890_10_28_2014_123456.jpg' s union all
              select 'AAA_q10WRQ_001_10_28_2014_12.jpg'
              ) d
       ) d

output:

AAAxZZZZ_7890_10-28-2014_123456.jpg
AAA_q10WRQ_001_10-28-2014_12.jpg


EDIT:
this version will only work with dates between years 2000 an 2029

Be One with the Optimizer
TG

Edited by - TG on 02/21/2014 16:30:43
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.91 seconds. Powered By: Snitz Forums 2000