News for hackers. Stuff that matters.
Re:Hardly-used SQL Server functions that should be used more... by infogulch on Thursday April 06, 2017 @10:55AM (#14051061)
Cool I didn't know about PARSENAME. I've always written udfs for that. I suppose it wouldn't work in all cases since it can only split on '.', but with REPLACE it gets you a long ways.

Edit: Looks like PARSENAME is more exciting than that. It actually unescapes escaped identifiers:

    select n, parsename('[srv with
    newline].[db [ with ]] brackets].[schema with . dot]."quoted . dot"', n)
    from (values (1),(2),(3),(4),(5)) n(n)

    /* results:
    1	quoted . dot
    2	schema with . dot
    3	db [ with ] brackets
    4	srv with 
    newline
    5	NULL
    */
Another fun fact about SQL Server: Object identifiers support newlines, among other crazy characters. Don't believe me?

    create table #t ([newline
    here] int)

    select * from #t
Stay safe, and always use QUOTENAME [1]. :D

[1]: https://docs.microsoft.com/en-us/sql/t-sql/functions/quotena...

There are no comments on this comment yet.