简体   繁体   English

如何从 sql server 2005 中的字符串/文本字段中提取日期字段

[英]How to extract date fields from string/text field in sql server 2005

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.当使用 sql server 2005 存储过程出现“~”字符时,我想从此字符串中提取两个日期字段。 Help me out in this case.在这种情况下帮助我。

Example: string: '长期租金;10/1/2012 ~ 10/31/2012'.示例:字符串:'租赁长期;10/1/2012 ~ 10/31/2012'。 At occurrence of ~ operator I would like to have from-date: 20121001 and to-date:20121031.在 ~ 运算符出现时,我希望从日期:20121001 和至今:20121031。

In this instance you can use the following but really you need an exists clause or something like that to test the string for the tilde (~) and as everyone else has stated, this only works if the string always has a semicolon(;) and a tilde(~).在这种情况下,您可以使用以下内容,但实际上您需要一个存在子句或类似的东西来测试波浪号 (~) 的字符串,正如其他人所说,这仅适用于字符串始终具有分号 (;) 和波浪号 (~)。 You can convert to the strings into datetime fields if you need.如果需要,您可以将字符串转换为日期时间字段。 I have placed the string in a variable to make it easier to read...我已将字符串放在一个变量中以使其更易于阅读...

DECLARE @string AS NVARCHAR(255)声明 @string 为 NVARCHAR(255)

SET @string = '长期租金;10/1/2012 ~ 10/31/2012' SET @string = '长期长期;10/1/2012 ~ 10/31/2012'

SELECT StartDate = SUBSTRING(@string,CHARINDEX(';',@string)+1,LEN(@string)-CHARINDEX('~',@string)-1) ,EndDate = LTRIM(RIGHT(@string,LEN(@string)-CHARINDEX('~',@string))) SELECT StartDate = SUBSTRING(@string,CHARINDEX(';',@string)+1,LEN(@string)-CHARINDEX('~',@string)-1) ,EndDate = LTRIM(RIGHT(@string,LEN( @string)-CHARINDEX('~',@string)))

Here is a method which will give the start and end dates.这是一个给出开始和结束日期的方法。 I left most of the testing selects in place but commented out.我保留了大部分测试选择,但已注释掉。

DECLARE @string AS NVARCHAR(255)
DECLARE @Seperator as char(1) = '~'
declare @CharStartDate as varchar(10)
declare @CharStopDate as varchar(10)
declare @StartDate as date
declare @StopDate as date

declare @I int

--SET @string = 'xvvvvvvcc;1/09/2012 ~ 1/10/2012xx'
--SET @string = 'xvvvvvvcc;12/31/2012 ~ 1/1/2012xx'
--SET @string = 'xvvvvvvcc;12/1/2012 ~ 10/0/2012xx'
SET @string = 'xvvvvvvcc;1/2/2012 ~ 1/3/2012xx'
--longest date 12/31/2011 = 10
--shortest date 1/1/2012 = 8
-- width of seperator = 3 

SELECT 
@CharStartDate = substring (@string, CHARINDEX(@Seperator,@string)-11,10)
,@CharStopDate = substring (@string, CHARINDEX(@Seperator,@string)+2,10)

--SELECT  @CharStartDate,@CharStopDate

select @I = ascii(substring(@CharStartDate,1,1))
While @I > 57
BEGIN
set @CharStartDate = substring(@CharStartDate,2,10)
--select @CharStartDate
select @I = ascii(substring(@CharStartDate,1,1))
END

select @I = ascii(substring(REVERSE(@CharStopDate),1,1))
While @I > 57
BEGIN
set @CharStopDate = REVERSE(substring(REVERSE(@CharStopDate),2,10))
--select @CharStopDate
select @I = ascii(substring(REVERSE(@CharStopDate),1,1))
END

--select ascii(';'),ascii('9'),ascii('8'),ascii('7'),ascii('6'),ascii('6'),ascii('4'),ascii('3'),ascii('2'),ascii('1'),ascii('0')
SELECT  @StartDate = @CharStartDate,@StopDate = @CharStopDate
--SELECT  @I,@string,@Seperator,@CharStartDate,@CharStopDate,@StartDate,@StopDate
select datediff(dd,@StartDate,@StopDate) AS 'DateDiff',@StartDate as 'Start Date',@StopDate as 'Stop Date'

I will leave it to you to check for the seperator.我会让你检查分隔符。

CREATE FUNCTION [dbo].[RemoveAlphaCharacters](@Temp nvarchar(max))
RETURNS nvarchar(max)
AS
BEGIN
  WHILE PatIndex ('%[^0-9~/]%', @Temp) > 0
  SET @Temp = Stuff(@Temp, PatIndex('%[^0-9~/]%', @Temp), 1, '')
  RETURN @Temp
END

DECLARE @string nvarchar(max) = '长期租金;10/1/2012 ~ 10/31/2012'
SELECT CONVERT(date, SUBSTRING([dbo].[RemoveAlphaCharacters](@string), 0,
       CHARINDEX('~', [dbo].[RemoveAlphaCharacters](@string))), 101) AS BDate,
       CONVERT(date, SUBSTRING([dbo].[RemoveAlphaCharacters](@string),
       CHARINDEX('~', [dbo].[RemoveAlphaCharacters](@string)) + 1,
       CHARINDEX('~', REVERSE([dbo].[RemoveAlphaCharacters](@string)))), 101) AS EDate

i have never used the older version of SQL cause i just graduated but doesnt it have the EXTRACT() function?.. The syntax goes like this below.我从未使用过旧版本的 SQL,因为我刚毕业,但它没有 EXTRACT() 函数吗?.. 语法如下。

SELECT First_Name ,    
  
    EXTRACT ( CAST(Created_date AS DATE) FROM Created_date ) AS Date_only ;

You specify 'First_name' to let SQL know you want it as a column and 'created_date' is the field from which youre trying to separate the date.您指定 'First_name' 以让 SQL 知道您希望将其作为列,而 'created_date' 是您试图从中分离日期的字段。 the cast function converts your field to DATE value before extractig it. cast 函数在提取它之前将您的字段转换为 DATE 值。

i hope this helps .我希望这有帮助 。 thank you.谢谢你。 if im wrong please let me know i would like to improve myself.如果我错了,请让我知道我想提高自己。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM