SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to set a column to four zeros
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

vamsidhar.tangutoori
Starting Member

19 Posts

Posted - 11/20/2012 :  14:22:09  Show Profile  Reply with Quote
Hi,
I am trying to insert into a table that looks like

ID (identity) Range_From(int) Range_to(int)

I am writing a stored procedure which has to insert into the range columns values from 0000 to 9999
my question is
1)how do i insert 0000 into the columns?
2)If i insert only a part of the range then it has to automatically insert another row with the remaining part of the range for ex:

if i am trying to insert only 0000 to 1000, then it has to insert another row with 1001 to 9999
or
if i insert 9000 to 9999, then it has to insert another row with 0000 to 8999.
Please help me understand how to do this!!!!!!!!!!!!!!!!

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 11/20/2012 :  14:29:53  Show Profile  Reply with Quote
Would this work for you?
DECLARE @startReqd INT, @endReqd INT,
	@startActual INT, @endActual INT;
	
SET @startReqd = 0; SET  @endReqd = 9999;

INSERT INTO tbl (Range_From,Range_to) VALUES (@startActual,@endActual);
IF @startActual > @startReqd
BEGIN
	INSERT INTO tbl (Range_From,Range_to) VALUES (@startReqd,@startActual-1);
END
IF @endActual < @endReqd
BEGIN
	INSERT INTO tbl (Range_From,Range_to) VALUES (@endActual+1,@endReqd);
END
Go to Top of Page

vamsidhar.tangutoori
Starting Member

19 Posts

Posted - 11/20/2012 :  14:41:57  Show Profile  Reply with Quote
Hi Sunitabeck,
It works great thanks for the help.
one more question when i am retreving the results i need it in 0000 or 0001 or 0012 format how do i achieve that?

quote:
Originally posted by sunitabeck

Would this work for you?
DECLARE @startReqd INT, @endReqd INT,
	@startActual INT, @endActual INT;
	
SET @startReqd = 0; SET  @endReqd = 9999;

INSERT INTO tbl (Range_From,Range_to) VALUES (@startActual,@endActual);
IF @startActual > @startReqd
BEGIN
	INSERT INTO tbl (Range_From,Range_to) VALUES (@startReqd,@startActual-1);
END
IF @endActual < @endReqd
BEGIN
	INSERT INTO tbl (Range_From,Range_to) VALUES (@endActual+1,@endReqd);
END


Go to Top of Page

jimf
Flowing Fount of Yak Knowledge

USA
2869 Posts

Posted - 11/20/2012 :  14:51:45  Show Profile  Reply with Quote
You'll have to convert then numebrs to varchar and then add the zeroes like

...VALUES( RIGHT('0000'+convert(varchar(4),@startReqd+1,4)
)

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

vamsidhar.tangutoori
Starting Member

19 Posts

Posted - 11/20/2012 :  14:57:44  Show Profile  Reply with Quote
Thanks Jim,
it really helped me!!!!!

quote:
Originally posted by jimf

You'll have to convert then numebrs to varchar and then add the zeroes like

...VALUES( RIGHT('0000'+convert(varchar(4),@startReqd+1,4)
)

Jim

Everyday I learn something that somebody else already knew

Go to Top of Page

jimf
Flowing Fount of Yak Knowledge

USA
2869 Posts

Posted - 11/20/2012 :  15:23:05  Show Profile  Reply with Quote
You're welcome!

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000