Posts

Showing posts from September, 2014

Using comma separated value parameter strings in SQL IN clauses

SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE FUNCTION [dbo].[CSVToTable] (@InStr VARCHAR(MAX)) RETURNS @TempTab TABLE    (id int not null) AS BEGIN     ;-- Ensure input ends with comma SET @InStr = REPLACE(@InStr + ',', ',,', ',') DECLARE @SP INT DECLARE @VALUE VARCHAR(1000) WHILE PATINDEX('%,%', @INSTR ) <> 0 BEGIN    SELECT  @SP = PATINDEX('%,%',@INSTR)    SELECT  @VALUE = LEFT(@INSTR , @SP - 1)    SELECT  @INSTR = STUFF(@INSTR, 1, @SP, '')    INSERT INTO @TempTab(id) VALUES (@VALUE) END RETURN END GO This creates a user function that takes a comma separated value string and converts it into a table that SQL does understand - just pass it the sting, and it works it all out. It's pretty obvious how it works, the only complexity is the REPLACE part which ensures the string is terminated with a single comma by appending one, and removing all double commas from the string. Without

some comman shortcuts

F1 Display Help Ctrl+C Copy the selected item Ctrl+X Cut the selected item Ctrl+V Paste the selected item Ctrl+Z Undo an action Ctrl+Y Redo an action Delete Delete the selected item and move it to the Recycle Bin Shift+Delete Delete the selected item without moving it to the Recycle Bin first F2 Rename the selected item Ctrl+Right Arrow Move the cursor to the beginning of the next word Ctrl+Left Arrow Move the cursor to the beginning of the previous word Ctrl+Down Arrow Move the cursor to the beginning of the next paragraph Ctrl+Up Arrow Move the cursor to the beginning of the previous paragraph Ctrl+Shift with an arrow key Select a block of text Shift with any arrow key Select more than one item in a window or on the desktop, or select text within a document Ctrl with any arrow key+Spacebar Select multiple individual items in a window or on the desktop Ctrl+A Select all items in a document or window F3 Search for a file or folder Alt+Enter Display properties for th