Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8838855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:06:14+00:00 2026-06-14T10:06:14+00:00

What is the best way of inserting a datetime value using a dynamic sql

  • 0

What is the best way of inserting a datetime value using a dynamic sql string, whilst at the same time being able to handle the possibility of the value being null?

The current statement inserts into a table from a select statement built using a string. The datetime value is stored in a parameter and the parameter is used in the select.

Like so:

SET @execsql = 'INSERT INTO ( start_date )
SELECT ( ''' + CAST(start_date as VARCHAR) + ''' + ')'
EXECUTE(@execsql)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:06:15+00:00Added an answer on June 14, 2026 at 10:06 am

    As you’re running Sybase something like

    Set @execsql = 'Insert Into <table> ( start_date ) Select (' + 
      Case When start_date is null Then
        'Null' 
      Else 
        '''' + Cast(start_date as Varchar) + '''' 
      End + ')'
    
    Execute(@execsql)
    

    Should do the trick

    Here’s the SQL Server way for reference

    Declare @sql nvarchar(max), @params navarchar(max)
    
    Set @sql = N'Insert Into Table (start_date) Select (@start_date)'
    Set @params = N'@start_date datetime'
    
    Exec sp_executesql @sql, @params, @start_date
    

    Obviously, in this case you wouldn’t need dynamic SQL, but I assume this is part of a larger example.

    As always with dynamic SQL questions, http://www.sommarskog.se/dynamic_sql.html is recommended reading.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's the best way of inserting information in table A and using the index
What is the best way to prepare the log files before inserting their values
what is the best way to do something , like inserting an element at
Possible Duplicate: Best way to prevent SQL injection in PHP? I've made a PHP
I'm reading that using the OUTPUT clause is probably the best way to retrieve
I couldn't find best solution when DateTime value is NULL. I use this techinque
Possible Duplicate: Best way to prevent SQL injection? For logging in: $username = mysql_real_escape_string(htmlspecialchars(strip_tags(trim($_POST['username'])),
I'm having a hell of time grasping the best way to package my Android
What is the best way of inserting python/C++ code in a Lyx document? The
What is the best plain javascript way of inserting X rows into a table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.