Data conversion

  • Good point Hugo.

    Thanks.

    Do you have clue on the additional comma which just before the closing bracket (i.e. Amount INT,)?

  • Great question...a lot of points to ponder!

    I had no problem with the implicit conversions.

    I'm fascinated to learn you can use data types as column names.

    (This is good to know so you don't do it unintentionally, not because it's something you would want to do intentionally.)

    I'm still very puzzled why the extra comma didn't cause the create table statement to fail.

  • paul.jones (3/18/2011)


    I was so convinced the extra comma in the CREATE TABLE would cause an error, I never even noticed the date of the 2nd of Chequary (Chequary is the month after December where you still write cheques with the wrong year on).

    So, why is it OK to have the extra comma? In BOL http://msdn.microsoft.com/en-us/library/ms174979.aspx doesn't the [,...n] bit mean you can have a comma followed by another column definition, but not just a comma on its own? Why does having 2 commas at the end fail but one is OK?

    I love the "chequary" comment!

    I don't know why the extra comma doesn't cause an error, but I do like it as a (probably uninteded) feature. I can write CREATE TABLE statements like this:

    CREATE TABLE TabName(

    Column1 datatype NOT NULL,

    Column2 datatype NOT NULL,

    Column3 datatype NOT NULL,

    CONSTRAINT xxx PRIMARY KEY (Column1),

    CONSTRAINT yyy FOREIGN KEY (Column2) REFERENCES SomeTable

    );

    I can easily comment or uncomment lines, and even if I comment the last line, I can still run the code with no error, without having to remember to remove the comma from the last uncommented line.


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • Surii (3/18/2011)


    Chris Houghton (3/18/2011)


    I'm half asleep this morning. Missed every one of the potential gotchas (implict conversion, US date format and extra comma), thought "this code looks ok" and selected the right answer. If i'd been more awake I'd have got the answer wrong on several counts 😀

    Same case with me..:w00t:

    Definitely an advantage to be a groggy, hung-over American in this one.

  • jcunningham (3/18/2011)


    I see I am not the only one who noticed the extra comma. Did the author plan for this to throw people off??

    Yes I did

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • Honestly, I guessed and got it right, not closed-my-eyes-and-threw-a-dart guessed, but the code just looked like it could work. If I had read carefully, I think I would have said it could not work because of the extra comma. I almost balked at the select inside the insert as I have not used this myself but it looked plausible so I went for it.

    So I learned AND got a point 🙂 Thanks for the question.

    Peter Trast
    Microsoft Certified ...(insert many literal strings here)
    Microsoft Design Architect with Alexander Open Systems

  • Bull, theres a comma at the end of the create. That will fail.

  • bitbucket-25253 (3/18/2011)[hr

    Yes I did

    It worked!

    Did you find any explanation of this behaviour?

  • Ryan Fitzgerald (3/18/2011)


    Bull, theres a comma at the end of the create. That will fail.

    In my best master Yoda voice, "Sure of this, are you?"

  • Hugo Kornelis (3/18/2011)


    When will people ever learn about date formats?

    The correct answer ("depends on the locale settings") was not listed, so I had to throw a coin to choose between theory a (question submitted by a stupid American or Japanese who thinks the US/Jap system is the only system) or theory b (question submitted by a stupic non-American/non-Japanese who thinks THEIR system is the only system).

    Note to anyone who ever intends to submit questions with hardcoded dates in them: please use the yyyymmdd format only! Or, if you need time as well, yyyy-mm-ddThh:mm:ss.

    If you really want to tick people off by using a different date format, then FIRST run your code with a SET LANGUAGE statement in front of it, trying all the major languages - then consider if you still want to keep your code that way.

    THANKS FOR CALLING ME STUPID. YOUR ARROGANCE OR SHOULD I SAY EGOTISM IS APPALLING.

    As aside, you really should spell check your work before posting or is "stupic" the proper spelling in your native language, if so then why did you also use "a stupid American" or is either correct?

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • tommyh (3/18/2011)


    I was hoping that SQL would fail on the last comma in the create table part. But SQL was just as happy to create a table like that.

    /T

    I second this comment...it tripped me up too.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • THANKS FOR CALLING ME STUPID. YOUR ARROGANCE OR SHOULD I SAY EGOTISM IS APPALING.

    As aside, you really should spell check your work before posting or is "stupic" the proper spelling in your native language, if so then why did you also use "a stupid American" or is either correct?

    *appalling* 😉

    I, too, took some slight offense at Hugo's use of stupid, but I also understand the consequences of being surrounded by a majority that assumes everyone else lives as they do. The arrogance can be breathtaking.

    One thing I've often appreciated about this forum versus others I have followed is the level of maturity and accommodation.

    Back to the QotD: Anyone come up with a good reason why the trailing comma is allowed? Or why data types aren't reserved words?

    Rich

  • I was going to get it right, but then saw the '2/13' date and dithered, knowing that the insert would fail on my British system, and then guessed that the questioner was also on a system with the same date format and that it was a cunning 'gotcha'.

    Oh well, live and learn.

  • bitbucket-25253 (3/18/2011)


    THANKS FOR CALLING ME STUPID. YOUR ARROGANCE OR SHOULD I SAY EGOTISM IS APPALLING.

    Hmmm. Are you now implying that your use of a non-universal date format was not cause by ignorance, but a deliberate attempt to trick non-Americans in giving the wrong answer? I thought better of you. But in that case, I have to apologize for the word stupid - describing how I feel about deliberately using US-only dates and omittiing the correct answer needs a completely different set of words.

    If it was ignorance, not malice, then sorry if my words offended you. I didn't mean to offend. I did mean to place a firm wake-up call to all the people (in my experience almost always Americans) who all too often forget that there are more countries in the world. That kind of ignorance has, in my opinion, no place in the 21st century world.

    When I started writing this reply, no less than 10 people had already commented in one way or another on the date format issue. You only responded to me, and in your response you only addressed a word that offended you, and one of my typos. I don't mind if you ignore me, but are you at least planning to address the concerns of those other 9 people?


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • Thanks for the question, it is interesting to know that you can have a superfluous comma in a CREATE TABLE statement. (Not sure that is of much value other than what HUGO mentioned for commenting code out.)

Viewing 15 posts - 31 through 45 (of 101 total)

You must be logged in to reply to this topic. Login to reply