Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-server-2005 SQL TSQL , +
There is a text filed in a table called as description. I would like to extract two date fields from this string when there is an occurrence of '~' character using sql server 2005 stored procedure. Help me out in this case.
 
Example: string: '长期租金;10/1/2012 ~ 10/31/2012'. At occurrence of ~ operator I would like to have from-date: 20121001 and to-date: 20121031.
Posted 22-Oct-12 0:40am
Edited 22-Oct-12 8:04am
Maciej Los158.3K
v2
Comments
djj55 at 22-Oct-12 11:39am
   
Is the data you are looking for always dates? Or of a given size?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The easiest way is to use regular expressions. As SQL server does not support regular expressions by default, you have to extend it using .net integrated user function. It is really not complicated. See these two articles: http://msdn.microsoft.com/en-us/magazine/cc163473.aspx[^], Regular Expressions in MS SQL Server 2005/2008[^].
If this task has to be done regularly, I suggest you implement two functions based on one of the above, that extracts first of last dates directly to sql date.
  Permalink  
Comments
Maciej Los at 22-Oct-12 13:21pm
   
Great answer, +5!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Use CHARINDEX[^] function to find ~ and then LEFT[^] or RIGHT[^] or SUBSTRING[^] function to return the part of string.
 
For example:
DECLARE @inputstring NVARCHAR(100)
DECLARE @pos INT
DECLARE @fromdate NVARCHAR(10)
DECLARE @todate NVARCHAR(10)
 
SET @inputstring = '长期租金;10/01/2012 ~ 10/31/2012'
SET @pos = CHARINDEX('~', @inputstring)
 
IF @pos >0
    BEGIN
        SET @fromdate = SUBSTRING(@inputstring,@pos-11,10)
        SET @todate = SUBSTRING(@inputstring,@pos+1,10)
        SELECT @fromdate as [from-date], @todate as [to-date]
    END
ELSE --@pos =0
    PRINT 'There''s no matches!'
  Permalink  
v2
Comments
damodara naidu betha at 24-Oct-12 3:18am
   
5+
Maciej Los at 24-Oct-12 13:48pm
   
Thank you ;)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi ..,
 
try this batch
 
DECLARE @String VARCHAR(200) = '长期租金;10/1/2012 ~ 10/31/2012'
 
SET @String = SUBSTRING(@String,CHARINDEX(';',@String,0)+1,LEN(@String))
 
SELECT SUBSTRING(@String,0,CHARINDEX('~',@String,0)) [From-Date], 
SUBSTRING(@String,CHARINDEX('~',@String,0)+1,LEN(@String)) [To-Date]
 
Thank you
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 210
2 BillWoodruff 205
3 Jochen Arndt 180
4 DamithSL 165
0 OriginalGriff 5,130
1 DamithSL 4,157
2 Maciej Los 3,670
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,821


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 24 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100