Popular Posts
-
Adam, Adam, thou art perfect before me. Thou shalt not lose thy children nor shalt thou see the bars of prison, jail, or cell ever again. ...
Sunday, November 10, 2019
Sunday, November 10, 2019
Agency is the most beautiful part of the gospel of Jesus Christ. Because of it, as Joseph Smith said we are free. Parents of children want to take away agency when they know they will not make the right guy choice. But it is curious that the same parents who want to harbor their children from the burden of choosing wrong also are the ones who shy away and seek to protect their children from the effects of those choices. Heavenly Father knows we cannot become perfect unless we are free. His plan works because he dies not hide us from the effects of our choices, that is, unless and to the degree which we repent.
Saturday, November 9, 2019
Journal Saturday, November 9 2019
Last night I thought about how stable God is. He plants his promises in our hearts and watches them grow. As men, we foolishly think that God will not come through. We think he has forgotten about us. We think that the creator of heaven and Earth will somehow not able to come through on the things he has promised us. How foolish we are? God does not forget he does not sorrow or sleep he always keeps his word. Last night was really nice I had peace in my heart as I walked to my children's room. I looked out the window at the top of the stairs and saw a star. I knew that the man who put that star in the sky would surely keep his promises he made to me. Joey was very cute last night she made a train set that went through the small dollhouse. She got upset when Lily was playing with him and tried to change the layout of the track. But I told him he would have to take a bench if he could not play with Lily. He immediately improved.
We watched avengers and then Joey went up to his bed. I came upstairs and prayed for him that he would have all that he needs and that his life would be filled with love. I prayed for Lily for the special girl that she is and the light that I see in her heart that I would be able to Foster that light and help her grow. I prayed for Maylee for the wonderful girl that she is and that her dreams would come true and that the tooth fairy would visit her.
The tooth fairy did come last night. Jerry also called me last night as I was falling asleep. I knew it was late so I did not answer. Jerry was out with Brianne last night at the Irvine spectrum she and Jerry had a good time eating and being with friends. I just texted Jerry if she wanted to do lunch. She has not responded.
I am here on the temple grounds. After I drop the children off for their visit I thought I might go and get Jeff to to learn c programming some more. I also thought to go pick up breakfast and bring it to Jerry. But I knew that I am not allowed to go to Jerry's house. It is a good rule. As I drove home listening to general conference, I felt prompted to drive down the 55 freeway. I thought about learning to fly. I thought about going to my work. But then I thought I need to go to the temple and feast on the words of Christ for they will tell me as the promise says all things that I should do.
Saturday, November 7, 2015
SQL Rules of Coding
If you ever google sql for loop, just stop right there.
SQL is by definition a for loop.
A SELECT statement does nothing more than iterate over the rows in the database that satisfy the conditions of the query.
Therefore, if you want a for loop that sets a variable and then does some operation on one row at a time, JUST STOP. What you really want is a join somehow, and you can get it all done much simpler in terms of code and processing power. It will be easier to debug and to understand, and you won't have to worry about setting up the attendant variables to process a true for loop. SQL is a for loop, use it that way!
SQL is by definition a for loop.
A SELECT statement does nothing more than iterate over the rows in the database that satisfy the conditions of the query.
Therefore, if you want a for loop that sets a variable and then does some operation on one row at a time, JUST STOP. What you really want is a join somehow, and you can get it all done much simpler in terms of code and processing power. It will be easier to debug and to understand, and you won't have to worry about setting up the attendant variables to process a true for loop. SQL is a for loop, use it that way!
Friday, June 12, 2015
Rules of Coding
Rules of Coding
- The most important language you will ever code in: English (or the common spoken language used in your company). -- So that everyone understands what you wrote and why.
- Never use a regular expression without saying in plain English what it is doing. Regex is so hard to understand!!! Example: what the heck is this doing. Found in the gruntfile.js of bootstrap at https://github.com/twbs/bootstrap/blob/master/Gruntfile.js return string.replace(/[-\\^$*+?.()|[\]{}]/g, '\\$&');
- If you are going to modify something extensively, you are better off writing a new one and swapping it in when it is ready to go live. For example, if you are extensivley modifying an email template that is currently being used in production, just make a new template with a creative name and use that, so you don't possibly mess up code that is still using the old template. --This way you can also keep a clean backup of the old template and be able to rollback quickly.
- No one cares. No matter how much you think a given coding principle is important, there is nothing worse than alienating your fellow co-workers by sticking to a principle. It may still be important, but no one cares in the end. Try living without it for a day and you'll find you are still alive.
- Don't bother committing code if you haven't tested it. There are runtime errors and compile time errors. So even if your code builds, it doesn't mean it's even going to work. This will save you time in the end, believe me. You will end up having to change your code that doesn't work, or rollback a feature that you comitted and later find out will never work (e.g. I added logic to update the CreatedDate of some table in the database, and come to find out you can't update a database autopopulated field. So I get to rollback the "addition" I made across 5 files).
Friday, June 5, 2015
Using XML to pass dynamic paramters to SQL stored procedure.
You can use an XML string to pass any number of options to a SQL stored procedure like so
Use a scalar valued function to get one value at a time, which is still very fast unless you have hundreds of key-value pairs with XML likeMyValue
Use like this
Use a scalar valued function to get one value at a time, which is still very fast unless you have hundreds of key-value pairs with XML like
Use like this
USE [SaveOn]
GO
/****** Object: StoredProcedure [dbo].[sp_GetClubUserRenewals] Script Date: 5/6/2015 12:37:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Adam Alt
-- Create date: 2015-4-30
-- Description: Gets count of users and total dollar value from user renewals per club.
-- =============================================
ALTER PROCEDURE [dbo].[sp_GetClubUserRenewals]
@Filters NVARCHAR(MAX)
AS
BEGIN
SET NOCOUNT ON;
--Declare variables
DECLARE @BeginDate DATETIME
--Parse out values from xml object
SELECT @BeginDate = dbo.GetFilterValue(@Filters,'BeginDate');
--Select statement
SELECT * FROM SOMETABLE WHERE(@BeginDate IS NULL OR DateField >= @BeginDate)
END
And here is the scalar valued function.
GO
/****** Object: UserDefinedFunction [dbo].[GetFilterValue] Script Date: 5/6/2015 12:35:35 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Adam Alt
-- Create date: 2015-4-30
-- Description: Gets the value of a single node in a Filters xml object defined like Value
-- =============================================
ALTER FUNCTION [dbo].[GetFilterValue](
@Filters XML,
@FilterName NVARCHAR(MAX))
RETURNS NVARCHAR(MAX)
AS
BEGIN
-- Declare the return variable here
DECLARE @RetVal NVARCHAR(MAX);
-- Add the T-SQL statements to compute the return value here
SELECT @RetVal = NULLIF(LTRIM(RTRIM(node.value('.','nvarchar(max)'))),'')
FROM @Filters.nodes('/Filters/*') AS Nodes(node)
WHERE node.value('local-name(.)','nvarchar(max)') = @FilterName;
-- Return the result of the function
RETURN @RetVal;
END;
Or get them all at once with a table valued function:
USE [SaveOn]
GO
/****** Object: UserDefinedFunction [dbo].[ParseFilters] Script Date: 5/4/2015 3:18:07 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Adam Alt
-- Create date: 2015-4-30
-- Description: Takes xml like MyValue and returns all name-value pairs in a table, removing empty entries.
-- =============================================
ALTER FUNCTION [dbo].[ParseFilters](
@XML XML)
RETURNS @RtnTable TABLE(ID INT IDENTITY(1,1),
Name NVARCHAR(MAX),
Value NVARCHAR(MAX))
AS
BEGIN
INSERT INTO @RtnTable(Name,
[Value])
SELECT node.value('local-name(.)','nvarchar(max)') AS Name,
NULLIF(LTRIM(RTRIM(node.value('.','nvarchar(max)'))),'') AS Value
FROM @XML.nodes('/Filters/*') AS Nodes(node);
RETURN;
END;
Tuesday, April 28, 2015
ReactJS.Net in Web Forms App with Server-side rendering
View the complete solution at https://github.com/adamvaul/React-Server-Side-Web-Forms.
I love ReactJS.Net. It is a framework for managing a React driven website in Visual Studio. Recently, I got interested in React and wanted to see if I could make it work in a web forms application (not an MVC application like all the demos show). I got a very simple demo to work based on the Chat tutorial on their website.
In this web forms app there is a little bit of code to render the html string to the page, and then there is a small snippet to add the block to register the react compenents client side. All of this magic happens with these few lines of code.
I followed the tutorials here at the ReactJS website http://reactjs.net/getting-started/tutorial.html. I also found this article to be helpful but I had to figure out the last part with env.GetInitJavaScript(); on my own. The article is here at http://xabikos.com/server%20side%20templating/web%20development/2015/03/18/using-reactjs.net-in-web-forms.html
And
In the hmtl:
asp:Literal ID="app" runat="server"
asp:Literal ID="litInitJS" runat="server"
And then in the code behind:
var env = AssemblyRegistration.Container.Resolve
var homeController = new HomeController();
var data = homeController.Get();
var commentBoxOptions = new
{
initialData = data,
url = "api/Home",
submitUrl="api/Home/addcomment",
pollInterval=2000
};
var commentBoxComponent = env.CreateComponent("CommentBox", commentBoxOptions);
app.Text = commentBoxComponent.RenderHtml();
string initJS = env.GetInitJavaScript();
litInitJS.Text = initJS;
I did not implement all the features like they did, but the application works.
View it at https://github.com/adamvaul/React-Server-Side-Web-Forms.
Friday, May 18, 2012
SQL: How to Parse Scientific Notation to a Decimal
Excel likes to write numbers into scientific notation. This raises a problem if you are trying to message data into a database from an excel file. (In order to read the Excel, we used Spreadsheet gear.Net assembly and then converted that to XML using
In C# code it is fairly easy to parse any kind of string to a decimal using
decimal.TryParse(myString, NumberFormat.Float, out myDecimal);
However, in SQL it becomes a little trickier, mostly because any attempt to parse a number that is not a decimal raising a Cannot convert varchar to decimal. Alternatively, when the scientific notation is large (e.g. 4E+16 which is 40,000,000,000,000,000) then you might also get an arithmetic overflow operation if you try to cast that to a Decimal(18,6).
The real problem with both of these errors are that they are batch-aborting errors. (See http://www.sommarskog.se/error-handling-I.html for a good explanation). The gist of if is that you must catch these errors in a try catch block, or turn off the SQL feature that raises those kinds of warnings before you parse something that may throw these kinds of errors. Then after you parse you can check to see if the value IS NOT NULL, and you know it is a real decimal. Enough talk, let's see some sql!
--List of nasty values from excel
--NA
--Ref
--4E+04
--6E-07
--2E-05
--2
--4E+16
--45
--[blank]
--[blank]
--3.445
--Div0
--Name
--4.879667
--NULL
--NULL
--NULL
DECLARE @TempPricesFromExcel TABLE
(
StringValue VARCHAR(50), --The value from Excel
IsNumber BIT, --This is needed because when called in an Update statement or function ISNUMERIC sometimes returns true when it is not a number!
PriceAsFloat FLOAT, --An intermediate value in the process, since float is more flexible than a decimal
FinalPrice DECIMAL(18,6) --Getting the value to this last one is the tricky part.
)
--Insert all the values
INSERT @TempPricesFromExcel (StringValue) VALUES ('NA')
INSERT @TempPricesFromExcel (StringValue) VALUES ('Ref')
INSERT @TempPricesFromExcel (StringValue) VALUES ('4E+04')
INSERT @TempPricesFromExcel (StringValue) VALUES ('6E-07')
INSERT @TempPricesFromExcel (StringValue) VALUES ('2E-05')
INSERT @TempPricesFromExcel (StringValue) VALUES ('2')
INSERT @TempPricesFromExcel (StringValue) VALUES ('4E+16')
INSERT @TempPricesFromExcel (StringValue) VALUES ('45')
INSERT @TempPricesFromExcel (StringValue) VALUES ('')
INSERT @TempPricesFromExcel (StringValue) VALUES ('')
INSERT @TempPricesFromExcel (StringValue) VALUES ('3.445')
INSERT @TempPricesFromExcel (StringValue) VALUES ('Div0')
INSERT @TempPricesFromExcel (StringValue) VALUES ('Name')
INSERT @TempPricesFromExcel (StringValue) VALUES ('4.879667')
INSERT @TempPricesFromExcel (StringValue) VALUES (NULL)
INSERT @TempPricesFromExcel (StringValue) VALUES (NULL)
INSERT @TempPricesFromExcel (StringValue) VALUES (NULL)
SELECT * FROM @TempPricesFromExcel --Make sure we got the data
UPDATE @TempPricesFromExcel SET IsNumber = ISNUMERIC(StringValue) --Save these results becuase ISNUMERIC is a little finnicky
SELECT * FROM @TempPricesFromExcel --Make sure it looks right
UPDATE @TempPricesFromExcel
SET PriceAsFloat = CASE -- This is a basic parse, eliminating NULL or blank values
WHEN StringValue IS NULL THEN NULL
WHEN LEN(StringValue) = 0 THEN NULL
WHEN ISNUMERIC(StringValue) = 1 THEN CONVERT(FLOAT, StringValue) --Convert to float
ELSE 0
END
WHERE IsNumber = 1
SELECT * FROM @TempPricesFromExcel
SET ANSI_WARNINGS OFF --Turn all these warnings off so any rows that don't parse will simply be left NULL
SET ARITHABORT OFF
SET ARITHIGNORE ON
UPDATE @TempPricesFromExcel SET FinalPrice = CONVERT(DECIMAL(18,6), PriceAsFloat)
SET ARITHIGNORE OFF
SET ARITHABORT ON
SET ANSI_WARNINGS ON
SELECT * FROM @TempPricesFromExcel --Now we have our list of all the values that could parse into the decimal of our chosen size.
As this procedure goes along, it first checks to see if the values are NUMERIC, and saves that in a separate column. Parsing to a float first is necessary so we can see what values are valid as a number, but could not ultimately be parsed into a decimal of our chosen length. The problem with DECIMAL datatype is that you have to specify a length. It is just like VARCHAR. In both cases you get errors when data is going to be truncated.
--List of nasty values from excel
--NA
--Ref
--4E+04
--6E-07
--2E-05
--2
--4E+16
--45
--[blank]
--[blank]
--3.445
--Div0
--Name
--4.879667
--NULL
--NULL
--NULL
DECLARE @TempPricesFromExcel TABLE
(
StringValue VARCHAR(50), --The value from Excel
IsNumber BIT, --This is needed because when called in an Update statement or function ISNUMERIC sometimes returns true when it is not a number!
PriceAsFloat FLOAT, --An intermediate value in the process, since float is more flexible than a decimal
FinalPrice DECIMAL(18,6) --Getting the value to this last one is the tricky part.
)
--Insert all the values
INSERT @TempPricesFromExcel (StringValue) VALUES ('NA')
INSERT @TempPricesFromExcel (StringValue) VALUES ('Ref')
INSERT @TempPricesFromExcel (StringValue) VALUES ('4E+04')
INSERT @TempPricesFromExcel (StringValue) VALUES ('6E-07')
INSERT @TempPricesFromExcel (StringValue) VALUES ('2E-05')
INSERT @TempPricesFromExcel (StringValue) VALUES ('2')
INSERT @TempPricesFromExcel (StringValue) VALUES ('4E+16')
INSERT @TempPricesFromExcel (StringValue) VALUES ('45')
INSERT @TempPricesFromExcel (StringValue) VALUES ('')
INSERT @TempPricesFromExcel (StringValue) VALUES ('')
INSERT @TempPricesFromExcel (StringValue) VALUES ('3.445')
INSERT @TempPricesFromExcel (StringValue) VALUES ('Div0')
INSERT @TempPricesFromExcel (StringValue) VALUES ('Name')
INSERT @TempPricesFromExcel (StringValue) VALUES ('4.879667')
INSERT @TempPricesFromExcel (StringValue) VALUES (NULL)
INSERT @TempPricesFromExcel (StringValue) VALUES (NULL)
INSERT @TempPricesFromExcel (StringValue) VALUES (NULL)
SELECT * FROM @TempPricesFromExcel --Make sure we got the data
UPDATE @TempPricesFromExcel SET IsNumber = ISNUMERIC(StringValue) --Save these results becuase ISNUMERIC is a little finnicky
SELECT * FROM @TempPricesFromExcel --Make sure it looks right
UPDATE @TempPricesFromExcel
SET PriceAsFloat = CASE -- This is a basic parse, eliminating NULL or blank values
WHEN StringValue IS NULL THEN NULL
WHEN LEN(StringValue) = 0 THEN NULL
WHEN ISNUMERIC(StringValue) = 1 THEN CONVERT(FLOAT, StringValue) --Convert to float
ELSE 0
END
WHERE IsNumber = 1
SELECT * FROM @TempPricesFromExcel
SET ANSI_WARNINGS OFF --Turn all these warnings off so any rows that don't parse will simply be left NULL
SET ARITHABORT OFF
SET ARITHIGNORE ON
UPDATE @TempPricesFromExcel SET FinalPrice = CONVERT(DECIMAL(18,6), PriceAsFloat)
SET ARITHIGNORE OFF
SET ARITHABORT ON
SET ANSI_WARNINGS ON
SELECT * FROM @TempPricesFromExcel --Now we have our list of all the values that could parse into the decimal of our chosen size.
As this procedure goes along, it first checks to see if the values are NUMERIC, and saves that in a separate column. Parsing to a float first is necessary so we can see what values are valid as a number, but could not ultimately be parsed into a decimal of our chosen length. The problem with DECIMAL datatype is that you have to specify a length. It is just like VARCHAR. In both cases you get errors when data is going to be truncated.
Subscribe to:
Posts (Atom)