What is wrong with this sql query. I am getting syntax error near convert
"SELECT * FROM collections c where c.submittedBy='679' AND CONVERT(varchar, c.eventTime, 103) ='2017-06-21'";
The eventTime is "eventTime": "2017-06-21T12:20:03.9366135+05:30"
The error is
{"Message: {\"errors\":[{\"severity\":\"Error\",\"location\":{\"start\":64,\"end\":71},\"code\":\"SC1001\",\"message\":\"Syntax error, incorrect syntax near 'CONVERT'.\"}]}\r\nActivityId: a0c51c53-fca0-4c81-8f10-65348749e3d2"}
The best way to write this query (across databases) is:
SELECT c.*
FROM collections c
WHERE c.submittedBy='679' AND
c.eventTime >= '2017-06-21' AND
c.eventTime < '2017-06-22';
This allows the database to use an appropriate index for filtering. Note that if submittedBy is a number, then don't use single quotes.
convert to date before to convert to varchar
SELECT *
FROM collections c
where c.submittedBy='679'
AND CONVERT(varchar(10), CONVERT(date, c.eventTime, 103)) = '2017-06-21'
if eventTime is already a datetime field should work also this way..
SELECT *
FROM collections c
where c.submittedBy='679'
AND CONVERT(date, c.eventTime) = '2017-06-21'
Related
Is it possible to use the LEFT() operator in MSSQL on a datetime.
I'm asking because this is my db:
In a SQL query i want now to SELECT only this objects WHERE Rueckmeldetatum = a date in form like (2023-01-27) so my string comperable has no time just a date. But with this SQL Query I'm getting no results:
SELECT TOP (1000) [KNR]
,[Rueckmeldedatum]
FROM [Fertigung].[dbo].[Box1Auswertung]
WHERE LEFT(Rueckmeldedatum,10) ='2023-01-27'
But normally or what i want to get is the 20th entry from the picture.
You should cast the datetime to date, then compare to a date literal:
SELECT TOP (1000) [KNR], [Rueckmeldedatum]
FROM [Fertigung].[dbo].[Box1Auswertung]
WHERE CAST(Rueckmeldedatum AS date) = '20230127';
Or, better yet, use this sargable version:
SELECT TOP (1000) [KNR], [Rueckmeldedatum]
FROM [Fertigung].[dbo].[Box1Auswertung]
WHERE Rueckmeldedatum >= '20230127 00:00:00' AND
Rueckmeldedatum < '20230128';
You should use this instead of LEFT or casting, then, if the index is available, your query can use this:
SELECT TOP (1000) [KNR]
,[Rueckmeldedatum]
FROM [Fertigung].[dbo].[Box1Auswertung]
WHERE Rueckmeldedatum >= '2023-01-27' AND Rueckmeldedatum < DATEADD(DAY, 1, '2023-01-27')
Also, make your query from the application parameterized.
I am trying to calculate a date using two table. Here the query
select DATEDIFF(DAY, convert(varchar,D.BUSS_DATE, 23), convert(varchar,A.BUSS_DATE,23))
FROM dbo.TrxA1 A WITH (NOLOCK)
LEFT JOIN SOURCE_TBL_MASTER_EXCHANGE_RATE_HISTORY D WITH (NOLOCK)
ON A.CCY = D.SPOT_RATE
But I get the error
Error converting data type varchar to numeric.
I have been converting those date columns to varchar but the error is still same.
Please help. Thank you.
You should be converting to dates not strings. No conversion may be needed at all if your data is using the correct types:
select datediff(day, D.BUSS_DATE, A.BUSS_DATE)
from dbo.TrxA1 A left join
SOURCE_TBL_MASTER_EXCHANGE_RATE_HISTORY D
on A.CCY = D.SPOT_RATE;
If your values are inappropriate stored as strings, you can convert using:
select datediff(day, convert(date, D.BUSS_DATE, 23), convert(date, A.BUSS_DATE, 23))
If you still have a problem, you can fix it using try_convert():
select datediff(day, try_convert(date, D.BUSS_DATE, 23), try_convert(date, A.BUSS_DATE, 23))
But that just masks the problem. You should find the offensive values:
select buss_date
from dbo.TrxA1 A
where try_convert(date, buss_date, 23) is null and buss_date is not null;
Then fix the values and change the data type to an appropriate date/time type.
Finally, do not use NOLOCK unless you know what it is doing. And you probably don't. NOLOCK is a license that says: "You can get me data from the table that is not 100% accurate."
i have a select where i need to make sure that my adate varchar(8) is between two dates supplied.
sometimes the passed in values are NULL so i would then show everything.
select something from myTable where
convert(date, adate) >= ISNULL(convert(date,#beginDate), convert(date, adate))
works fine
but when i make it:
select something from myTable where
convert(date, adate) >= ISNULL(convert(date,#beginDate), convert(date, adate))
and convert(date, adate) <= ISNULL(convert(date,#endDate), convert(date, adate))
i get:
Conversion failed when converting date and/or time from character string.
or i can do the <= line without the >= without an error, but not both of them together, what's going on??
the two lines look identical, i can't figure out what i'm doing wrong here.....
Check your value for #endDate. That parameter is your only difference between the SELECT that works, and the one that doesn't. Chances are, it (sometimes) contains a value that cannot be converted to a date. Usual suspect is the empty string ('')
It looks both #startdate and #enddate are string types. Best thing is to have them in ISO format (yyyymmdd) and you should be fine.
For example try using these;
Select #startdate = '20130101', #enddate = '20130221'
I'm not sure on what you're trying to do but...
Since any of the values could come by NULIFIED, you said you would get every result in the table... so it might be easier just to set the query to filter it on the language you're using before the DB...
Or as I would put on a PHP sample...
IF ( !$beginDate || !$endDate )
{
$query = "SELECT * FROM myTable";
}
ELSE
{
$query = "SELECT * FROM myTable WHERE 'whatever clause'";
}
I am trying to clean up the where clause statement in the following code:
SELECT
CONVERT(datetime, [UTC_Time_Stamp], 127) AS TimeStamp
FROM
Table
WHERE
CASE
WHEN ISDATE([UTC_Time_Stamp]) = 1
THEN CONVERT(datetime, [UTC_Time_Stamp], 127)
ELSE CAST('1/1/1900' AS datetime)
END > CAST('11/09/2012' AS datetime)
AND
CASE
WHEN ISDATE([UTC_Time_Stamp]) = 1
THEN CONVERT(datetime, [UTC_Time_Stamp], 127)
ELSE CAST('1/1/3000' AS datetime)
END < CAST('11/10/2012' as datetime)
ORDER BY
TimeStamp;
UTC_Time_Stamp is stored as a string and is sometimes null. I was previously running into a conversion error inside my where clause. I fixed the error following advice from this question here, but I feel like there has to be a simpler way to achieve the same result.
You need to do the conversion within a case statement. SQL is a descriptive language and does not guarantee the order of processing. So, a where clause does not necessarily happen before other processing, even when it is in a subquery or a CTE. However, SQL does guarantee the order of processing in a case statement (that contains no expressions with aggregation functions).
You can simplify your statement by using a subquery:
select TimeStamp
FROM (select t.*,
Case When ISDATE([UTC_Time_Stamp]) = 1 Then CONVERT(datetime, UTC_Time_Stamp, 127) end) as TimeStamp
from Table t
) t
WHERE coalesce(TimeStamp, cast('1/1/1900' as datetime)) > cast('11/09/2012' as datetime) and
coalesce(TimeStamp, cast('1/1/3000' as datetime)) < cast('11/10/2012' as datetime)
ORDER BY TimeStamp;
This does the conversion to TimeStamp, for valid values. You can then use the variable in the outer query.
I would also encourage you to get used to the ANSI standard format for dates (YYYYMMDD or YYYY-MM-DD) instead of ambiguous formats like '11/10/2012'. It may be clear to you that this means 2012-11-10 . . . or is that 2012-10-11 . . . but the format is ambiguous.
I like CTEs for this, or you could also do temp tables, table variables or an inline derived table like #Derek.
Basically, we're going to grab the proper datatype first, and then have a much easier time creating our query:
;with CTE as (
-- Bring back the column as datetime
select case when isdate(UTC_Time_Stamp) = 1 then cast(UTC_Time_Stamp as datetime) end as UTC_Time_Stamp
from [Table]
)
-- Simple select with the proper datatype
select convert(varchar(50), UTC_Time_Stamp, 127) as [TimeStamp]
from CTE
-- May still need gt and lt functionality
where UTC_Time_Stamp between cast('11/09/2012' as datetime) and cast('11/10/2012' as datetime)
It seems like you're using some arbitrarily small and large values for TimeStamp when you have non-dates, which is probably unnecessary given your comparison, so I've removed them.
Note that I am using the datetime datatype in the CTE and for the comparison, only converting it to a string for presentation.
Also note that between is inclusive, so you might need to go back to your separate > and < where clause.
It's easier to do something like this (using whatever convert/between clause in the predicate that you see fit):
SELECT CONVERT(datetime, [UTC_Time_Stamp], 127) as TimeStamp
FROM (
select [UTC_Time_Stamp]
from Table
WHERE ISDATE([UTC_Time_Stamp]) = 1
) a
WHERE
convert(datetime, [UTC_Time_Stamp], 127) between '11/9/2012' and '11/10/2012'
ORDER BY TimeStamp;
In this time for me this solves the problem of working with temporary table alias
or sub queries that can slow down the selection of millions of records.
select your_column
from your_table
where case when ISDATE(your_column) = 1
then Cast(your_column as date)
end Between '01/01/2016' and '01/01/2017'
order by your_column
Regards
Please help me to find out error in my SQL query. I have created this query to compare dates
select * from Joinplans jp
where cast(convert(varchar,GETDATE(),103) AS datetime) BETWEEN
CASE(convert(varchar,jp.planstartDate,103) AS datetime) AND
CASE(convert(varchar,DATEADD(DAY,jp.planDays,jp.planstartDate),103) AS DATETIME)
It's giving me the error:
incorrect near 'AS'
I am using SQL Server 2005.
You wrote case instead of cast in two instances.
If planStartDate is actually a date, then there is no need to cast it to a character column:
Select ...
From Joinplans jp
where GetDate() Between planStartDate And DateAdd(day, jp.planDays, jp.planStartDate)
Now, if planStartDate is storing both date and time data, then you might want to use something like:
Select ...
From Joinplans jp
Where planStartDate <= GetDate()
And GetDate() < DateAdd(day, jp.planDays + 1, jp.planStartDate)
This ensures that all times on the last date calculated via the DateAdd function are included