Friday, February 24, 2012

For loops in Tsql

Can somebody please tell me how can i write a tsql statement in sql server 2000.

Same time how can i get the last digit of a inteager variable through tsql .What i want is to write 'right(intVariable,4) which is in vb .I want that in sql server 2000

Thank you

The best way to write TSql is to use Query Analyzer.

To get the last digit of an integer variable try: Right( Convert(varchar(10), intVariable ), 1 ) That will turn the int into a string, then get the last digit.

Since your subject has For Loop, but you haven't asked about For Loops, I'll just throw it in. TSql has While() instead of For. Declare a variable for your counter, use the While, and be sure to increment the counter.

Declare @.counter int

Set @.counter = 1

While @.counter < 10

Begin

Do something significant

Set @.counter = @.counter + 1

End

|||

Hi rohanfernando, I moved your question over to the Transact-SQL forum. The folks over here should be able to help you with your question.

Cheers,

JJustice [MSFT]

|||The easiest way to get the last digit of an integer is to get the 'mod 10' of it. Like this:

select 234 % 10 --gives 4
select 12098523 % 10 --gives 3

Rob

No comments:

Post a Comment