Solved

T-SQL : Variables

Posted on 2011-03-04
8
389 Views
Last Modified: 2012-05-11
Hi !

There's a part of the code from a report a run everyday that I can't understand.

	DECLARE @CRLD CHAR(2) = CHAR(13) + CHAR(10)
	DECLARE @LineBreak VARCHAR(10) = @CRLD + '<br />'

Open in new window


Can someone explain me the prupose of this.

Thank You !

-M
0
Comment
Question by:Rubicon2009
8 Comments
 
LVL 19

Expert Comment

by:amenkes
Comment Utility
It appears you are storing a variable @CRLD as 2 characters, a carriage return and a line feed.
The 2nd variable @LineBreak adds the first variable plus an HTML break for output to the web.
0
 
LVL 7

Accepted Solution

by:
tlovie earned 125 total points
Comment Utility
What it does is declare a @CRLD as a CHAR(2), and assigns the ascii values chr(13) and chr(10) to it (CR + LF)
then it does the same for @LineBreak, it defines it as a varchar(10) and assigns the value of @CRLD concatenated with '<br />'

0
 

Author Comment

by:Rubicon2009
Comment Utility
Butt how is it possible to store 23 characters in a 2 characters space ?
0
 
LVL 15

Assisted Solution

by:derekkromm
derekkromm earned 125 total points
Comment Utility
char(13) and char(10) are 1 character each

char(13) represents a carriage return
char(10) represents a line feed

for example, do "select char(70)" in query analyzer and you'll get "F", a single character. 70 is the ascii representation of "F"
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 19

Assisted Solution

by:amenkes
amenkes earned 125 total points
Comment Utility
It is not 23 characters. CHAR(2) is defining the storage type of 2 characters.

CHAR(13) is the ASCII code for a Carriage return, it is not 13 characters.
CHAR(10) is the ASCII code for Line Feed, not 10 characters.
0
 
LVL 18

Assisted Solution

by:deighton
deighton earned 125 total points
Comment Utility
CHAR(2) means 'two character variable', but then very confusingly CHAR(13) + CHAR(10) means concatenate ASCII character 13 to ASCII character 10

CHAR has different meaning on each side
0
 

Author Comment

by:Rubicon2009
Comment Utility
Thank you ! I'm very surprised, I was not expecting this kind of logic at all.
0
 

Author Closing Comment

by:Rubicon2009
Comment Utility
Thank a lot !
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now