Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody,
Select Query is not returning any values
QUERY:

SQL
SELECT 
    Latitude,
    Longitude 
FROM 
    table 
WHERE 
    Device_DateTime BETWEEN '2014-03-26 14:18:00.000' AND '2014-03-26 14:19:00.000';

-- The above Query is returning values
But in case of Dyanamic Query:
SQL
SELECT @SQL=N'SELECT Latitude,Longitude FROM dbo.' + @tablename +
' WHERE Device_DateTime BETWEEN ''' + @MinId  + ''' AND ''' + @toDateTime + '''
EXEC sp_executesql @SQL

--The above Query is returning Error like Conversion failed when converting date and/or time from character string.

So i changed Query as CAST(Device_DateTime as NVARCHAR(50))

Its not giving any error but values are empty..

Any help can be appreciated...
Posted
Updated 26-Mar-14 22:34pm
v3

I had the same problem in some old projects. The error could be generated if you are using different Culture in the data that come from your application and your DB server. For example if in your code your Thread.CurrentThread.CurrentUICulture.Name == "de-DE" and you are using sending a date like "23/07/2013" (the German format for date is "dd/mm/yyyy"), and your DB server are using English culture ==> an error will be generated because he expect the next date format "mm/dd/yyyy"!

So the solution is that in your code, before to setup the date param to convert it to string by using the CultureInfo used in your DB server.
 
Share this answer
 
Comments
Member 10017719 27-Mar-14 4:47am    
Thank u Raul Iloc
Raul Iloc 27-Mar-14 5:21am    
Welcome, if my solution helped you you can accepted.

Note that the solution2 din not solve your problem, it just works for a particular case (see above my comment that I added to it)!
here is the alternate way, hope this will help without using culture Info

SQL
DECLARE @SFromDate  varchar(20)
DECLARE @SToDate    varchar(20)

SET @SFromDate = convert(char(18),  '2014-03-26 14:18:00.000', 107)
SET @SToDate = convert(char(18), '2014-03-26 14:18:00.000', 107)

DECLARE @SQL varchar(1000)
SELECT @SQL =N'SELECT Latitude,Longitude FROM urtablename
WHERE urDates between CAST(''' + @SFromDate + '''As datetime) and CAST(''' + @SToDate + ''' As datetime)'

exec (@sql)
 
Share this answer
 
Comments
Member 10017719 27-Mar-14 4:47am    
Awesome...:)
Raul Iloc 27-Mar-14 5:19am    
Your solution is not solving the problem, and is working only if you know the format of the date time string, but this was the problem that this format depends on the current culture used in the application that call the stored procedure.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900