Thursday, 16 April 2015

sql server sub query with a comma separated resultset

I have above table structure and if i want to show my child table's value comma separated then below is my solution.

 SELECT n.nominationID
        , SUBSTRING((
                            SELECT ',' + af.awardFocusName
                            FROM NominationAwardFocus naf
                            JOIN AwardFocus af
                                ON naf.awardFocusID = af.awardFocusID
                            WHERE n.nominationID = naf.nominationID
                            FOR XML PATH('')

                        ), 2, 1000000)
    FROM Nomination n

And another solutions is
DECLARE @listStr VARCHAR(MAX)
(SELECT @listStr = COALESCE(@listStr+',' , '') + c.Code FROM RefferalCPTCodes  r inner join CPTCode c on r.CPTCodeID=c.id where ReferralID=80) 
SELECT @listStr

Monday, 30 March 2015

For generate regex for Numeric value range

AngularJS validation of two forms on same page

I have two forms on the same page that need validation. The thing is one of the forms is being databound so that I can check if it is valid using $invalid. But the other form seems like it is not getting bound and cannot call any of the methods on it ($dirty, $invalid, etc.)

Please see here below
replace
   <div class="error" data-ng-show="checkoutBillingForm.txtBillingAddress1.$dirty && checkoutBillingForm.txtBillingAddress1.$invalid">
with
   <div class="error" data-ng-show="checkoutBillingForm.txtBillingAddress1.$dirty || checkoutBillingForm.txtBillingAddress1.$invalid">
var app = angular.module('app', []);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
  <form name="checkoutShippingForm" role="form" novalidate="">
    <div class="row">
      <div class="col-md-4">
        <label for="txtAddress1">Address 1:</label>
      </div>
      <div class="col-md-8">
        <input type="text" name="txtAddress1" data-ng-model="vm.Shipping.Address.AddressLine1" required="" />
        <div class="error" data-ng-show="checkoutShippingForm.txtAddress1.$dirty && checkoutShippingForm.txtAddress1.$invalid">
          <small class="error" data-ng-show="checkoutShippingForm.txtAddress1.$error.required">An Address is required.</small>
        </div>
      </div>
    </div>
    Invalid? {{checkoutShippingForm.$invalid}}
    <!--Doesn't show -->
  </form>
  <form name="checkoutBillingForm" role="form" novalidate="">
    <div class="row">
      <div class="col-md-4">
        <label for="txtBillingAddress1">Billing Address 1:</label>
      </div>
      <div class="col-md-8">
        <input type="text" id="txtBillingAddress1" name="txtBillingAddress1" data-ng-model="vm.Billing.Address.AddressLine1" required="" />
        <div class="error" data-ng-show="checkoutBillingForm.txtBillingAddress1.$dirty || checkoutBillingForm.txtBillingAddress1.$invalid">
          <small class="error" data-ng-show="checkoutBillingForm.txtBillingAddress1.$error.required">An Address is required.</small>
        </div>
      </div>
    </div>
  </form>
</div>

Thursday, 22 January 2015

Check whether one list contain another list value in LINQ


var CheckExist = List1.Any(l1 => List2.Any(l2 => l1.Name == l2.Name));


Here CheckExist  object will return true if any record of List1 contain in List2

Sunday, 28 December 2014

Formatting date in Linq-to-Entities query causes exception

invoices.Select(i => new 
{
    FormattedDate = (     EntityFunctions.Right(String.Concat(" ", SqlFunctions.StringConvert((double?) SqlFunctions.DatePart("dd", i.DocumentDate))), 2)
                        + "/"
                        + EntityFunctions.Right(String.Concat(" ",SqlFunctions.StringConvert((double?) SqlFunctions.DatePart("mm", i.DocumentDate))), 2)
                        + "/"
                        + EntityFunctions.Right(SqlFunctions.StringConvert((double?) SqlFunctions.DatePart("yyyy", i.DocumentDate)), 4)
                       ).Replace(" ", "0")
}

Friday, 27 June 2014

Convert returned JSON Object Properties to (lower first) camelCase

I have JSON returned from an API like so:
Contacts: [{ GivenName: "Matt", FamilyName:"Berry" }]
To keep this consitent with my code style (camelCase - lower case first letter) I want to transform the array to produce the following:
 contacts: [{ givenName: "Matt", familyName:"Berry" }]
Whats the easiest/best way to do this? create a new Contact object and iterate over all the conatacts in the returned array?


I needed a generic method that accepted an array or object. This is what I'm using (I borrowed KyorCode's firstToLower() implementation):


function convertKeysToCamelCase(obj) {
    if (!obj || typeof obj !== "object") return null;

    if (obj instanceof Array) {
        return $.map(obj, function(value) {
            return convertKeysToCamelCase(value);
        });
    }

    var newObj = {};
    $.each(obj, function(key, value) {
        key = key.charAt(0).toLowerCase() + key.slice(1);
        newObj[key] = value;
    });

    return newObj;
};
Example calls:
var contact = { GivenName: "Matt", FamilyName:"Berry" };

console.log(convertKeysToCamelCase(contact));
// logs: Object { givenName="Matt", familyName="Berry"}

console.log(convertKeysToCamelCase([contact]));
// logs: [Object { givenName="Matt", familyName="Berry"}]

console.log(convertKeysToCamelCase("string"));
// logs: null

console.log(contact);
// logs: Object { GivenName="Matt", FamilyName="Berry"}
 

Thursday, 22 May 2014

Give custom validation message in HTML5

Custom Message in HTML5

Generally if you are just giving required in input it will give its own validation message in html5. But if you want to give your custom message then do this.

<input id="UserID" type="text" required="required" oninvalid="this.setCustomValidity('Witinnovation')">