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

  • SEARCH
  • Home
  • 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 7760007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:46:13+00:00 2026-06-01T13:46:13+00:00

I have created a procedure with this structure but it doesn’t work for datetime

  • 0

I have created a procedure with this structure but it doesn’t work for datetime input parameter

I executed this query but

declare @a datetime
declare @b datetime 

set @a='2012/04/06 12:23:45'
set @b='2012/08/06 21:10:12'

exec LogProcedure 'AccountLog', N'test', @a, @b

but SQL Server got me this error

Conversion failed when converting date and/or time from character string.

but when I test with this query it works

  exec LogProcedure 'AccountLog',N'test'

Stored procedure code:

alter PROCEDURE LogProcedure
    @TableName VARCHAR(60),
    @SearchString NVARCHAR(50),
    @DateFirst DateTime = '',
    @DateLast DateTime = ''
AS
BEGIN
    SET NOCOUNT ON

    DECLARE @FinalSQL      NVARCHAR(MAX)

    SET @FINALSQL = 'SELECT * FROM [' + @TableName + '] where 1=2  '

    IF @DateFirst <> '' and @DateLast <> ''
       set @FinalSQL  = @FinalSQL + '  or convert (Date,DateLog) >=     '''+@DateFirst + ' and convert (Date,DateLog) <='''+@DateLast  

    SELECT 
       @FinalSQL  = @FinalSQL + ' or  [' + SYSCOLUMNS.NAME + '] LIKE N''%' + @SearchString + '%'' ' 
    FROM SYSCOLUMNS 
    WHERE OBJECT_NAME(id) = @TableName
    AND TYPE_NAME(SYSCOLUMNS.XTYPE) IN ('VARCHAR','NVARCHAR','CHAR','NCHAR','INT','DECIMAL')
    ORDER BY COLID

    EXEC(@FinalSQL)
END 

This query is true too

SELECT * 
FROM AccountLog 
where 1=2  or convert (Date, DateLog) >= '2012/04/06' 
and convert (Date, DateLog) <='2012/08/06'
  • 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-01T13:46:15+00:00Added an answer on June 1, 2026 at 1:46 pm

    You should use the ISO-8601 format for string representations of dates – anything else is dependent on the SQL Server language and dateformat settings.

    The ISO-8601 format for a DATETIME when using only the date is: YYYYMMDD (no dashes or antyhing!)

    For a DATETIME with the time portion, it’s YYYY-MM-DDTHH:MM:SS (with dashes, and a T in the middle to separate date and time portions).

    If you want to convert a string to a DATE for SQL Server 2008 or newer, you can use YYYY-MM-DD (with the dashes) to achieve the same result. And don’t ask me why this is so inconsistent and confusing – it just is, and you’ll have to work with that for now.

    So in your case, you should try:

    declare @a datetime
    declare @b datetime 
    
    set @a = '2012-04-06T12:23:45'   -- 6th of April, 2012
    set @b = '2012-08-06T21:10:12'   -- 6th of August, 2012
    
    exec LogProcedure 'AccountLog', N'test', @a, @b
    

    Furthermore – your stored proc has problem, since you’re concatenating together datetime and string into a string, but you’re not converting the datetime to string first, and also, you’re forgetting the close quotes in your statement after both dates.

    So change this line here to this:

    IF @DateFirst <> '' and @DateLast <> ''
       SET @FinalSQL  = @FinalSQL + '  OR CONVERT(Date, DateLog) >= ''' + 
                        CONVERT(VARCHAR(50), @DateFirst, 126) +   -- convert @DateFirst to string for concatenation!
                        ''' AND CONVERT(Date, DateLog) <=''' +  -- you need closing quotes after @DateFirst!
                        CONVERT(VARCHAR(50), @DateLast, 126) + ''''      -- convert @DateLast to string and also: closing tags after that missing!
    

    With these settings, and once you’ve fixed your stored procedure which contains problems right now, it will work.

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

Sidebar

Related Questions

I have a procedure that expects a parameter of type TObject, something like this:
I have this stored procedure: CREATE OR REPLACE PROCEDURE LIQUIDACION_OBTENER ( p_Cuenta IN NUMBER,
I have this stored procedure for Oracle: create or replace procedure bns_saa_message_get() <--- PROBLEM
I have created a procedure and used the below statement inside that . select
I have created the following stored procedure.. CREATE PROCEDURE [dbo].[UDSPRBHPRIMBUSTYPESTARTUP] ( @CODE CHAR(5) ,
I have just created a report in Report Manager using a Stored Procedure which
I have a modal dialog that is created with the following: procedure TFormCompose.createParams(var Params:
I have a dynamic SQL statement I've created in a stored procedure. I need
I created and launch the SQLJ Java procedure (using SQL Developer). I have some
I have a TSQL script that does a lot of database structure adjustments but

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.