SQL Stuff

Not 'stuff' as in 'Where's my stuff'... 'Stuff' as in the T-SQL function.

http://msdn.microsoft.com/en-us/library/ms188043.aspx

I've tried for years to find a good use for this function, and I've finally found one

I frequently need to convert a date to the first of the month. Normally I use several steps, first calculating the day of the month and then subtracting that number from the date using DATEADD. But now...

Convert a date to the first of the month

convert(datetime,stuff(dtdate,5,2,'01'))

It appears that this acts on a date formatted as

May 15 2011 11:00AM

This line 'stuffs' the string '01' into the day portion of the date, and then converts the result back to a datatime variable.

Clear out the time portion of a DATETIME

Based on that, we can also do this to clear out the time portion

print stuff(getdate(),13,7,'12:00 AM')

Get a date in the format of 'Jan 2012'

stuff(convert(varchar(12),@as_of),4,3,'') as monthYear2

Get a date in the format of 'MMYYYY'

set @output = stuff(convert(varchar(10),@input,101),3,4,'')

 

 

 


RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences