26
Dec 07

ReplaceAll for JavaScript

Unlike java code, JavaScript doesn’t have an exclusive “replaceAll” function. But that doesn’t mean the functionality’s absent in javaScript.

To replace all the occurrences of a particular character or string in a given string we make use of the letter ‘g’ which implies to replace all the occurrences of the given letters.

Ex. var stringToPerformReplace = “3+5-1+2″;

var replacedString = stringToPerformReplace.replace(“+”,”-”,”g”);

The new String will be “3-5-1-2″.

In a similar manner instead of “g”, if you specify “i” it implies “ignorecase”.

Note: You can club both together i.e. “gi” is valid.


24
Dec 07

Escape single quotes and wild cards ‘%’ , ‘_’ in MS SQL

To escape single quotes ( ‘ ) in MS SQL all you need to do is add two single quotes instead of one.

Ex. If you want to insert the name D’souza into MS SQL the query goes like this:

INSERT into name_column ([name]) VALUES (‘D”Souza’)

Look carefully. We have replaced single quote in between the letter D and the word SOUZA with two SINGLE QUOTES. (It’s not a double quote)

Similarly, you may also face a problem while querying for values which contain the characters ‘%’ and ‘_’ in them. This is so because SQL considers these two characters as wild cards for multiple characters and single character string matching.

There are 2 ways to escape these wild cards.

1. You can use the default square braces “[]” like this- [%] or [_]

2. You can define your own escape character by using the keyword ESCAPE at the end of your where clause.

Ex. select * from name_column where name like ‘gyan\_sagar’ ESCAPE ‘\’

In the example above we escape “_” by defining “\” as an escape character using the ESCAPE keyword.

Note: You can only define a single character for escaping and not a string.

I hope this post has made your work a bit more easier than before. Your suggestions and comments are always welcome. :)


24
Dec 07

My journey into the TECH WORLd

Each day as I come across something new, I write it down for all to view.

Knowledge is power, power should be shared, but if you don’t do it…Nobody cares!!!

-Nischal Shetty