Thursday, October 11, 2012

CAST a varchar to an int in MSSQL

I made the following stored procedure in the name of making a stored procedure which I could get to fail based upon what I passed in.

USE [IT]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sp_tblUsers_GetByUserID]
   @UserIDAsString varchar(50)
AS
BEGIN
SELECT * FROM tblUsers WHERE UserID = CAST(@UserIDAsString AS INT)
END
GO

 
 

I need this for testing. CAST and CONVERT are detailed here: http://blog.sqlauthority.com/2007/07/07/sql-server-convert-text-to-numbers-integer-cast-and-convert/.

No comments:

Post a Comment