SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


getdate() in dynamic SQL


getdate() in dynamic SQL

Author
Message
smknox
smknox
Say Hey Kid
Say Hey Kid (713 reputation)Say Hey Kid (713 reputation)Say Hey Kid (713 reputation)Say Hey Kid (713 reputation)Say Hey Kid (713 reputation)Say Hey Kid (713 reputation)Say Hey Kid (713 reputation)Say Hey Kid (713 reputation)

Group: General Forum Members
Points: 713 Visits: 126
I would like to use the value of GetDate() within a dynamic SQL statement.
THe following line:

', DateUpdated = '+ GetDate()+ CHAR(13)+

produces a syntax error "error converting datetime from character string"

Thanks for the help.

Sam



RBarryYoung
RBarryYoung
SSC-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: General Forum Members
Points: 35778 Visits: 9518
Try this:

', DateUpdated = '+ CAST(GetDate() as varchar(24))+ CHAR(13)+



-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
RBarryYoung
RBarryYoung
SSC-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: General Forum Members
Points: 35778 Visits: 9518
Though looking at it I think that you will need more apostrophes...

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Prasad Bhogadi
Prasad Bhogadi
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2537 Visits: 450
smknox (5/26/2008)
I would like to use the value of GetDate() within a dynamic SQL statement.
THe following line:

', DateUpdated = '+ GetDate()+ CHAR(13)+

produces a syntax error "error converting datetime from character string"

Thanks for the help.

Sam

Try This

', DateUpdated = '''+ CONVERT(VARCHAR(12),GETDATE(),101)+''''+ CHAR(13) +

Prasad Bhogadi
www.inforaise.com
Prasad Bhogadi
Prasad Bhogadi
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2537 Visits: 450
You may want to use the convert function based on how you want to save the output of GETDATE() function in your DateUpdated Column.

Prasad Bhogadi
www.inforaise.com
Sergiy
Sergiy
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26112 Visits: 12483
', DateUpdated = GetDate() ' + CHAR(13)
Smile
Prasad Bhogadi
Prasad Bhogadi
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2537 Visits: 450
Sergiy (5/27/2008)
', DateUpdated = GetDate() ' + CHAR(13)
Smile

This is straight forward. We were trying to re-invent the wheel Smile

Prasad Bhogadi
www.inforaise.com
smknox
smknox
Say Hey Kid
Say Hey Kid (713 reputation)Say Hey Kid (713 reputation)Say Hey Kid (713 reputation)Say Hey Kid (713 reputation)Say Hey Kid (713 reputation)Say Hey Kid (713 reputation)Say Hey Kid (713 reputation)Say Hey Kid (713 reputation)

Group: General Forum Members
Points: 713 Visits: 126
Thanks! Sergei's syntax was it!

Sam



Jacob Luebbers
Jacob Luebbers
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1422 Visits: 1215
One very small thing: unless you're going to be somehow running the dynamic SQL string on a pre-OSX Mac(!) I'd use the "correct" Windows CRLF sequence, which is "char(13) + char(10)" rather than just plain "char(13)".

Doesn't make a difference to the SQL parser, but it's cleaner if you're going to be visualising the string in most Windows apps.

Regards,

Jacob
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search