Re: 14 byte date/time in file name

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Jeroen Mostert (jmostert@xxxxxxxxx) writes:
Yes, that occurred to me as well, but my thoughts were running in another
direction altogether.

DECLARE @x VARCHAR(19);
DECLARE @i INT;
SELECT @x = CONVERT(CHAR(19), GETDATE(), 120), @i = 5;
SELECT @x = STUFF(@x, @i, 1, ''), @i = @i + 2;
SELECT @x = STUFF(@x, @i, 1, ''), @i = @i + 2;
SELECT @x = STUFF(@x, @i, 1, ''), @i = @i + 2;
SELECT @x = STUFF(@x, @i, 1, ''), @i = @i + 2;
SELECT @x = STUFF(@x, @i, 1, ''), @i = @i + 2;
SELECT @x;

Note how I unrolled a loop here for efficiency.[*]

I was unable to find something even more roundabout without it becoming
too far-fetched; I blame T-SQL's limited expressiveness.

Reminds of when I as a senior student and helped evaluating the tests of the
younger students in the basic programming course. In my batch there was a
guy who had written the same code six times, rather than writing a loop.
(The problem related to a dice.) I wanted to fluke him, since he apparently
had missed an important point. But the teacher who reviewed my ratings,
said we had to approve him, since his code after all worked correctly.


--
Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
.



Relevant Pages

  • Re: Grouping by Day for a Date Range
    ... an item goes in an out of a workflow state. ... DECLARE @YESTERDAY DATETIME; ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ... Books Online for SQL Server 2005 at ...
    (comp.databases.ms-sqlserver)
  • Re: Cursor for loops
    ... There are customers in SQL Server. ... Check out DECLARE. ... Then you combine that with a while loop. ... UPDATE or DELETE) and that is most often much much faster than looping using a cursor. ...
    (microsoft.public.sqlserver.server)
  • Re: Cursor for loops
    ... There are customers in SQL Server. ... Check out DECLARE. ... Then you combine that with a while loop. ... UPDATE or DELETE) and that is most often much much faster than looping using a cursor. ...
    (microsoft.public.sqlserver.programming)
  • Re: Cursor for loops
    ... There are customers in SQL Server. ... Check out DECLARE. ... Then you combine that with a while loop. ... UPDATE or DELETE) and that is most often much much faster than looping using a cursor. ...
    (microsoft.public.sqlserver.clients)
  • Re: Creating rows based on date range from another table
    ... if there are alternative solutions. ... A loop is more complex to program, ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ... Books Online for SQL Server 2005 at ...
    (comp.databases.ms-sqlserver)