Geoffrey Emery
Data Stories..Letting Data Tell Your Story

Instr/InString translation for T-Sql

May 6, 2008 11:39 by gemery

I just ran over some code I remembered grabbing from the web forever ago i thought i would share  the wealth. It works like a charm!

USE [EmailGenerator]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 
CREATE  function [dbo].[InString](
 
      @string varchar(200), 
      @searchfor varchar(50), 
      @position int
      ) returns int
 
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Returns the position of the character AFTER the nth instance 
of the string
 
-- TEST CASE #1
-- should return 18
select dbo.InString('123456 123456 123456 123456', '23', 3) 
                    as [Test Case #1]
 
-- TEST CASE #2
-- should return 25
select dbo.InString('test1/test2/test3/test4/test5/', '/', 4) 
                     as [Test Case #2]
 
-- TEST CASE #3
declare @teststring varchar(50)
set @teststring = 'test1/test2/test3/test4/test5/'
select substring(@teststring, dbo.Instring(@teststring,'/',3),5)
                     as [Test Case #3]
-- should return 'test4'
 
-- TEST CASE #4 (variable length delimited fields
declare @teststring2 varchar(50)
set @teststring2 = 'test123/test/testtestestest/testxyz/test/'
select substring( @teststring2, 
                  dbo.Instring(@teststring2, '/', 3),
                  (dbo.Instring(@teststring2, '/', 4) -1) 
                           - dbo.Instring(@teststring2, '/', 3)
                  ) as [Test Case #4]
-- should return 'testxyz'
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
 
as
begin
declare @lenstring int, @poscount int, @stringpos int
set @lenstring = datalength(@searchfor)
set @poscount = 1
set @stringpos = 1
while @poscount <= @position and @stringpos <= len(@string) 
      begin
            -- if we find the string segment we're looking for 
        if substring(@string, @stringpos, @lenstring)=@searchfor
                  begin
                -- is the instance of the string the one we are
                --  looking for?
                        if @poscount = @position 
                              begin
                        set @stringpos = @stringpos + @lenstring
                                    return @stringpos
                              end
                -- else look for the next instance of the string
                -- segment
                        else 
                              begin
                                    set @poscount = @poscount + 1
                              end
                  end
            set @stringpos = @stringpos + 1
      end
return null
end
 

 

Technorati Tags: ,,,

Tags: ,
Categories: T-SQL
Actions: E-mail | Permalink | Comments (1) | Comment RSSRSS comment feed

T-Sql Seperate Date and Time

April 23, 2008 17:36 by gemery

I just used this for the 1000th time so I thought i would blog about it. Just replace getdate() and with your date...

-- just the date
SELECT CONVERT(char(10), getdate(), 101) AS [Date]
SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))
-- just the time
SELECT (getdate() - 
    CAST(ROUND(CAST(getdate() AS float), 0, 1) AS datetime))
SELECT CONVERT (datetime, (1 - (CONVERT (float, getdate() ) - 
     CONVERT (int, CONVERT (float, getdate())))) * - 1 - 1)

Tags:
Categories: SQL Server | T-SQL
Actions: E-mail | Permalink | Comments (2) | Comment RSSRSS comment feed