2013-08-08

TSQL - Function to transform a given comma-separated string value into a table with one row per element

Execution example:

getTableFromCSV('asdf,qwer,poiu');


Funcion's code:

ALTER function [dbo].[getTableFromCSV]
(
       @csv VARCHAR(MAX)
)
RETURNS
       @tbl TABLE( val VARCHAR(255) )
AS
BEGIN

       DECLARE @sTemp VARCHAR(255)
       WHILE LEN(@csv) > 0
       BEGIN
              SET @sTemp = LEFT(@csv, ISNULL(NULLIF(CHARINDEX(',', @csv) - 1, -1), LEN(@csv)))
              SET @csv = SUBSTRING(@csv,ISNULL(NULLIF(CHARINDEX(',', @csv), 0), LEN(@csv)) + 1, LEN(@csv))
              INSERT INTO @tbl VALUES (LTRIM(RTRIM(@sTemp)))
       END
       -- kill the nulls/ empty strings
       DELETE FROM @tbl WHERE val IS NULL OR val = '' 
RETURN
END
;


TSQL - Function to get string returned as a table with one record for each word, and some basic stats

If you create the function (code below), and execute this:
select * from getWords( 'this is only a one one figurative test test ', 0, 1 );

You will get this:


The function's code:

create FUNCTION [dbo].[getWords]
(
       @text nvarchar(4000)
       , @ReturnOnlyMeaningful bit
       , @SuppressDuplicates bit
)
RETURNS
       @words
       TABLE (
              WordID smallint --primary key
              --, Position smallint
              , Word varchar(255)
              , OrderOfAppearance tinyint
              , AlphabeticRank tinyint
              , [Length] tinyint
                     , LengthRank tinyint
              , Frequency tinyint
              , WordCount tinyint
                     , DistinctWordCount tinyint
                    
              , Meaningful tinyint
              , MeaningfulWordCount tinyint
                     , MeaningfulDistinctWordCount tinyint
             
              , OriginalText varchar(4000)
       )
AS
BEGIN

       -- Parameter validation and cleanup
      
              -- Do not process empty strings
              if ltrim(rtrim(@text)) = ''
              begin
                     return
              end

              -- Double quote characters if found
              /*
                     select REPLACE( 'this is" something', '"', '""' );
                     select REPLACE( 'this is'' something', '''', '''''' );
              */
              set @text = REPLACE( @text, '"', '""' );
              set @text = REPLACE( @text, '''', '''''' );
             



       -- If it is ok to display dupicates
       if @SuppressDuplicates = 0
       begin
              insert into
                     @words
              select
                     occurrence
                     , display_term
                     , dense_rank()
                           over(
                                  order by occurrence asc
                           )
                           as OrderOfAppearance
                     , DENSE_RANK()
                           over(
                                  order by display_term asc
                           ) as AlphabeticRank
                     , LEN( display_term ) as [Length]
                     , dense_rank()
                           over(
                                  /*
                                  partition by 
                                         case  
                                                when special_term = 'Exact Match'
                                                then 1
                                                else 0
                                         end
                                  */                        
                                  order by LEN( display_term ) desc
                           ) as isMeaningfulLengthRank                           
                     , CAST( 1 as tinyint ) as Frequency     
                     , null as WordCount
                     , null as DistinctWordCount
                     , cast(
                           case  
                                  when special_term = 'Exact Match'
                                  then 1
                                  else 0
                           end
                           as tinyint
                     ) as isMeaningful         
                     , null as MeaningfulWordCount
                     , null as MeaningfulDistinctWordCount
                     , @text as OriginalText   
              from
                     sys.dm_fts_parser ( N'"' + @text + '"', 1033, 0, 0)
              where
                     -- Return only meaningful, according to the parameter
                     (
                           @ReturnOnlyMeaningful = 0
                           or (
                                  @ReturnOnlyMeaningful = 1
                                  and special_term = 'Exact Match'
                           )
                     )
                     -- Do not return end of file word.
                     and display_term not in
                     (
                           'END OF FILE' -- Seems to be produced by hyphens
                     )
              ;
       end
      
      
      
       -- If we are to suppress duplicates
       else
       begin
              insert into
                     @words
              select
                     min( occurrence ) as occurrenceMin
                     , display_term
                     , dense_rank()
                           over(
                                  order by min( occurrence ) asc
                           )
                           as OrderOfAppearance             
                     , DENSE_RANK()
                           over(
                                  order by display_term asc
                           ) as AlphabeticRank              
                     , LEN( display_term ) as [Length]
                     , dense_rank()
                           over(
                                  /*
                                  partition by 
                                         case  
                                                when special_term = 'Exact Match'
                                                then 1
                                                else 0
                                         end                       
                                  */
                                  order by LEN( display_term ) desc
                           ) as isMeaningfulLengthRank                                         
                     , COUNT(*) as Frequency   
                     , null as WordCount 
                     , null as DistinctWordCount
                     , cast(
                           case  
                                  when special_term = 'Exact Match'
                                  then 1
                                  else 0
                           end
                           as tinyint
                     ) as isMeaningful                
                     , null as MeaningfulWordCount
                     , null as MeaningfulDistinctWordCount                 
                     , @text as OriginalText   
              from
                     sys.dm_fts_parser ( N'"' + @text + '"', 1033, 0, 0 )
              where
                     -- Return only meaningful, according to the parameter
                     (
                           @ReturnOnlyMeaningful = 0
                           or (
                                  @ReturnOnlyMeaningful = 1
                                  and special_term = 'Exact Match'
                           )
                     )
                     -- Do not return end of file word.
                     and display_term not in
                     (
                           'END OF FILE'
                     )
              group by
                     display_term
                     , case
                           when special_term = 'Exact Match'
                           then 1
                           else 0
                     end
              ;     
       end
      
      
       -- Word count
       update
              @words
       set
              WordCount = ( select SUM(Frequency) from @words )
              , DistinctWordCount = ( select COUNT(*) from @words )
              , MeaningfulWordCount = ( select SUM( Meaningful * Frequency ) from @words )
              , MeaningfulDistinctWordCount = ( select SUM( Meaningful ) from @words )
       ;
      
      
       -- Return
       return;
      
end



TSQL - Text field into table with one row per word

SQL Server 2012...

select * from sys.dm_fts_parser ( '"this is a-long text file file"', 1033, 0, 0 );

For more details, read
http://technet.microsoft.com/en-us/library/cc280463.aspx

2013-07-10

TSQL - Print statement does not immediately show, how to fix

Use RAISERROR instead...

RAISERROR('YOUR TEXT GOES IN HERE', 0, 1) WITH NOWAIT;

2013-07-08

TSQL - Error catching basic structure (try/catch/rollback)

begin try
begin tran


       -- YOUR STATEMENTS HERE


       commit tran
end try
begin catch

       -- Display error
       /*
       SELECT
              ERROR_NUMBER() AS ErrorNumber,
        ERROR_MESSAGE() AS ErrorMessage
       ;
       */
       print('ERROR...');
       print( 'Number: ' +  cast(ERROR_NUMBER() as varchar(50)) );
       print( 'Message: ' + cast(ERROR_MESSAGE() as varchar(max)));


       -- Announce rollback
       print('Rolling back!');
       rollback tran


end catch


TSQL - If object exists (table for example)

if OBJECT_ID('DATABASE_NAME_HERE.SCHEMA_NAME_HERE.TABLE_NAME_HERE', 'U') is not null
begin
       print('The table exists!');
end;


2013-07-05

PHP - UTF-8 fields into arrays


After this, you can use the array properly as it will store the UTF-8 characters appropriately (won't display nulls as it would otherwise). Json_encode will work just fine after this.

while( $row = mysql_fetch_assoc($result,MYSQL_ASSOC) ) {
  $rows[] = array_map( 'utf8_encode', $row );
}