Skip to main content

Function inn Sql Server

select Ascii('a')

1 Program
SET TEXTSIZE 0
SET NOCOUNT ON
-- Create the variables for the current character string position
-- and for the character string.
DECLARE @position int, @string char(15)
-- Initialize the variables.
SET @position = 1
SET @string = 'Du monde entier'
WHILE @position <= DATALENGTH(@string)
BEGIN
SELECT ASCII(SUBSTRING(@string, @position, 1)),
CHAR(ASCII(SUBSTRING(@string, @position, 1)))
SET @position = @position + 1
END
SET NOCOUNT OFF
GO


LEFT
Returns the left part of a character string with the specified number of characters.

Syntax
LEFT ( character_expression , integer_expression )



SELECT LEFT(title, 5)
FROM titles
ORDER BY title_id


LEN
Returns the number of characters, rather than the number of bytes, of the given string expression, excluding trailing blanks.

Syntax
LEN ( string_expression )

Arguments
string_expression

Is the string expression to be evaluated.

Return Types
int

Examples
This example selects the number of characters and the data in CompanyName for companies located in Finland.

USE Northwind
GO
SELECT LEN(CompanyName) AS 'Length', CompanyName
FROM Customers
WHERE Country = 'Finland'


LOWER
Returns a character expression after converting uppercase character data to lowercase.

Syntax
LOWER ( character_expression )

Arguments
character_expression

Is an expression of character or binary data. character_expression can be a constant, variable, or column. character_expression must be of a data type that is implicitly convertible to varchar. Otherwise, use CAST to explicitly convert character_expression.

Return Types
varchar

Examples
This example uses the LOWER function, the UPPER function, and nests the UPPER function inside the LOWER function in selecting book titles that have prices between $11 and $20.

USE pubs
GO
SELECT LOWER(SUBSTRING(title, 1, 20)) AS Lower,
UPPER(SUBSTRING(title, 1, 20)) AS Upper,
LOWER(UPPER(SUBSTRING(title, 1, 20))) As LowerUpper
FROM titles
WHERE price between 11.00 and 20.00

LTRIM
Returns a character expression after removing leading blanks.

Syntax
LTRIM ( character_expression )

Arguments


Examples
This example uses LTRIM to remove leading spaces from a character variable.

DECLARE @string_to_trim varchar(60)
SET @string_to_trim = ' Five spaces are at the beginning of this
string.'
SELECT 'Here is the string without the leading spaces: ' +
LTRIM(@string_to_trim)



REPLACE
Replaces all occurrences of the second given string expression in the first string expression with a third expression.

Syntax
REPLACE ( 'string_expression1' , 'string_expression2' , 'string_expression3' )



REPLICATE
Repeats a character expression for a specified number of times.



SELECT Replicate(username+' ',2) as UserName from FMP_tbluser

Result
UserName
------------
testtest
user1user1
user2user2

SELECT REPLACE('abcdefghicde','cde','xxx')
GO
Here is the result set:

------------
abxxxfghixxx
(1 row(s) affected)



REVERSE
Returns the reverse of a character expression.

SELECT Reverse(username) as UserName from FMP_tbluser
UserName
-------
tset
1resu
2resu


Select username from FMP_tbluser

UserName
------
test
user1
user2


SELECT Right(username,2) as UserName from FMP_tbluser
UserName
-------
st
r1
r2



SPACE
Returns a string of repeated spaces.


Select username+space(20)+username as username from FMP_tbluser
UserName
--------
test test
user1 user1
user2 user2

Select username+space(10)+username as username from FMP_tbluser
UserName
--------


test test
user1 user1
user2 user2




SUBSTRING
Returns part of a character, binary, text, or image expression. For more information about the valid Microsoft® SQL Server™ data types that can be used with this function, see Data Types.

Syntax
SUBSTRING ( expression , start , length )

SELECT substring(username,1,2) as UserName from FMP_tbluser

UserName
--------
te
us
us

Comments

Popular posts from this blog

URL Rewritting

http://www.simple-talk.com/dotnet/asp.net/a-complete-url-rewriting-solution-for-asp.net-2.0/ http://msdn.microsoft.com/en-us/library/ms972974.aspx URL Rewriting in ASP.NET Summary: Examines how to perform dynamic URL rewriting with Microsoft ASP.NET. URL rewriting is the process of intercepting an incoming Web request and automatically redirecting it to a different URL. Discusses the various techniques for implementing URL rewriting, and examines real-world scenarios of URL rewriting. (31 printed pages) Download the source code for this article. Contents Introduction Common Uses of URL Rewriting What Happens When a Request Reaches IIS Implementing URL Rewriting Building a URL Rewriting Engine Performing Simple URL Rewriting with the URL Rewriting Engine Creating Truly "Hackable" URLs Conclusion Related Books Introduction Take a moment to look at some of the URLs on your website. Do you find URLs like http://yoursite.com/info/dispEmployeeInfo.aspx?EmpID=459-099&type=summ...

SEND A PDF FILE AS AN ATTACHEMENT OF MAIL!

System.Net.Mail.MailMessage m1 = new System.Net.Mail.MailMessage(); m1.From = "manpreet@gmail.com" m1.Subject = "Test mail "; m1.Body = str.ToString(); m1.IsBodyHtml = true; m1.To.Add("jasdeep@gmail.com"); m1.CC.Add("jasdeep123@gmail.com"); m1.Attachments.Add(new Attachment(strserverpath + @"\pdf\" + PdfFileName)); smtp.Send(m1);

Sql Server Tips

IF WE WANT TO SELECT  TOP N ROWS  FROM A TABLE ,  WE USE THE FETCH NEXT  SELECT    * FROM  table1   ---//First m rows ignore first n rows ORDER BY id OFFSET n ROWS FETCH NEXT m ROWS ONLY if n=0, m=10 then first  10 rows if n=10, m=10 then first  10 rows start from 11th row Row_number()   SELECT  ROW_NUMBER() OVER(order by db.id) Insertion Simultaneouly in temp table with output clause DECLARE @table1 table (   Id int,   name nvarchar(50) ); DECLARE @table2 table (  Id int,  name nvarchar(50) ); INSERT INTO @table2 OUTPUT INSERTED.*   INTO @table1   select top 10 id, name  from FinalTable SELECT * FROM @table2; SELECT * FROM @table1;