TSQL convert datetime to int error - tsql

SQL is giving an error on this line:
IF YEAR(#Var_Report_To)%(4)=0 AND MONTH(#Var_Report_To)>=3
Error: Implicit conversion from data type datetime to int is not allowed. Use the CONVERT function to run this query.
Any ideas on how to fix?

Assuming your variable is a legit date, I don't see a problem with your expression. The following runs fine for me:
DECLARE #Var_Report_To DATETIME = '2016-03-01';
IF YEAR(#Var_Report_To)%(4)=0 AND MONTH(#Var_Report_To)>=3
SELECT GETDATE()

Related

Conversion failed when converting the varchar value 'F01' to data type int

It appears that [t_l_unit] being a varchar is conflicting with the filters in my Where clause. Can I fix this with a Cast of some sort?
Going by the title of your post, your are trying to convert the value "F01" to an INT?
Obviously this is not possible because of the "F" in the string.
What you can do is use the TRY_CAST function which will convert the value to an INT if it can and will return NULL otherwise
SELECT val1, va2, etc
FROM myTable
WHERE TRY_CAST([t_l_unit] as INT) = 12
The query didn't come through, but you should be able to just do:
CAST([t_l_unit] AS INT)
to use it as a number in your where clause.

syntax used to type cast date(data type_ to integer in postgresql

I want convert Date data type to Integer, i have tried to type cast the date(data type) to integer by using ::INT, its not working.
I have got the following error
ERROR: column "date" is of type integer but expression is of type
date HINT: You will need to rewrite or cast the expression.
What you can do is:
replace(your_date::varchar, '/', '')::integer
Full example:
select replace(now()::date::varchar, '-', '')::integer
Use the TO_CHAR function with FM prefix and then cast it to Integer.
select to_char(date_column,'FMddFMmmYYYY')::INT as dateint FROM t
Demo

TSQL Cannot create index on view - the view uses an implicit conversion from string to datetime

SQL SERVER 2008 r2
I'm trying to Create an Indexed view however I'm getting the following error
Cannot create index on view '' because the view uses an implicit conversion from string to datetime or smalldatetime. Use an explicit CONVERT with a deterministic style value.
The issue is with an INT column [GPSTime] that records the number of seconds from '1970-01-01 00:00:00' and I'm trying to CONVERT/CAST this is to a DATETIME, eg
CAST(DATEADD(SS,[GPSTime],'1970-01-01' ) AS DATETIME)
or
CONVERT(VARCHAR,DATEADD(SS,[GPSTime],'1970-01-01' ),113)
or
CONVERT(DATETIME,DATEADD(SS,[GPSTime],'1970-01-01' ),113)
Each of the three options above gives me the error I mentioned earlier.
Is the way around this?
Going to make a guess that the issue is actually on the '1970-01-01', try this:
Dateadd(ss, gpsTime, convert(datetime, '1970-01-01', 101))
Or you could keep the datetime value in another table (as a datetime to avoid convert) or write a deterministic function to return your datetime as such:
create function [dbo].[UnixEpoch]
()
returns datetime
with schemabinding
as
begin
RETURN convert(datetime, '1970-01-01', 101)
end
go
select objectproperty(object_id('[dbo].[UnixEpoch]'), 'IsDeterministic')
SELECT dbo.unixEpoch()
EDIT:
note the datetime style applied to the convert(datetime, '1970-01-01', 101)
according to documentation at http://msdn.microsoft.com/en-us/library/ms178091.aspx :
Source or target type is datetime or smalldatetime, the other source
or target type is a character string, and a nondeterministic style is
specified. To be deterministic, the style parameter must be a
constant. Additionally, styles less than or equal to 100 are
nondeterministic, except for styles 20 and 21. Styles greater than 100
are deterministic, except for styles 106, 107, 109 and 113.
i got the same error and fixed it by not only using convert:
CONVERT(DATETIME, firstdate, 102) AS firstdate
On the SELECT clause, but also in the WHERE clause as:
WHERE firstdate >= CONVERT(DATETIME,'20150101',102)
I hope this solution helps!

How to embed a function returning a string in a Q query?

I'm using Q.f to format column fields from integer to float with 4 digits precision:
fmt_price:{[val] .Q.f[4;](val*0.0001)}
select fmt_price[price] from mytable
The fmt_price works well at the q prompt, but if I embed the function in a query I get this error:
An error occurred during execution of the query. The server sent the
response: `type
The fmt_price call works if I return a float or integer variable, rather than the result of Q.f.
You need to do an each over the list. Currently you are passing a list of values to .Q.f, when it expects an atom. Something like the following is what you need:
fmt_price:{[val] .Q.f[4;] each (val*0.0001)}

function to_char(unknown, unknown) is not unique

When running the following the query.select * from surgicals where to_char(dt_surgery ,'DD-MM-YYYY' ) = to_char('12-02-2012','DD-MM-YYYY');
the error coming as 'SQL state 42725: ERROR: function to_char(unknown, unknown) is not unique'
How to run above select query?
You probably mean to_char('12-02-2012'::date, 'DD-MM-YYYY'). to_char cannot convert a plain string to string. Still, it does not seem to make sense, you need one of these two, depending on the format of your date constant (which cannot be determined from the actual example date you provided):
select * from surgicals where to_char(dt_surgery ,'DD-MM-YYYY' ) = '12-02-2012';
select * from surgicals where to_char(dt_surgery ,'MM-DD-YYYY' ) = '12-02-2012';
The wrongness here is that you're doing string comparison of dates. Use date/time math, which can take into account fun things like time zones etc. and still get it right.
Maybe this is what you need:
SELECT *
FROM surgicals
WHERE date_trunc('day', dt_surgery) = '2012-02-12'
;