Wednesday, 12 August 2015
Built String Function in Sql Server
05:43
No comments
Built
String Function in Sql Server
1.
Ascii: Its
return the ascii code for the given character expression
Syntax:
ASCII('character_expresssion')
Example print ascii('A')
Output : 65
***************** Using ASCII in Sql Server **********************************
Example:1
declare @st int
set @st=65
while(@st<=90)
begin
print char(@st)
set @st=@st+1
end
Output : A B C D E F G H I J K L M N O P Q R S T U V W X
Y Z
65 to 90 = A to Z
97 to 22 = a to z
48 to 58 = 1 to 10
2. Char:
Its convert on
int ascii code to character .The integer expression should be between 0-255
Syntax:
char(integer _expression)
Example print char(66)
Output : B
3. LTRIM: remove blanks on the left
hand side of the given character expression
Syntax:
LTRIM(‘character_expresssion')
Example print LTRIM(' KUSH')
Output : KUSH
4. RTRIM: remove blanks on the right
hand side of the given character
expression
Syntax:
RTRIM(‘character_expresssion')
Example print RTRIM('TIWARI ')
Output : TIWARI
4. Lower : convert all the characters in the
character_expression to lower case letter
Syntax:
LOWER(‘character_expresssion')
Example print LOWER('MANOJ')
Output : manoj
5. Upper : convert all the characters in the
character_expression to upper case letter
Syntax:
UPPER (‘character_expresssion')
Example PRINT UPPER('yadav')
Output : YADAV
6. Reverse : convert all the characters in the
character_expression to upper case letter
Syntax:
REVERSE (‘any_string _expresssion')
Example PRINT REVERSE('yadav')
Output : vaday
7. LEN : return length of any string expression
Syntax:
Len (‘string _expresssion')
Example print Len(' jyotirana')
Output : 10 ( 1 white space + 9 char)
8. Left: Return the specified
number of characters from the left hand side of the given characters expression
Syntax: Left ('Character_Expression', Inter_Expression)
Example: SELECT Left('ABCDEF',3)
Output : ABC
9. Right: Return the specified number of characters from
the right hand side of the given characters expression
Syntax: Right ('Character_Expression',Inter_Expression)
Example: SELECT Right('ABCDEF',3)
Output : DEF
10. CHARINDEX Return the starting position of the specified expression
in characters string
Syntax: CHARINDEX('Expression_To_Find','Expression_To_Search')
Example: SELECT CHARINDEX('@','kushktiwari@gmail.com')
Output : 12
10. Substring Return substring (part of string) from
the given expressions
Syntax: Substring('expression', start, length)
Example: SELECT Substring('kush@gmail.com',6,9)
Output : gmail.com
*************************
query *************************************
select SUBSTRING('kushktiwari@gmail.com',charindex('@','kushktiwari@gmail.com'),10)
Result @gmail.com
select SUBSTRING('kushktiwari@gmail.com',charindex('@','kushktiwari@gmail.com')+1,10)
Result gmail.com
select SUBSTRING('kushktiwari@gmail.com',CHARINDEX('@','kushktiwari@gmail.com')+1,len('kushktiwari@gmail.com')-CHARINDEX('@','kushktiwari@gmail.com'))
Result gmail.com
******** by this
Sql function we find domain name of any emailid *********
create function
GetDomainName(@emailid varchar(50))
returns varchar(50)
as
begin
declare @res varchar(50)=null
set @res=SUBSTRING(@emailid,CHARINDEX('@',@emailid)+1,len(@emailid)-CHARINDEX('@',@emailid))
return @res
end
--execute function
select dbo.GetDomainName('kushtiwari@outlook.com')
Result
outlook.com
--query for how to create table …………………………….
create table Employee
(
SrNo int identity(1,1) primary key,
FirstName
varchar(50),
LastName
varchar(50),
EmailId
varchar(50),
Address varchar(50)
)
select
FirstName+ ' ' +LastName as FullName
,
EmailId,
Address,
SUBSTRING(EmailId,CHARINDEX('@',EmailId)+1,len(EmailId)-CHARINDEX('@',EmailId)) as DomainName
from employee
Note: By this query we can
find that how many account exist on specific domain, you can see the result in
this image which are given below
select SUBSTRING(EmailId,CHARINDEX('@',EmailId)+1,len(EmailId)-CHARINDEX('@',EmailId)) as DomainName,
count(EmailId) as TotalEmailId from employee
group by SUBSTRING(EmailId,CHARINDEX('@',EmailId)+1,len(EmailId)-CHARINDEX('@',EmailId))
Result
Tuesday, 30 June 2015
How to implement Remote Validation in ASP.NET MVC
23:58
1 comment
Remote validation is concepts to create
server calls to validate information while not posting the whole form to the
server once server facet validation is desirable to user facet. It is all done got wind of model and
controller that is pretty neat.
---Sql Server Query
create database test
use test
create table StudentInfo
(
SrNo int,
Name varchar(50),
UserName varchar(50) primary key,
Password varchar(50),
EmailId varchar(50) unique,
Course varchar(50)
)
Step -1
First we will
take MvcApplication and take
class which name is
Student in Model Folder to apply
Remote Validation in our Application
*******************************************************************
using System;
using
System.Collections.Generic;
using
System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace
MvcRemoteValidation.Models
{
public class Student
{
public int SrNo { get; set; }
public string
Name { get; set; }
[Remote("CheckUserName", "Home")]
public string UserName { get; set; }
public string EmailId { get; set; }
public string
Password { get; set; }
public string Course { get; set; }
}
}
************************************************************
then we will work
on Home Contrller
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
// using this name
space..............
using
MvcRemoteValidation.Models;
namespace
MvcRemoteValidation.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
testEntities tt = new testEntities();
[HttpPost]
public ActionResult Index(StudentInfo s1)
{
tt.StudentInfoes.Add(s1);
tt.SaveChanges();
ViewBag.msg = "Data Saved
Successfully";
return View();
}
//public bool
CheckUserName(string UserName)
//{
// return !tt.StudentInfoes.Any(obj =>
obj.UserName == UserName);
//}
public JsonResult CheckUserName(string UserName)
{
var v =
tt.StudentInfoes.Where(obj => obj.UserName == UserName).Select(obj =>
obj).FirstOrDefault();
if (v != null)
{
return Json("UserName
already exit",
JsonRequestBehavior.AllowGet);
}
else
{
return Json(true, JsonRequestBehavior.AllowGet);
}
}
}
}
Then we will right click on
Index and add view with strongly type,such as given the below picture
Then you can see auto generated code of Index.cshml
@model MvcRemoteValidation.Models.Student
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<link href="~/Content/Site.css" rel="stylesheet" />
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Student</legend>
<div class="editor-label">
@Html.LabelFor(model
=> model.SrNo)
</div>
<div class="editor-field">
@Html.EditorFor(model
=> model.SrNo)
@Html.ValidationMessageFor(model
=> model.SrNo)
</div>
<div class="editor-label">
@Html.LabelFor(model
=> model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model
=> model.Name)
@Html.ValidationMessageFor(model
=> model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model
=> model.UserName)
</div>
<div class="editor-field">
@Html.EditorFor(model
=> model.UserName)
@Html.ValidationMessageFor(model
=> model.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(model
=> model.EmailId)
</div>
<div class="editor-field">
@Html.EditorFor(model
=> model.EmailId)
@Html.ValidationMessageFor(model
=> model.EmailId)
</div>
<div class="editor-label">
@Html.LabelFor(model
=> model.Password)
</div>
<div class="editor-field">
@*
@Html.EditorFor(model => model.Password)*@
@Html.PasswordFor(model=>model.Password)
@Html.ValidationMessageFor(model
=> model.Password)
</div>
<div class="editor-label">
@Html.LabelFor(model
=> model.Course)
</div>
<div class="editor-field">
@Html.EditorFor(model
=> model.Course)
@Html.ValidationMessageFor(model
=> model.Course)
</div>
<p>
<input type="submit" value="Create" />
@ViewBag.msg
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to
List",
"Index")
</div>
</body>
</html>
Result
Topic -2 here we will discuss how to create check the availability with username and emailid with Remote attribute concept in Mvc
We will do shown some modifications in class ‘Student’ as given below <highlighted
in yellow>:
public class Student
{
public int SrNo { get; set; }
public string
Name { get; set; }
[Remote("CheckUserNameandEmailId", "Home", AdditionalFields
= "EmailId")]
public string UserName { get; set; }
public string EmailId { get; set; }
public string
Password { get; set; }
public string Course { get; set; }
}
We will do other modifications in controller Home as
given below <highlighted in yellow>:
public JsonResult
CheckUserNameandEmailId(string EmailId,string UserName)
{
var v =
tt.StudentInfoes.Where(obj => obj.UserName == UserName &&
obj.EmailId == EmailId).Select(obj => obj).FirstOrDefault();
if (v != null)
{
return Json("UserName
and EmailId already match", JsonRequestBehavior.AllowGet);
}
else
{
return Json(true, JsonRequestBehavior.AllowGet);
}
}
Result
Note:
We will also use some another overload of Remote attribute
in class ‘Student as given below
<highlighted in yellow>:
Code for Student.cs......................
[Remote("CheckUserName", "Home", ErrorMessage="User Name
alreay is use")]
public
string
UserName { get; set;
}
Code for in
HomeController.cs......................
public JsonResult CheckUserName(string UserName)
{
return
Json(!tt.StudentInfoes.Any(obj => obj.UserName == UserName),JsonRequestBehavior.AllowGet);
}
Subscribe to:
Posts (Atom)




.jpg)











