The Butter Connection

aka "stanguru.com" and "themargerums.com"

Computer Tips & Help
AS400
PC
Hardware
Programming
Web Development
Virus
Spyware/Malware
Spam
Hoax Don't Spread It
Sports
Cancer
Multiple Sclerosis
Election Stuff
Photography
Handy Links
Interesting
Cool Things
Gamer Stuff
Gallery
Clarion · SQL · SQL Dates · SQL RPG examples · Batch Files
Procs and Funcs

 

  • function - remove non-numeric chars and spaces
    use YOURDB
    go

    alter function cleanit(@fromname varchar(255))
    returns varchar(255)

    begin
    declare @counter int
    declare @end_loop int
    declare @ret_str varchar(255)
    declare @char_eval varchar(1)
                set @ret_str = ''
                set @counter = 1
                set @end_loop = len(@fromname)

                while @counter <= @end_loop
                begin
                      set @char_eval = substring(@fromname, @counter, 1)
                      if charindex(@char_eval,'0123456789') >= 1
                            set @ret_str = @ret_str + @char_eval
                      set @counter = @counter + 1
                end

    return @ret_str
    end
  • function - create an Acronym from a string (good for creating a vendor abbreviation or vendor indentifier)
    use YOURDB
    go

    create/alter function acronymit(@fromname varchar(255))
    returns varchar(255)
    begin
    declare @x int,
            @nextword_offset int,
            @toname varchar(255),
            @i int

    --this cleans the string from funky characters, comment it out if you dont care
    select @i = patindex('%[^a-zA-Z0-9 ]%', @fromname)
    while @i > 0
    begin
        select @fromname = replace(@fromname, substring(@fromname, @i, 1), ' ')
        select @i = patindex('%[^a-zA-Z0-9 ]%', @fromname)
    end

    set @toname = SUBSTRING(@fromname,1,1)
    set @x = 0
    select @nextword_offset = charINDEX( ' ',@fromname,2 )

    WHILE (@nextword_offset > 0)
    begin
       set @toname = rtrim(@toname) +  UPPER( SUBSTRING( @fromname,@nextword_offset+1,1) )
       select @nextword_offset = charINDEX( ' ',@fromname,@nextword_offset+1 )
    end

    return @toname
    end

     

    Truncates and shrinks log files for each user database on MS Sql server.

    declare @ssql nvarchar(4000)
    set @ssql= '
            if ''?'' not in (''tempdb'',''master'',''model'',''msdb'') begin
            use [?]
            declare @tsql nvarchar(4000) set @tsql = ''''
            declare @iLogFile int
            declare LogFiles cursor for
            select fileid from sysfiles where  status & 0x40 = 0x40
            open LogFiles
            fetch next from LogFiles into @iLogFile
            while @@fetch_status = 0
            begin
              set @tsql = @tsql + ''DBCC SHRINKFILE(''+cast(@iLogFile as varchar(5))+'', 1) ''
              fetch next from LogFiles into @iLogFile
            end
            set @tsql = @tsql + '' BACKUP LOG [?] WITH TRUNCATE_ONLY '' + @tsql
            --print @tsql
            exec(@tsql)
            close LogFiles
            DEALLOCATE LogFiles
            end'

    exec sp_msforeachdb @ssql

     


    Clear all keys(indexes) and constraints for rebuild
    create 2 procedures below in your DB
    then exec the following
    EXEC sp_MSforeachtable "Utils_drop_constraints '?'"
    EXEC sp_MSforeachtable "Utils_DeleteAllIndexesOnTable '?'"

     

     


     

    -------------------------------------------------------------------------------------
    ---Drop all contraints on a table
    --- for all tables in DB use: EXEC sp_MSforeachtable "sp_drop_constraints '?'"
    ---
    SET QUOTED_IDENTIFIER  OFF   
    SET ANSI_NULLS  ON
    GO

    if exists (select * from sysobjects where id = object_id(N'[dbo].[Utils_drop_constraints]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
    drop procedure [dbo].[Utils_drop_constraints]
    GO

    create proc Utils_drop_constraints @tablename sysname
    as

    -- name:   sp_drop_constraints (renamed by SAM)
    -- author: douglas bass
    -- date:   06/19/2000
    -- sp_drop_constraints will drop all constraints on the specified table,
    -- including CHECK, FOREIGN KEY, PRIMARY KEY, UNIQUE, and DEFAULT constraints. 
    -- Compile it in your master database and use it from any user database.

    set nocount on

    declare @constname sysname,
     @cmd  varchar(1024)

    declare curs_constraints cursor for
     select  name
     from  sysobjects
     where  xtype in ('C', 'F', 'PK', 'UQ', 'D')
     and (status & 64) = 0
     and     parent_obj = object_id(@tablename)

    open curs_constraints

    fetch next from curs_constraints into @constname
    while (@@fetch_status = 0)
    begin
     select @cmd = 'ALTER TABLE ' + @tablename + ' DROP CONSTRAINT ' + @constname
     exec(@cmd)
     fetch next from curs_constraints into @constname
    end

    close curs_constraints
    deallocate curs_constraints

    return 0


    GO
    SET QUOTED_IDENTIFIER  OFF   
    SET ANSI_NULLS  ON
    GO
    ---end drop all constraints
    --++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

     

     

     


     

     

    -------------------------------------------------------------------------------------
    ---Drop all indexes on a table
    --- for all tables in DB use: Exec sp_MSforeachtable "Utils_DeleteAllIndexesOnTable '?'"
    ---
    /******  ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO

    if exists (select * from sysobjects where id = object_id(N'[dbo].[Utils_DeleteAllIndexesOnTable]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
    drop procedure [dbo].[Utils_DeleteAllIndexesOnTable]
    GO

    create PROCEDURE [dbo].[Utils_DeleteAllIndexesOnTable] @TableName VarChar(200)
    AS
    BEGIN
    Declare @IndexName varchar(200)


    DECLARE index_cursor CURSOR FOR
    SELECT name FROM sysindexes where id = object_id(@TableName)
    AND NAME IS NOT NULL --and ROWS > 0

    OPEN index_cursor

    -- Perform the first fetch.
    FETCH NEXT FROM index_cursor into @IndexName

    WHILE @@FETCH_STATUS = 0
    BEGIN

    if left(@IndexName,2) = 'PK'
    BEGIN
    print 'drop constraint ' + @IndexName + ' on ' + @TableName
    Exec( 'ALTER TABLE ' + @TableName +
    'DROP CONSTRAINT ' + @IndexName )

    END
    ELSE
    BEGIN
    -- This is executed as long as the previous fetch succeeds.
    print 'drop index ' + @IndexName + ' on ' + @TableName


    Exec('drop index ' + @IndexName + ' on ' + @TableName)
    END

    FETCH NEXT FROM index_cursor into @IndexName

    END

    CLOSE index_cursor
    DEALLOCATE index_cursor
    END
    ---end drop all indexes
    --++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


  • Validate Date procedure


    CREATE OR REPLACE FUNCTION validate_date_format
     (p_input_date in VARCHAR2) RETURN BOOLEAN IS
       myResult DATE;
      BEGIN
       myResult := TO_DATE(p_input_date, 'MM/DD/YYYY');
       IF LENGTH(SUBSTR(p_input_date,
          INSTR(p_input_date, '/', 1, 2) + 1)) = 4 THEN
          RETURN TRUE;
       ELSE
          RETURN FALSE;
       END IF;
       EXCEPTION
       WHEN OTHERS THEN
          RETURN FALSE;
    END validate_date_format;

    ---PROC convert date, format it properly so it can be converted
    --- if you have a m/d/ccyy then it will turn it to 0m/0d/ccyy, also it will turn mm/dd/yy to mm/dd/ccyy
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[deformatdate]') )--and OBJECTPROPERTY(id, N'IsProcedure') = 1)
    drop function [dbo].[deformatdate]
    GO
    CREATE  FUNCTION deformatdate (@p_input_date as VARCHAR(15))
      RETURNs varchar(15)
      as

      BEGIN

        declare @myResult varchar(15)
        declare @myDate varchar(15)
        declare @dt VARCHAR(15)
        declare @delim CHAR(1)

        SET @dt = ltrim(rtrim(@p_input_date)) --'10/09/1999'
        SET @delim = '/'
    -- return(@dt)
        IF LEN(@dt) - LEN(REPLACE(@dt, @delim, SPACE(0))) = 2
           begin
    --        return((len(@dt)))
    --        return( len(@dt) - CHARINDEX(@delim, REVERSE(@dt)) )
    --        return( CHARINDEX(@delim, REVERSE(@dt)) - 1 )
            if (CHARINDEX(@delim, REVERSE(@dt)) - 1) = 4
                set @myResult =
                RIGHT('00' + CAST( cast( SUBSTRING(@dt, 1, CHARINDEX(@delim, @dt) - 1)  as int) AS VARCHAR(2)),2) + @delim +
                RIGHT('00' + CAST( cast( SUBSTRING(@dt, CHARINDEX(@delim, @dt) + 1,CHARINDEX(@delim, @dt,CHARINDEX(@delim, @dt) + 1) - CHARINDEX(@delim, @dt) - 1) as int) AS VARCHAR(2)),2) + @delim +
                SUBSTRING(@dt, LEN(@dt)+ 1 - CHARINDEX(@delim, REVERSE(@dt)) + 1, LEN(@dt))
            else
                begin
                set @myDate =
                RIGHT('00' + CAST( cast( SUBSTRING(@dt, 1, CHARINDEX(@delim, @dt) - 1)  as int) AS VARCHAR(2)),2) + @delim +
                RIGHT('00' + CAST( cast( SUBSTRING(@dt, CHARINDEX(@delim, @dt) + 1,CHARINDEX(@delim, @dt,CHARINDEX(@delim, @dt) + 1) - CHARINDEX(@delim, @dt) - 1) as int) AS VARCHAR(2)),2) + @delim +
                SUBSTRING(@dt, LEN(@dt)+ 1 - CHARINDEX(@delim, REVERSE(@dt)) + 1, LEN(@dt))
                set @myResult = CONVERT(varchar, convert(datetime,@myDate,1) , 110)
                end
           end
        ELSE
            IF LEN(@dt) - LEN(REPLACE(@dt, @delim, SPACE(0))) = 1
                set @myResult = '-1' --@dt
    --            set @myResult =
    --            RIGHT('00' + CAST( cast( SUBSTRING(@dt, 1, CHARINDEX(@delim, @dt) - 1) as int) AS VARCHAR(2)),2) + @delim +
    --            SUBSTRING(@dt, LEN(@dt)+ 1 - CHARINDEX(@delim, REVERSE(@dt)) + 1, LEN(@dt))
            ELSE
                set @myResult = '-1' --@dt

        if isdate(@myresult) <> 1
            set @myResult = '01/01/1900'


        return(@myresult)

    END

     

     

     

     



     


    You are here: Home-Computer Tips & Help-Programming-SQL-Procs and Funcs