Posts

Showing posts with the label TSQL

TSQL - String Concatenation

On current project, I ran into a scenario where I needed to concatenate a series of rows into a single string value.  Nothing major, just concatenate them with a delimiter and return the result.  As all of us Microsoft SQL developer's know and deal with is the lack of a SQL supported function that is able to assist us in this "minor" feature. As this is my knowledge dump for techniques and other methods used for the various platforms, here we go: Construct: The first step is to identify what information you want, the basic's of query design.   SELECT Email FROM Contacts Group By Email Group by is conditional, depending on your situation and how you organize your referential integrity. Stage Next, lets build our environment, not the SProc just the supporting actors in the process. declare @email varchar(max) = '' SELECT @email += Email + ', ' FROM Contacts Group By Email This simply concatenates each of ...

SQL - IsNullOrWhiteSpace Function

Just another SQL Code-snippet to check for NULL or whitespace value: -- ============================================= -- Author: John Wood -- Create date: 20151129 -- Description: Returns a boolean value -- ============================================= ALTER FUNCTION [Int].IsNullOrWhiteSpace ( -- Add the parameters for the function here @val sql_variant ) RETURNS bit AS BEGIN declare @ret bit if(SQL_VARIANT_PROPERTY(@val, 'BaseType') in ('varchar', 'nvarchar', 'char', 'nchar')) begin declare @char varchar = cast(@val as varchar) select @ret = iif(ltrim(rtrim(@char)) = '', 1, 0) end else begin -- Return the result of the function select @ret = iif( @val IS NULL, 1, 0 ) end return @ret END GO

Time Scalar Function

Everyone knows that you can use the Convert DML  to convert a DateTime data-type to a string evaluation for either split storage or display to an consuming interface. As most of us use the Date portion of a DateTime value for 90% of everything we are rendering the data to.  But what about that 10% where we either need to split render the Date & Time seperately.  After much review, we only have two quick and easy values to use and neither are really interchangeable. Option #1, with 2 forms: select convert(varchar(10), getdate(), 108) -- Produces: 13:53:15 select convert(varchar(15), getdate(), 114) -- Produces: 13:53:15:707 Now when using these forms of conversion, about 75% you have met the need for your requirement.  But lets say you need to use the Time string created for say a Filename ( sometext_[[TIME]].txt ), what do you do? Well the next code segment is what will be produced in order meet the FileName convention of special char...

Search Instance for a Stored Procedure usage

References: Peter Chamley MSSql Tips - Greg Robidoux: Listing SQL Server Object Dependencies Recently, I had the need to skim acrossed all the databases on a single instance.  In doing so I figured that the infamous 'sp_msforeachdb' undocumented stored procedure would be useful but was unsure on the methodology or pattern to implement.

Acquire SQL Service Account

Reference:  Jason Yousef Blog 1: DECLARE @SrvAccount varchar(100) 2: set @SrvAccount ='' 3: 4: EXECUTE master.dbo.xp_instance_regread 5: N'HKEY_LOCAL_MACHINE', 6: N'SYSTEM\CurrentControlSet\Services\SQLSERVERAGENT', 7: N'ObjectName', 8: @SrvAccount OUTPUT, 9: N'no_output' 10: 11: SELECT 12: @@SERVERNAME as [MachineName] 13: , @SrvAccount as SQLAgent_ServiceAccount