Tuesday, March 27, 2018

parse float with Comma Decimal Values

javascript's parseFloat doesn't take a locale parameter. So you will have to replace comma(,), dollar sign($) with empty

Example :
 Full Cost  Value : $1,500.00

convert the commas to Normal value(decimal value).

var fullcost = parseFloat($("#fullcost").text().replace('$', '').replace(',', '').replace(',', '.')); //1500.00

fullcost =parseFloat(1500.00)

Result value : 1500.00



The parseFloat() function parses a string and returns a floating point number.

This function determines if the first character in the specified string is a number. If it is, it parses the string until it reaches the end of the number, and returns the number as a number, not as a string.

Note: Only the first number in the string is returned!

Note: Leading and trailing spaces are allowed.

Note: If the first character cannot be converted to a number, parseFloat() returns NaN.
   

Wednesday, March 21, 2018

Common Table Expressions

Common Table Expressions are also called CTEs. This feature was introduced with SQL Server 2005. The CTE is preferred to use as an alternative to a Subquery/View.

Sub-queries

A sub-query is a query within a query. It is also called an inner query or a nested query. A sub-query is usually added in a where clause of the SQL statement.

Example
Select Name,Age, employeeID   
From table1   
Where employeeID in   
( 
   Select employeeID from table2 where salary >=7500 /******Sub Query******/ 
)

use a CTE

With aliastablename (column1) 
 
AS 
 
(Query)

example
With table1CTE(EmployeeID) 
 
AS   
(Select employeeID from table1 where salary >=7500)

Advantages
CTE improves the code readability.
CTE provides recursive programming.
CTE makes code maintainability easier.
Though it provides similar functionality as a view, it will not store the definition in metadata.

Monday, March 12, 2018

Find all tables containing column with specified name


select * from INFORMATION_SCHEMA.COLUMNS
where COLUMN_NAME like '%TableColumnName%'
order by TABLE_NAME


Select * from  INFORMATION_SCHEMA.COLUMNS
where COLUMN_NAME LIKE '%TableColumnName%'

How can I list all foreign keys referencing a given table in SQL Server?

EXEC sp_fkeys 'TableName'

Wednesday, February 28, 2018

Add $ symbol to column values

To add $ symbol to column values and convert the column values to western number system
Example 1.
declare @value int = 4255
select '$ ' + replace(convert(varchar(100), convert(money, @value), 1), '.00', '')

-- output: $ 4,255

Example 2
declare @value Decimal(18,2)= 5488.00
select '$ ' + convert(varchar(100), convert(money, @value), 1)

-- output: $ 5488.00

Friday, February 9, 2018

What are the pros/cons of using a synonym vs. a view?

They are different things. A synonym is an alias for the object directly, a view is a construct over one or more tables.

Some reasons to use a view:

May need to filter, join or otherwise frig with the structure and semantics of the result set

May need to provide legacy support for an underlying structure that has changed but has dependencies that you do not want to re-work.

May provide security where some of the contents of the table should be visible to a class of users but not all. This could involve removing columns with sensitive information or filtering out a subset of the records.

May wish to encapsulate some business logic in a form that is accessible to users for reporting purposes.

You may wish to unify data from more than one source.

Reasons to use a synonym:

You may wish to alias an object in another database, where you can't (or don't want to) hard code the reference to the specific database.

You may wish to redirect to a source that changes over time, such as an archive table.

You want to alias something in a way that does not affect the query optimiser.

syntax: -


Create View vwtablename

as 

Select *  from tablename  where CreatedOn>'2014-01-26'

union

Select *  from tablename  where CreatedOn<='2014-01-26'

go
select * from vwtablename
go
Drop  View vwtablename

Synonym is just a second name of table used for multiple link of database.

View can be created with many tables, and with virtual columns and with conditions. But synonym can be on view.

Alias is temporary and used with one query. Synonym is permanent and not used as alias.


With a view, I can

hide columns
add predicates (WHERE clause) to restrict rows
rename columns
give a column name to a SQL expression
With a synonym, I can:

reference objects in other schemas and databases without qualifying the name

Thursday, February 1, 2018

Javacript: Set or Update a URL/QueryString Parameter, and update URL

Method1 :-

// Explicitly save/update a url parameter using HTML5's replaceState().
function updateQueryStringParam(param, value) {
  baseUrl = [location.protocol, '//', location.host, location.pathname].join('');
  urlQueryString = document.location.search;
  var newParam = key + '=' + value,
  params = '?' + newParam;

  // If the "search" string exists, then build params from it
  if (urlQueryString) {
    keyRegex = new RegExp('([\?&])' + key + '[^&]*');
    // If param exists already, update it
    if (urlQueryString.match(keyRegex) !== null) {
      params = urlQueryString.replace(keyRegex, "$1" + newParam);
    } else { // Otherwise, add it to end of query string
      params = urlQueryString + '&' + newParam;
    }
  }
  window.history.replaceState({}, "", baseUrl + params);
}


Method2:-

var Inputurl ="http://www.example.com/MP/Test.aspx?ID=2&Name=Kumar&Dept=N&Loc=Chennai"

// Function Implemented
 var OutputUrl=replaceKeyValue(Inputurl , "Name", "Karthik");

// Output Url
Result Url as below

http://www.example.com/MP/Test.aspx?ID=2&Name=Karthik&Dept=N&Loc=Chennai
 
// Javascript Function

function replaceKeyValue(url, keyName, newValue) {
            var urlParts = url.split("?");
            if (urlParts.length == 2) {
                var queryString = urlParts[1];
                queryString = queryString.split("&");
                var newQueryString = [];
                if (queryString.length > 0) {
                    $.each(queryString, function (i, e) {
                        var keyValue = e.split("=");
                        if (keyValue.length == 2 && $.trim(keyValue[0]) == keyName) {
                            keyValue[1] = newValue
                        }
                        newQueryString.push(keyValue.join("="));
                    })
                }
                return urlParts[0] + "?" + newQueryString.join("&");
            }
            return url;
        }

Change default Port on Next.js app

 If any other app or process is running on port 3000 , you will get this error in your terminal Port 3000 is already in use. error Command f...