Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

DATALENGTH Expand / Collapse
Author
Message
Posted Monday, August 23, 2010 10:49 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 11:58 AM
Points: 1,676, Visits: 1,758
kevin.l.williams (8/23/2010)
Interesting that this does not work the same way.

select datalength(convert(varchar, getdate(), 101))


This is by design and has also been covered many times in recent QotDs. Forgetting to specify the size when declaring a local variable results in size defaulting to 1 while forgetting to specify the size when using convert - 30. Because the 101 format means mm/dd/yyyy, totalling 10 characters, the result of the query is 10 because the default 30 is greater than that.

Oleg
Post #973622
Posted Monday, August 23, 2010 11:00 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Thursday, September 25, 2014 2:58 PM
Points: 3,173, Visits: 1,267
Oleg Netchaev (8/23/2010)


This is by design and has also been covered many times in recent QotDs. Forgetting to specify the size when declaring a local variable results in size defaulting to 1 while forgetting to specify the size when using convert - 30. Because the 101 format means mm/dd/yyyy, totalling 10 characters, the result of the query is 10 because the default 30 is greater than that.

Oleg


Thanks.

I never like using implicit coding like this. It seems lazy and risky.



Post #973627
Posted Monday, August 23, 2010 2:13 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, September 5, 2014 2:00 PM
Points: 2,160, Visits: 2,191
Nice question, thanks!

I'm surprised after all the recent questions that were related that we still have ~25% answering this one incorrectly.
Post #973725
Posted Monday, August 23, 2010 3:24 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 8:18 PM
Points: 17,824, Visits: 15,756
Thanks for the question



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Post #973755
Posted Tuesday, August 24, 2010 3:42 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 1:26 AM
Points: 1,812, Visits: 498
Nice question on the basics and all the ensuing explanations - thanks
Post #973956
Posted Tuesday, August 24, 2010 9:00 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Tuesday, July 23, 2013 6:34 AM
Points: 654, Visits: 265
Nice question.
Post #974202
Posted Wednesday, March 7, 2012 2:01 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, July 30, 2014 10:52 PM
Points: 483, Visits: 244
Great question and pick on the data types. It reminds that by default '1' is assigned to non-defined variable declaration for the char & varchar data types.

Thanks.
Post #1263271
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse