Friday, January 10, 2014

[MSSQL][Library] Convert number to text in Bahasa Indonesia (udf__Terbilang)

USE <Database_Name>
GO
IF OBJECT_ID(N'udf__TerbilangHundred') IS NOT NULL
  DROP FUNCTION udf__TerbilangHundred
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- *****************************************************************************
-- Name         : udf__TerbilangHundred 
-- Author       : Chandra Gunawan
-- Date         : 19-Nov-11
-- Description  : Return the Terbilang (number to text in Bahasa)
--                for each hundred base
-- Parameters   : 
--     @pHundedValue decimal  Integer value in hundred base
--
-- Usage Sample :
--   -------------------------------------------------
--   SELECT dbo.udf__TerbilangHundred(121) 
--   --> Result: Seratus Dua Puluh Satu
--   -------------------------------------------------
--
-- Maintenance Log
-- =============================================================================
-- Date        ID     Description
-- -----------------------------------------------------------------------------
-- 19-Nov-11   chg    Initial Revision
-- *****************************************************************************
CREATE FUNCTION [dbo].[udf__TerbilangHundred]
(  
  @pHundredValue AS decimal
) 
RETURNS varchar(200) AS 
BEGIN
  DECLARE @iS     integer, 
          @iP     integer, 
          @iR1    integer, 
          @cTS    varchar(200),
          @cST    varchar(200),
          @cTP    varchar(200), 
          @cBil   varchar(200), 
          @cHasil varchar(200)
          
  SELECT @cST  = RIGHT(RTRIM(STR(@pHundredValue + 1000)), 3),
         @iS   = CAST(RIGHT(@cST, 1) AS integer),
         @iP   = CAST(SUBSTRING(@cST, 2, 1) AS integer),
         @iR1  = CAST(LEFT(@cST, 1) AS integer),
         @cBil = 'Satu    Dua     Tiga    Empat   Lima    ' +
                 'Enam    Tujuh   Delapan Sembilan'
   
  IF @iS = 0
    SET @cTS = ''
  ELSE
    SET @cTS = RTRIM(SUBSTRING(@cBil, @iS * 8 - 7, 8)) + ' '
     
  IF @iP = 0
    SET @cTP = @cTS
  ELSE
    IF @iP = 1
      IF @iS = 0
        SET @cTP = 'Sepuluh '
      ELSE
        IF @iS = 1
          SET @cTP = 'Sebelas '
        ELSE
          SET @cTP = RTRIM(SUBSTRING(@cBil, @iS * 8 - 7, 8)) + ' Belas '
    ELSE
      SET @cTP = RTRIM(SUBSTRING(@cBil, @iP * 8 - 7, 8)) + ' Puluh ' + @cTS
     
  IF @iR1 = 0
    SET @cHasil = @cTP
  ELSE
    IF @iR1 = 1
      SET @cHasil = 'Seratus ' + @cTP
    ELSE
      SET @cHasil = RTRIM(SUBSTRING(@cBil, @iR1 * 8 - 7, 8)) + ' Ratus ' + @cTP
      
  RETURN @cHasil
END
GO
GRANT EXECUTE ON dbo.udf__TerbilangHundred TO public
GO

IF OBJECT_ID(N'udf__Terbilang') IS NOT NULL
  DROP FUNCTION udf__Terbilang
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- *****************************************************************************
-- Name         : udf__Terbilang 
-- Author       : Chandra Gunawan
-- Date         : 19-Nov-11
-- Description  : Return the Terbilang (number to text in Bahasa)
--                for input decimal value
-- Parameters   : 
--     @pNumber   dec        Value to be converted
--     @pCurrency vchar(10)  (optional) The currecncy name - default: Rupiah
--     @pCent     vchar(10)  (optional) The cent name - default: Sen
--
-- Usage Sample :
--   -------------------------------------------------
--   SELECT dbo.udf__Terbilang(1231211.34, default, default)
--   --> Result: Sebelas Ribu Dua Ratus Sebelas Rupiah Tiga Puluh Empat Sen
--   -------------------------------------------------
--
-- Maintenance Log
-- =============================================================================
-- Date        ID     Description
-- -----------------------------------------------------------------------------
-- 19-Nov-11   chg    Initial Revision
-- *****************************************************************************
CREATE FUNCTION [dbo].[udf__Terbilang]
(
  @pNumber   decimal(19,2),
  @pCurrency varchar(10) = 'Rupiah',
  @pCent     varchar(10) = 'Sen'
) 
RETURNS varchar (200) AS 
BEGIN
  DECLARE @dB       decimal, 
          @dR       decimal, 
          @dJ       decimal, 
          @dM       decimal, 
          @dT1      decimal, 
          @dKoma1   decimal, 
          @iKoma2   integer, 
          @cHasil   varchar(200),
          @cS_T     varchar(200),
          @cRibu    varchar(200),
          @cJuta    varchar(200),
          @cMiliar  varchar(200),
          @cTx      varchar(200),
          @cAngka1  varchar(200),
          @cTx1     varchar(200),
          @cZ       varchar(250)

  SET @cS_T = CASE 
               WHEN Len(RTRIM(CAST(CAST(@pNumber AS bigint) AS varchar(20)))) < 15
                 THEN REPLICATE('0',15 - Len(RTRIM(CAST(CAST(@pNumber AS bigint) AS varchar(20))))) + RTRIM(CAST(CAST(@pNumber AS bigint) AS varchar(20)))
               ELSE RTRIM(CAST(CAST(@pNumber AS bigint) AS varchar(20)))
             END
             
  SET @cZ = CAST(15 - Len(RTRIM(CAST(CAST(@pNumber AS integer) AS varchar(20)))) AS varchar(20)) +
           RTRIM(CAST(CAST(@pNumber AS integer) AS varchar(20)))
           
  SET @dR = CAST(RIGHT(@cS_T, 3) AS integer)
  SET @dB = CAST(SUBSTRING(@cS_T, 10, 3)  AS integer)
  SET @dJ = CAST(SUBSTRING(@cS_T, 7, 3)  AS integer)
  SET @dM = CAST(SUBSTRING(@cS_T, 4, 3)  AS integer)
  SET @dT1 = CAST(LEFT(@cS_T, 3)  AS integer)

  SET @cRibu   = CASE 
                   WHEN @dB = 0 THEN ' ' 
                   ELSE 
                     CASE 
                       WHEN @dB = 1 THEN 'Seribu' 
                       ELSE [dbo].udf__TerbilangHundred(@dB) + 'Ribu' 
                     END 
                 END
  
  SET @cJuta   = CASE 
                   WHEN @dJ = 0 THEN ' ' 
                   ELSE [dbo].udf__TerbilangHundred(@dJ) + 'Juta ' 
                 END

  SET @cMiliar = CASE 
                   WHEN @dM = 0 THEN ' ' 
                   ELSE [dbo].udf__TerbilangHundred(@dM) + 'Miliar' 
                 END
                
  SET @cTx = CASE
               WHEN @pNumber < 1000 --Ratusan
                 THEN [dbo].udf__TerbilangHundred(@dR)
               WHEN @pNumber < 1000000 --Ribuan
                 THEN @cRibu + ' ' + [dbo].udf__TerbilangHundred(@dR)
               WHEN @pNumber < 1000000000 --Jutaan
                 THEN [dbo].udf__TerbilangHundred(@dJ) + '' + RTRIM('Juta ' + @cRibu) + ' ' + [dbo].udf__TerbilangHundred(@dR)
               WHEN @pNumber < 1000000000000 --Miliaran
                 THEN [dbo].udf__TerbilangHundred(@dM) + '' + RTRIM('Miliar ' + @cJuta + @cRibu) + ' ' + [dbo].udf__TerbilangHundred(@dR)
               WHEN @pNumber >= 1000000000000 --Trilliun
                 THEN [dbo].udf__TerbilangHundred(@dT1) + '' + RTRIM('Trilliun ' + @cMiliar + @cJuta + @cRibu) + ' ' + [dbo].udf__TerbilangHundred(@dR)
             END
            
  SET @cAngka1 = RTRIM(CAST(@pNumber AS varchar(20)))
  SET @dKoma1 = CASE WHEN charindex('.', @cAngka1) = 0 
                  THEN 0 
                  ELSE CAST(RTRIM(SUBSTRING(@cAngka1, charindex('.',@cAngka1) + 1, 2)) AS integer) 
                END
  SET @iKoma2 = Len(CASE WHEN charindex('.',@cAngka1) = 0 
                      THEN '0' 
                      ELSE RTRIM(SUBSTRING(@cAngka1, charindex('.',@cAngka1) + 1, 2)) 
                    END)

  IF @dKoma1 > 0
  BEGIN
    IF @dKoma1 < 10 AND @iKoma2 = 1
      SET @dKoma1 = @dKoma1 * 10

    SET @cTx1 = [dbo].udf__TerbilangHundred(@dKoma1)
    SET @cHasil = @cTx + @pCurrency + ' ' + @cTx1 + @pCent
  END
  ELSE
    SET @cHasil = @cTx + @pCurrency

  RETURN (@cHasil)
END
GO
GRANT EXECUTE ON dbo.udf__Terbilang TO public
GO

[MSSQL][Library] Substitute parameter in a text (udf__SubsTextParam)

USE <Database_Name>
GO
IF OBJECT_ID(N'udf__SubsTextParam') IS NOT NULL
  DROP FUNCTION udf__SubsTextParam
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- *****************************************************************************
-- Name         : udf__SubsTextParam 
-- Author       : Chandra Gunawan
-- Date         : 14-Sep-12
-- Description  : Return the substituted text parameter
-- Notes        : Format parameter in text is {}
--                 is sequnce number of parameter list, starting with 0
--                e.g.: @pText      => Customer {0} activated on {1}
--                      @pParamList => 12000001,1-Jan-2012
--                      @pDelimiter => ,
--                      RETURN      => Customer 12000001 activated on 1-Jan-2012
--
-- Usage Sample :
--   -------------------------------------------------
--   SELECT dbo.udf__SubsTextParam('Customer {0} activated on {1}', 
--                                      '12000001,1-Jan-2012',
--                                      ',')
--   -------------------------------------------------
--
-- Maintenance Log
-- =============================================================================
-- Date        ID     Description
-- -----------------------------------------------------------------------------
-- 14-Sep-12   chg    Initial Revision
-- *****************************************************************************
CREATE FUNCTION [dbo].[udf__SubsTextParam](
  @pText       varchar(1000),    -- Input Text
  @pParamList  varchar(500),     -- Parameter List
  @pDelimiter  varchar(5) = ','  -- Delimiter
  )
RETURNS varchar(2000)
AS
BEGIN
  DECLARE @i       int,
          @j       int,
          @cParam  varchar(500)

  -- Process substitution
  SET @j = -1
  WHILE @pParamList <> ''
  BEGIN
    SELECT @j = @j + 1,
           @i = CHARINDEX(@pDelimiter, @pParamList)

    IF @i = 0
      SELECT @cParam = @pParamList,
             @pParamList = ''
    ELSE
      SELECT @cParam = SUBSTRING(@pParamList, 1, @i - 1),
             @pParamList = SUBSTRING(@pParamList, @i + LEN(@pDelimiter), LEN(@pParamList))
             
    SET @pText = REPLACE(@pText, '{' + CONVERT(varchar, @j) + '}', @cParam)
  END
  
  RETURN @pText
END
GO
GRANT EXECUTE ON dbo.udf__SubsTextParam TO public
GO

[MSSQL][Library] Convert comma separated list of item into single column table (udf__ListToSingleColumn)

USE <Database_Name>
GO
IF OBJECT_ID(N'udf__ListToSingleColumn') IS NOT NULL
  DROP FUNCTION udf__ListToSingleColumn
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
-- *****************************************************************************
-- Name         : udf__ListToSingleColumn
-- Author       : Chandra Gunawan
-- Date         : 02-Jun-2009
-- Description  : Convert comma seperated list of item to table
-- Parameters   : 
--     @pList       vchr(8000)  Delimited list of item to split
--                                format: []
--                                example: a,b,c
--     @pDelimiter  vchr(1)     Delimiter (dlm)
--
-- Usage Sample :
--   -------------------------------------------------
--   SELECT * FROM dbo.udf__ListToSingleColumn('a,b,c', ',')
--   -------------------------------------------------
--
-- Maintenance Log
-- =============================================================================
-- Date        ID     Description
-- -----------------------------------------------------------------------------
-- 02-Jun-09   chg    Initial Version
-- *****************************************************************************
CREATE FUNCTION dbo.udf__ListToSingleColumn 
( 
    @pList      varchar(8000),
    @pDelimiter varchar(1) = ','
) 
RETURNS @pTable TABLE 
( 
    item varchar(1000) 
) 
BEGIN  
  DECLARE @iSpot smallint,
          @cStr  varchar(8000),
          @cSql  varchar(8000)  
     
  IF ISNULL(@pDelimiter, '') = '' SET @pDelimiter = ','

  WHILE @pList <> ''  
  BEGIN  
    SET @iSpot = CHARINDEX(@pDelimiter, @pList)  
    IF @iSpot > 0  
    BEGIN  
      SET @cStr = LEFT(@pList, @iSpot - 1)  
      SET @pList = RIGHT(@pList, len(@pList) - @iSpot)  
    END  
    ELSE BEGIN  
      SET @cStr = @pList  
      SET @pList = ''  
    END  
    INSERT @pTable SELECT @cStr 
  END  
  RETURN 
END 
GO
GRANT SELECT ON udf__ListToSingleColumn TO public
GO

Monday, January 6, 2014

[Tool] How To Create an ISO Image File From a CD, DVD, or BD

Make an ISO Image From any Disc


Creating an ISO file from any disc is pretty easy with the right free tool and is a fantastic way to backup important CDs, DVDs, or BDs to your hard drive.
Creating and storing ISO backups of your important software installation discs, and even operating system setup discs, is a smart plan. Compliment that with an unlimited online backup service and you have a near bulletproof disc backup strategy.

ISO images are great because they're self contained, perfect representations of the data on a disc. Being single files, they're easier to store and organize than simple copies of the folders and files on a disc would be.

Windows doesn't have a built-in way of creating ISO image files so you'll need to download a program to do it for you. Luckily there are several free tools available that make creating ISO images a really easy task.

Difficulty: Easy

Time Required: Creating an ISO image file from a CD, DVD, or BD could take anywhere from a few minutes to over a hour, depending on the size of the disc and the speed of your computer

Here's How:

  1. Download BurnAware Free, a completely free program that, among other tasks, can create an ISO image from all types of CD, DVD, and BD discs.

    Important: During installation, there's a Sponsored Offer screen that "recommends" that you install a browser toolbar. Feel free to unselect that option and continue.

    Note: There are also "Premium" and "Professional" versions of BurnAware that are not free. However, the "Free" version is fully capable of creating ISO images from your discs, which is the aim of this tutorial.

    BurnAware Free works in Windows 8, Windows 7, Windows Vista, Windows XP, and even Windows 2000 and NT. Both 32-bit and 64-bit versions of those operating systems are supported.
     
  2. Install BurnAware Free by executing the burnaware_free file you just downloaded.

    Note: BurnAware Free tries to get you to install a browser toolbar from AVG during installation. If you're not interested in doing that, you can skip the add-on by choosing Custom installation and making sure all three boxes are unchecked. You may also want to uncheck the Add BurnAware to AutoRun menu option near the end of the install.
     
  3. Run BurnAware Free, either from the shortcut created on the Desktop, or automatically via the last step in the installation.
     
  4. Once BurnAware Free is open, click on Copy to Image.

    The Copy to Image tool will appear in addition to the existing BurnAware Free window that's already open.

    Tip: You might have seen a Make ISO icon next to Copy to Image but you don't want to choose that for this particular task. The Make ISO tool is for creating an ISO image not from a disc, but from a collection of files you select, like from your hard drive or other source.
     
  5. From Source, choose the optical disc drive that you plan on using. If you only have one drive, you'll only see one choice.
     
  6. Click or touch Browse....
     
  7. Navigate to the location that you want to create to ISO image file to, give the soon-to-be-made file a name in the File name text box, and then click or touch on Save.

    Note: Optical discs, especially DVDs and BDs, can hold several gigabytes of data and will create ISOs of equal size. Make sure that whatever drive you choose to save the ISO image to has enough room to support it. Your primary hard drive likely has plenty of free space so choosing a convenient location there, like your Desktop, as the location to create the ISO image is probably fine.

    Important: If your ultimate plan is to get the data from a disc onto a flash drive so you can boot from it, please know that simply creating an ISO file directly onto the USB device isn't going to work as you expect. In most cases, like when installing Windows 7 from a flash drive, you have to take some extra steps to make sure the flash drive is properly formatted and the files are correctly copied to the drive.
     
  8. Insert the CD, DVD, or BD disc that you want to create the ISO image from into the optical drive you chose in Step 5.

    Note: Depending on how AutoRun is configured in Windows on your computer, the disc you just inserted may start (e.g. the movie may start playing, you may get a Windows installation screen, etc.). Regardless, close whatever comes up.
     
  9. Click or touch Copy.

    Tip: Do you get a There is no disc in the source drive message? If so, just click or touch OK and then try again in a few seconds. Chances are, the spin-up of the disc in your optical drive hasn't completed so Windows just doesn't see it yet. If you can't get this message to go away, make sure you're using the right optical drive and that the disc is clean and undamaged.
     
  10. Wait while the ISO image is created from your disc. You can watch the progress by keeping an eye on the Image progress bar or the x of x MB written indicator.
     
  11. The ISO creation process is complete once you see the Copy process completed successfully box.

    The ISO file will be named and located where you decided in Step 7.
     
  12. You can now press or click OK to that message, close the Copy to Image window, and also the BurnAware Free window. You can also now remove the disc you were using from your optical drive.

Tips:

  1. You can only create ISO images from discs that your optical drive supports. For example, if you only have a CD drive, you won't be able to make ISO images from DVD or BD discs because your drive won't be able to read the data from them.
     
  2. While you won't be able to follow my tutorial above exactly, there are several other free ISO creation tools available if you don't like BurnAware Free or it's not working for you. Some include ISODisk, ImgBurn, ISO Recorder, and Free DVD to ISO Maker.

Tuesday, February 12, 2013

[excel] How to force a newline in formula?

Case:
I want to bring together three columns of text into 1 column. For example:
A = CommentA
B = CommentB
C = CommentC

If you concatenate (=A&B&C) you get CommentACommentBCommentC. I know you can put in characters and spaces using "" inbetween the & (e.g =A&","&B&","&C), but is there a way to get excel to move the next comment onto a new line within the same cell so it looks like:
CommentA
CommentB
CommentC

Answer:
1. Try using the function CHAR to generate a newline character in your formula. It should look something like this ...
=A1&CHAR(10)&B1&CHAR(10)&C1

2. You will probably need to format the cell so that alignment has Text Wrap activated.

Monday, January 28, 2013

[mssql] mdf is Compressed But Does not Reside in a Read-only Database or Filegroup. The File Must be Decompressed

Last night I loaded a massive ASP.NET 2.0 project I did some years ago in VB.NET. It used a SQL 2000 database for data storage, so I had to attach the mdf file to get it to work. So I opened SQL Server Management Studio, opened a connection to the server instance, and went for the Attach database option.

When I selected the file, the following error came up:

“Store.mdf is Compressed But Does not Reside in a Read-only Database or Filegroup. The File Must be Decompressed.”

So here’s a quick tip for anyone who faces a similar issue. It took a few seconds to understand the problem but then I remembered file compression options. Opened my mdf file properties dialog box, and in the advance tab the problem was revealed: the “compress content” checkbox was checked.

After unchecking and saving changes, SQL Server accepted the attach with no problem whatsoever.