String.Format like functionality in T-SQL?

If you are using SQL Server 2012 and above, you can use FORMATMESSAGE. eg. DECLARE @s NVARCHAR(50) = ‘World’; DECLARE @d INT = 123; SELECT FORMATMESSAGE(‘Hello %s, %d’, @s, @d) — RETURNS ‘Hello World, 123’ More examples from MSDN: FORMATMESSAGE SELECT FORMATMESSAGE(‘Signed int %i, %d %i, %d, %+i, %+d, %+i, %+d’, 5, -5, 50, -50, … Read more

T-SQL Between Dates Confusion

A date is a point in time, not a time span. ’12/31/2010′ is a point, too. Namely, it’s the midnight of the 31st of December. Everything that happened after this point is ignored. That’s exactly the behaviour you want (even if you haven’t realised that yet). Do not think that when you choose to omit … Read more

How to use an Alias in a Calculation for Another Field

That method doesn’t work in SQL Server. You can accomplish the same thing in a couple different ways: 1.) Use the code for each aliased column instead of the alias: (SELECT COUNT(*) FROM UserEvent UE WHERE UE.EventTypeID = 1 AND UE.PracticeID = au.PracticeID AND (UE.EventDate BETWEEN @Date1 and @Date2) – COUNT(CASE WHEN udi.DevicePlatform = ‘iOS’ … Read more