table with DateTime?

  • Hi Friends,

    How to create a table with datetime?

    like tablename_getdate()

    Is that possible friends?

    Thanks,
    Charmer

  • Yes. Try dynamic sql.

    _____________________________________
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at Amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.
  • PaulB-TheOneAndOnly (3/30/2012)


    Yes. Try dynamic sql.

    Gimme an example Please...

    Thanks,
    Charmer

  • how about a view, so you have the date? is that what you want?

    Create view TableToday

    AS

    select

    DATEADD(dd, DATEDIFF(dd,0,getdate()), 0) As TheDate,

    GETDATE() as TheDateTime,

    edit: misread the question...thought you wanted a table with the datetime, not a tablename:

    DECLARE @sql varchar(8000)

    SET @sql = 'CREATE TABLE [TABLE_'

    + DATEADD(dd, DATEDIFF(dd,0,getdate()), 0)

    +' ] (ID int,OtherColumns varchar(30) )'

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Charmer (3/30/2012)


    PaulB-TheOneAndOnly (3/30/2012)


    Yes. Try dynamic sql.

    Gimme an example Please...

    Nothing beats vendor's documentation 🙂 check here: http://support.microsoft.com/kb/175850

    _____________________________________
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at Amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.
  • PaulB-TheOneAndOnly (3/30/2012)


    Charmer (3/30/2012)


    PaulB-TheOneAndOnly (3/30/2012)


    Yes. Try dynamic sql.

    Gimme an example Please...

    Nothing beats vendor's documentation 🙂 check here: http://support.microsoft.com/kb/175850

    also try here, http://bit.ly/HppLBX

  • Lowell (3/30/2012)


    how about a view, so you have the date? is that what you want?

    Create view TableToday

    AS

    select

    DATEADD(dd, DATEDIFF(dd,0,getdate()), 0) As TheDate,

    GETDATE() as TheDateTime,

    edit: misread the question...thought you wanted a table with the datetime, not a tablename:

    DECLARE @sql varchar(8000)

    SET @sql = 'CREATE TABLE [TABLE_'

    + DATEADD(dd, DATEDIFF(dd,0,getdate()), 0)

    +' ] (ID int,OtherColumns varchar(30) )'

    I have already tried this one...but its concatenating not creating date & time...

    Thanks,
    Charmer

  • maybe if you could write out a full sql definition example that you are looking for.

    Something like the below. Don't include the dynamic part, just what the output should look like.

    CREATE TABLE tblTemp

    (

    [ID] NUMERIC(18, 0),

    [Date_12312012] DATETIME,

    [Time_1200PM] DATETIME

    )

  • Charmer (3/30/2012)


    Lowell (3/30/2012)


    how about a view, so you have the date? is that what you want?

    Create view TableToday

    AS

    select

    DATEADD(dd, DATEDIFF(dd,0,getdate()), 0) As TheDate,

    GETDATE() as TheDateTime,

    edit: misread the question...thought you wanted a table with the datetime, not a tablename:

    DECLARE @sql varchar(8000)

    SET @sql = 'CREATE TABLE [TABLE_'

    + DATEADD(dd, DATEDIFF(dd,0,getdate()), 0)

    +' ] (ID int,OtherColumns varchar(30) )'

    I have already tried this one...but its concatenating not creating date & time...

    you did try executing the string you build after you concatenate? i just tested this, and it works fine, my first guess was concatenating date + varchar, so that was wrong:

    DECLARE @sql varchar(8000)

    SET @sql = 'CREATE TABLE [TABLE_'

    + convert(varchar,getdate(),112 )

    +'] (ID int,OtherColumns varchar(30) )'

    print @sql --does it look right?

    exec(@sql) --does it run right?

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Lowell (3/30/2012)


    Charmer (3/30/2012)


    Lowell (3/30/2012)


    how about a view, so you have the date? is that what you want?

    Create view TableToday

    AS

    select

    DATEADD(dd, DATEDIFF(dd,0,getdate()), 0) As TheDate,

    GETDATE() as TheDateTime,

    edit: misread the question...thought you wanted a table with the datetime, not a tablename:

    DECLARE @sql varchar(8000)

    SET @sql = 'CREATE TABLE [TABLE_'

    + DATEADD(dd, DATEDIFF(dd,0,getdate()), 0)

    +' ] (ID int,OtherColumns varchar(30) )'

    I have already tried this one...but its concatenating not creating date & time...

    you did try executing the string you build after you concatenate? i just tested this, and it works fine, my first guess was concatenating date + varchar, so that was wrong:

    DECLARE @sql varchar(8000)

    SET @sql = 'CREATE TABLE [TABLE_'

    + convert(varchar,getdate(),112 )

    +'] (ID int,OtherColumns varchar(30) )'

    print @sql --does it look right?

    exec(@sql) --does it run right?

    Thank you Lowell...Its good..I get it what i expected...

    Thanks,
    Charmer

Viewing 10 posts - 1 through 9 (of 9 total)

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