Sunday, April 24, 2011

New Craft Project

DSC_0047

Thanks to a birthday gift card to JoAnn, I’ve been working on a new craft project.  I’ve had my eye on this one for awhile.  It’s kind of what I imagine our daughter will look like as a little girl.  I work on it in the evenings when Desmond is in bed.

As I was thinking about it I realized this is the first cross-stitch I’ve done in about six years!  I go in spurts.  I’ll really get into one project for awhile then it will get old so I’ll move on to something else.  I’ve been a bit knit crazy the past year so it’s time to switch.  It’s been really fun going back to cross-stitch.

Thursday, April 07, 2011

Convert CSV data into a SQL table


Recently I have been working with some report data that is stored statically in a database in CSV format. I had a need to have that CSV data displayed in a tabular format. I searched around and couldn't find anything written to do this in SQL, so I decided to write a stored procedure to do this very thing. It will take a CSV and parse it out into a SQL table. For instance if I have report data that looks like this...

"FirstName","LastName","DOB","Children"
"John","Smith","1/5/1980","Bob,Sally,Joe,Chris"
"Jane","Smith","2/25/1982","Bob,Sally,Joe,Chris"
"Bruce","Wayne","5/13/1975",""
"Peter","Parker","5/23/1970",""
"","","",""

It would be transformed into this ...

FirstNameLastNameDOBChildren
John Smith 1/5/1980 Bob,Sally,Joe,Chris
Jane Smith 2/25/1982 Bob,Sally,Joe,Chris
Bruce Wayne 5/13/1975
Peter Parker 5/23/1970

Note that in the last column the commas are preserved because they are within the quotation mark text qualifiers. Below is the stored procedure that does this. Feel free to use it, and leave a comment if you found it useful.

-- ==========================================================================================
-- Author:  Davin Studer
-- Create date: 4/5/2011
-- Description: This will take a CSV input
-- and transform it into a table
--
-- Params:
-- @string          - The CSV string
-- @textQualifier   - Character that denotes a string within the CSV
-- @columnDelimiter - Character that denotes different columns ... doesn't have to be a comma
-- @rowOneIsHeader  - Does the first row contain column names?
-- ==========================================================================================
create procedure [dbo].[CSVToTable]
    @string nvarchar(max) = '',
    @textQualifier varchar(1) = '',
    @columnDelimiter varchar(1) = ',',
    @rowOneIsHeader bit = 0
as
begin
    -- set nocount on added to prevent extra result sets from
    -- interfering with select statements.
    set nocount on;

    -- We need and input string
    if isnull(@string, '') = ''
    begin
        raiserror ('Please supply an CSV string', 15, 1)
        return
    end
 
    -- We need and column delimiter
    if isnull(@columnDelimiter, '') = ''
    begin
        raiserror ('Please supply a column delimiter.', 15, 1)
        return
    end
  
    -- Make sue the user doesn't pass null as a value
    if isnull(@textQualifier, '') = ''
    begin
        set @textQualifier = ''
    end

    -- Make sue the user doesn't pass null as a value
    if isnull(@rowOneIsHeader, '') = ''
    begin
        set @rowOneIsHeader = 0
    end

    declare
        @columns int = 1,
        @columnNames nvarchar(max) = '',
        @stop bit = 0,
        @position int = 0,
        @temp nvarchar(1) = '',
        @dataStart int = 0,
        @sql nvarchar(max) = '',
        @qualifierToggle bit = 0,
        @tempString nvarchar(max) = '',
        @delimiterReplacementUTFNumber int = 2603

    -- Get rid of the ##tempCSVSplitToTable table if it exists
    if object_id('tempdb..##tempCSVSplitToTable') is not null
    begin
        drop table ##tempCSVSplitToTable
    end

    -- Get rid of white space
    set @string = rtrim(ltrim(@string))

    -- Set the EOL to char(13)
    set @string = replace(@string, char(13) + char(10), char(13))
    set @string = replace(@string, char(10), char(13))

    -- Deal with the delimiter character within the text qualifier characters
    if @textQualifier <> ''
    begin
        while @position <> len(@string)
        begin
            set @temp = substring(@string, @position, 1)
            if @temp = @textQualifier
            begin
                if @qualifierToggle = 0
                begin
                    set @qualifierToggle = 1
                end
                else
                begin
                    set @qualifierToggle = 0
                end
            end
            if @temp = @columnDelimiter and @qualifierToggle = 1
            begin
                set @tempString = @tempString + nchar(@delimiterReplacementUTFNumber) -- replace with UTF delimiter replacement character
            end
            else
            begin
                set @tempString = @tempString + @temp
            end
            set @position = @position + 1
        end

        set @string = @tempString
    end

    -- Get rid of text qualifier ... we don't need it now
    if @textQualifier <> ''
    begin
        set @string = replace(@string, @textQualifier, '')
    end

    -- Get column names
    set @position = 1
    while @stop = 0
    begin
        set @temp = substring(@string, @position, 1)
        if @temp = @columnDelimiter
        begin
            set @columns = @columns + 1
            set @columnNames = @columnNames + ','
        end
        else if @temp = char(13)
        begin
            set @stop = 1
        end
        else
        begin
            set @columnNames = @columnNames + @temp
        end
        set @position = @position + 1
    end

    set @dataStart = @position

    if @rowOneIsHeader = 0
    begin
        set @dataStart = 1
        set @position = 1
        set @columnNames = ''
        while @position - 1 < @columns
        begin
            set @columnNames = @columnNames + ',Column' + cast(@position as varchar(1))
            set @position = @position + 1
        end
        set @columnNames = substring(@columnNames, 2, len(@columnNames))
    end

    -- Build ##tempCSVSplitToTable table
    set @sql = @sql + 'create table ##tempCSVSplitToTable (' + char(13) + '['
    set @stop = 0
    set @position = 1
    while @stop = 0
    begin
        set @temp = substring(@columnNames, @position, 1)
        if @temp <> ','
        begin
            set @sql = @sql + @temp
        end
        else
        begin
            set @sql = @sql + '] nvarchar(max),' + char(13) + '['
        end
   
        set @position = @position + 1

        if @position - 1 = len(@columnNames)
        begin
            set @sql = @sql + '] nvarchar(max)'
            set @stop = 1
        end
    end
    set @sql = @sql + ')' + char(13)
    exec (@sql)

    -- insert values into ##tempCSVSplitToTable table
    set @position = @dataStart
    while @position - 1 < len(@string)
    begin
        set @stop = 0
        set @sql = 'insert into ##tempCSVSplitToTable ([' + replace(@columnNames, ',', '],[') + ']) values ('''
        while @stop = 0
        begin
            set @temp = substring(@string, @position, 1)

            -- end of column
            if @temp = @columnDelimiter
            begin
                set @sql = @sql + ''','''
            end
            -- EOL
            else if @temp = char(13) or datalength(@temp) = 0
            begin
                set @stop = 1
            end
            -- deal with apostrophe in data
            else if @temp = ''''
            begin
                set @sql = @sql + ''''''
            end
            -- column data that isn't an apostrophe
            else
            begin
                set @sql = @sql + @temp
            end

            set @position = @position + 1
        end 
        set @sql = @sql + ''')'

        -- Get rid of any UTF delimiter replacements that were put in to take the place of the delimiter character within the text qualifier
        if @textQualifier <> ''
        begin
            set @sql = replace(@sql, nchar(@delimiterReplacementUTFNumber), @columnDelimiter)
        end

        exec(@sql)
    end

    select * from ##tempCSVSplitToTable

    -- destroy ##tempCSVSplitToTable table
    if object_id('tempdb..##tempCSVSplitToTable') is not null
    begin
        drop table ##tempCSVSplitToTable
    end
end

Saturday, April 02, 2011

Getting Ready for Baby

IMG_2935IMG_2941

Last weekend my parents came up and helped me paint the nursery.  It’s now a pretty lilac color and I’ve had fun sewing curtains and decorating it.  Desmond has been helping me along the way, and he likes all the baby things that are coming out of boxes.  Speaking of boxes – I emptied one and made it into a house for him.  It’s been a huge hit.

IMG_2946

Today is our last free, unscheduled day before baby arrives.  Weird!  April weekends are full of Tae Soo Do events (Davin’s black belt ceremony and a tournament) and family birthdays.  There are so many things I could do today… or I might just relax.

For more baby news and health updates check out my pregnancy blog.

Thursday, March 24, 2011

Little Boy Blue

Desmond has officially moved back into his bedroom!  We moved him out of the “burgundy room” fairly early because it reeked of cat pee.  I started painting it in September, and then it sat untouched for awhile through early pregnancy discomforts and a-fib episodes.  I worked on it here and there, ripping out trim, sanding, retexturing, pulling out carpet, and sealing the subfloor.  Last week we finally got our new carpet installed and Davin put in the trim (we just have the closet and IMG_2918window trim left to do).  Today I finished up touch ups, and Desmond helped me move in the furniture.  He was so excited!  I was, too.

With this transition back into the “blue room” we are struck with how big Desmond is getting.  He’s such a little boy and no longer a baby.  Getting a new baby in a few weeks will change so much in our family dynamics.  We’re phasing out the things baby will use such as the gliding rocker that will go in the nursery, but it was sad not to rock Desmond tonight.  Our little boy is growing up!  What a mix of sadness and joy all at once.

Saturday, March 12, 2011

Cute Sayings

Desmond hiding behind the curtain: "I'm a Jertain!" While reviewing our phone number with Desmond, Davin says it and Desmond replys, "Good job, Daddy." The next time Davin says it Desmond says, "Good job, Daddy, again." The third time Davin prompts him to repeat it, Desmond says, "Good job, Daddy, again and again." At Sweet Tomatoes Desmond was playing around rather than eating. Davin was trying to get him to finish his dinner and rather than saying it verbally again, he simply pointed to Desmond's food. Desmond then turned with a smile and pointed to Davin's food. We got a good laugh at his imitation. While making dinner Desmond was helping me roll out the dough and he said, "Dough. Doe a deer!" He's always making new associations. I'm not sure where he picked up this word because it's not like we're saying it all the time but in the car Desmond all of a sudden blurts out, "Husband." I told him that Daddy was my husband so he repeated, "Daddy is my husband." Later at dinner I was telling Davin about it and Desmond said again, "Daddy is my husband." We corrected him and told him that Davin was not his husband but his daddy and he was only my husband. Desmond was about to cry because he wanted Daddy to be his husband. We kept explaining about family relationships: daddy, mommy, son, husband, wife, brother, sister. Desmond just ended up confused but at least we avoided tears.

Wednesday, March 09, 2011

Our Not-So-Little Desmond

It’s hard to believe that very soon we will have two children!  I am looking forward to lightening my load and getting this body back in shape again.  Desmond likes to give baby sister kisses.  I think she’ll need to get used to getting lots of love. 

IMG_2914IMG_2915

I’ve been struck with how big Desmond is getting lately.  Not only has he grown half an inch in the last 6 weeks (making him 38.5 inches tall), but he continues to surprise me with the things he says and his growing skills.  He can dress and undress himself, count to 29, and recognizes numbers 0-10 which is handy when we tell him he can’t get up until his clock says 7.  He knows all his letters including some letter sounds and can spell his name.  He also just learned our phone number. 

Recently a car honked it’s horn and Desmond said, “Somebody pushed the trumpet button.”  He also reminds me to take smaller bites and made a joke the other day by saying “cheese” instead of “please.”  He makes us laugh on a regular basis.  I should be better at writing these things down because I forget most of it.

IMG_2900Last weekend we went down to Albany to celebrate the Boydston March birthdays: Dad – March 1, Grandpa – March 4, Daniel – March 6, and me – March 15.  We had gifts and dinner at the Sizzler.  Here’s Desmond giving Nana an eskimo kiss.  He has a routine: hug, kiss, eskimo kiss, high five, knuckles, thumbs up.  We’re getting everyone trained in that.

Thursday, February 10, 2011

My Little Puzzler

Desmond takes after me in his love for puzzles.  Yesterday we had fun putting together every jigsaw puzzle he has.  We have long since left the wooden peg puzzles in favor of jigsaw puzzles.  A few months ago I did a 1000-piece puzzle and while I was sorting Desmond put together the first three pieces on his own.  He can do 48 piece puzzles all by himself now, so I’m thinking of getting even bigger ones for him. 

It was Desmond’s idea to not wear clothes so that’s why he’s only in a diaper and socks.  And yes, he’s back in diapers.  Our last potty training session lasted eight weeks, but he was still having lots of major accidents, and I can only remember about four times where he told us he needed to go potty.  My hospital stay totally derailed everything so it is back to diapers for now.

IMG_2863IMG_2868

IMG_2864IMG_2873

Monday, February 07, 2011

Financial (or should I say “Faith”) Testing

The second half of last year was expensive with a house purchase, unexpected hospital bills, a new roof, and a major vehicle repair. Well, the trend is continuing into 2011. We’ve been trimming down our budget for the last few months wherever we can, but it seems every little bit we save gets eaten up and then some by some unavoidable bill. We got zinged with another big hospital bill (have to put it on a payment plan this time), a car that failed emissions (there goes another couple hundred) and then decided to break down completely yesterday (actually make that closer to a thousand). Ack! At least I already had an appointment for the emissions work so we knew right where to have it towed.

The sermon yesterday talked about difficulties driving us to dependence on God rather than ourselves. I feel like waving my arms and saying, “Um, God, do you see what we’ve already had to pay for? Do you see what we have left? Do you see my pregnant belly ticking down to even more expenses?” His reply to me is, “Do you see what I’ve already provided? Do you trust me to keep my promise and continue to provide?” Yes, thank you for the Christian mechanic, the clearance baby clothes, Davin's mom giving us AAA, and for the previous house owners finally paying off their water bill. Of course God knows exactly what we’re facing and He’s asking us to trust Him. It’s all His money anyway and if this is how He wants to spend it, then okay. We’re just trying to make the best decisions we can with what we’ve been given. Here’s hoping for a nice tax return and that we can qualify for the kids to be on Washington’s Apple Health for Kids health insurance program.

Saturday, January 29, 2011

Sledding Day

IMG_2830IMG_2827

IMG_2844IMG_2843

This morning after our toddler alarm clock got us up, we got ready, packed our snow stuff and headed to Mt. Hood for some sledding.   Thankfully Desmond’s snow pants still fit and some borrowed snow boots worked if we took the fleece inserts out. 

Last year we took Desmond sledding for the first time and he didn’t like it, but this year was a whole different story.  He was climbing in the snow while Davin and I got our gear on, and he laughed and kept saying, “This is fun!” as Davin pulled him along on the sled.  We found a good gentle hill to ourselves and Desmond even took a few runs on his own.  Even after two hours of snow play he didn’t want to leave this time.

I felt a bit strange with my pregnant belly hanging out of my unzipped snow pants (thankfully they have suspenders so they stayed up), and it was quite a work out pulling Desmond in the sled back up the hill after our runs together, but it was so much fun!  We’ll have to try to go back before the winter is over.

Sunday, January 16, 2011

Atrial Fibrillation Revisit

At the end of lunch on Tuesday I felt my heart start racing. “Uh-oh,” I thought and paused to pay attention to what my heart was doing. Desmond was finished with lunch at this point and was playing under the table. It took me a minute to realize he just peed and pooped while I wasn’t paying attention. (He likes to go under or behind things.)

So I was cleaning up poop while on the phone with Kaiser advice. The nurse on the other end was calm until I mentioned I was also 24 weeks pregnant. Then she sounded a bit flustered and wasn’t sure who to transfer me to. We got it figured out though and as I thought they wanted me to go to the ER. “Here we go again!” I thought as I packed a bag of entertainment and snacks for Desmond and an overnight bag for me with my knitting, reading and personal items I missed last time I was in the hospital. I felt winded going up and down the stairs.

Davin rushed home because I’m not supposed to drive in a-fib. It was nice not doing this in the middle of the night, but the inconvenience this time was Desmond missing his nap. Still he was a trouper and did great. He remembered last time and wasn’t quite as worried this time around. He did cry when they left me for the night, though.

It was nice having familiarity this time which made it a bit less scary. I saw the same ER doctor and the same Kaiser doctor as last time though they see enough people they didn’t remember me. My heart wasn’t beating quite as intensely as last time though it was still up in the 100-120s most of the time with higher spikes into 160s.

The doctors talked sooner about cardioversion this time. We wrestled with whether or not to try shocking me out of it, but I didn’t like the idea of shocking the baby. The other option was trying to control my heart rate with more medication, but that gets to the baby too, so I really felt stuck. They said either option was most likely safe for the baby, but there aren’t any studies on pregnant women so they didn’t really know what option to suggest over the other. In the end we chose not to do the cardioversion because my heart could go right back into a-fib and then we’d be back in the same boat needing medication anyway. My prenatal doctor I’ve been seeing was told I was there and stopped in during her shift. It was perfect timing and I felt comforted to talk to her and go through my options again.

All through Wednesday they had me on more Metoprolol, the beta blocker I’ve been on since September. There wasn’t really any change though. I think it was Wednesday evening when they started Diltiazem, a calcium blocker to see if that would drop my heart rate. The only problem was my blood pressure. The lowest I saw it was 83/44 and I could tell it made the nurses nervous when the systolic pressure got below 100 so sometimes they wouldn’t give me any more meds. The cardiologist came in on Thursday morning and “cracked the whip” as he said to tell them to keep giving me the medicine as long as I felt okay. I’m a lot younger than their usual patients so I can handle a lower blood pressure. I never felt dizzy or lightheaded though when I saw the low blood pressure numbers I felt like I should have felt something different, but I felt fine. The Diltiazem started working (I was on an IV to get a quick and steady dose) and my heart rate went down a little though it was still high I was still in a-fib.

Mom came up on Thursday to see me and help out with Desmond. That was a huge help. Our friend Sarah was able to watch Des on Wednesday and he had fun playing with Chloe all day. Thursday and Friday he got lots of time with Nana and that freed up Davin to stay with me.

On Thursday evening while Davin was settling everyone in at home my heart went back into a regular rhythm. It was 6:18 pm and the nurse who worked all three nights I was there came in and gave me the good news. Whoohoo! It lasted 54 hours this time; the longest yet. I can feel when a-fib starts but it kind of slips back to normal quietly so I can’t tell the moment that happens. They still wanted to keep me one more night to transfer me from the IV to the pills and see how I tolerated the medication.

I rested much easier that night and early Friday morning they said I could go home and I was discharged around 9:30 am. It felt so good to get all unhooked from everything, especially the circulation leg cuffs that squeezed me for three days. I have a sore, itchy rash from those because my legs sweat so badly.

I’m so relieved to be back in a normal heart rhythm. It just doesn’t feel good to have a racing, skipping heart when I’m just resting. It’s like when you get startled and it makes your heart jump and beat fast except it never calms down.

The downside is that now I’m on all this medication. They’re being a bit more aggressive now that I’ve demonstrated the ability to go back into a-fib. I’m taking an aspirin a day to thin my blood since a-fib increases my chance for a stroke. (I turned down the blood thinner injections they suggested.) I just hope thinner blood doesn’t negatively affect my labor and delivery. I’m also on more Metoprolol, three times a day instead of two which can cause a low birth rate and they added the Diltiazem which can cause cardiovascular birth defects. The pharmacist was really concerned to give me my medication since I am pregnant, but it’s the safest stuff they’ve got.

I’m looking forward to seeing my prenatal doctor on Tuesday to go over how things are now and how the next few months might be. I wish I was delivering sooner so the baby wouldn’t have to be on my medications so long. At least I’m in the second trimester where a lot of the major development has already happened. It’s nice to know a good and all-powerful God is in control of both my heart beat and the baby’s growth. I just sure hope the next time I’m in the hospital is to meet my baby.

P.S. We hoped to hold off until May to meet my deductible and out-of-pocket maximum for the year since we're still recovering from our new roof, but here it is. We'll be paying it now instead. The upside is that there's a good chance the labor and delivery will be free now.