I have update statement that keeps failing .
Update dbo.Marker
set MarkId= case
when Ninja = 55 and cast(Tometo as varchar) = 0031A then 22
else MarkId
end
the data type for MarkId is int data type ,
Ninja is int data type and Tometo is varchar (9) .
this is the error i am getting
Msg 245, Level 16, State 1, Line 48
Conversion failed when converting the varchar value '031A' to data type int.
You have missing quotes around 0031A
Update dbo.Marker
set MarkId= case
when Ninja = 55 and cast(Tometo as varchar) = '0031A' then 22
else MarkId
end
To find the rows that cause the issue run below query
select case
when Ninja = 55 and cast(Tometo as varchar) = '0031A' then 22
else MarkId
end
from dbo.Marker
then find out what rows return non-integer value. I can't help more w/o having your data.
Related
I have a table fepu00 and trigger on it.
The part of code causing tha problem looks like follows:
if (old_record.potime = NEW.putime --new AP (read in statement above)
or old_record.pupisb::NUMERIC != NEW.pupisb::NUMERIC) then
insert into dl356_table
values (old_record.poid, old_record.poidma, old_record.ponmaf,
old_record.adstr, old_record.adpsc, old_record.adcit, old_record.adidze,
NEW.pupisb::numeric,
case when old_record.potime = NEW.putime then '1' else '2' end,
NEW.putime);
end if;
The query I'm executing is really simple:
update fepu00 set pudas = ? where puid = ?
It works, but only for some quantity of times. Then it throws:
ERROR: type of parameter 25 (numeric) does not match that when preparing the plan (text)
Where: PL/pgSQL function dl356_trigger() line 75 at IF
Number of updated records varies from a few to a few hundreds.
When I run the same query (with the same parameters) again, it works properly until the next fail.
Thanks for any suggestions.
I have a stored procedure as under
ALTER PROCEDURE [dbo].[usp_testProc]
(#Product NVARCHAR(200) = '',
#BOMBucket NVARCHAR(100) = '')
--exec usp_testProc '','1'
AS
BEGIN
SELECT *
FROM Mytbl x
WHERE 1 = 1
AND (#Product IS NULL OR #Product = '' OR x.PRODUCT = #Product)
AND (#BOMBucket IS NULL OR #BOMBucket = '' OR CAST(x.BOMBucket AS NVARCHAR(100)) IN (IIF(#BOMBucket != '12+', #BOMBucket, '13,14')))
END
Everything else if working fine except when I am passing the bucket value as 12+ . It should ideally show the result for bucket 13 and 14. But the result set is blank.
I know IN expects values as ('13','14'). But somehow not able to fit it in the program.
You can express that logic in a Boolean expression.
...
(#BOMBucket <> '12+'
AND cast(x.BOMBucket AS nvarchar(100)) = #BOMBucket
OR #BOMBucket = '12+'
AND cast(x.BOMBucket AS nvarchar(100)) IN ('13', '14'))
...
But casting the column prevents indexes from being used. You rather should cast the other operand. Like in:
x.BOMBucket = cast(#BOMBucket AS integer)
But then you had the problem, that the input must not be a string representing an inter but can be any string. this would cause an error when casting. In newer SQL Server versions you could circumvent that by using try_cast() but not in 2012 as far as I know. Maybe you should rethink your approach overall and pass a table variable with the wanted BOMBucket as integers instead.
Is it possible, and if so, how can the following change be achieved?
Given the table:
param_tab
param_id serial
value integer
anothervalue integer
update_date TIMESTAMP
I would like to do something similar to this:
UPDATE param_tab pt
CASE WHEN CONDITION THEN pt.value = 14, pt.anothervalue = 20 END
pt.update_date = someTimestamp;
So update_date is always updated and value and anothervalue only in case of some condition
Use the CASE statement in the correct place:
UPDATE param_tab pt
SET value = CASE WHEN condition THEN 14 ELSE pt.value END,
anothervalue = CASE WHEN condition THEN 20 ELSE pt.anothervalue END,
update_date = someTimestamp;
I have a piece of code that inserts into a table. Simply, I have a table with structure
Table1:
Id int PK
Type tinyint
Status tinyint
After digging in our PROD, I observed that EF might be intermittently defaulting the value to 255 when the value should range (1-3). I understand 255 is the max value for tinyint in SQL Server but I have no defaults in both table definition and in EDMX.
Usage:
//OUTAGE_TYPE
public enum OUTAGE_TYPE
{
Unknown = -1,
SwitchingPlan = 1,
PlannedIncident = 2,
UnplannedIncident = 3
}
//CREATE INSTANCE
var outage = new Outage
{
OutageTypeId = (byte)OUTAGE_TYPE.SwitchingPlan,
OutageStatusId = (byte)OUTAGE_STATUS.Approved,
IncidentRID = incident.RID,
WorkRID = incident.WorkRID,
ETR = incident.ETR,
DateInserted = Time.Now(),
DateUpdated = Time.Now()
};
Captured SQL:
INSERT [dbo].[Outage]([OutageTypeId], [OutageStatusId], [IncidentRID], [WorkRID], [ETR], [DateInserted], [DateUpdated])
VALUES (#0, #1, #2, NULL, #3, #4, #5)
SELECT [Id]
FROM [dbo].[Outage]
WHERE ##ROWCOUNT > 0 AND [Id] = scope_identity()
-- #0: '255' (Type = Byte, Size = 1)
-- #1: '1' (Type = Byte, Size = 1)
-- #2: 'INC 11000700' (Type = String, Size = 255)
-- #3: '05-08-2016 10:05:52' (Type = DateTime2)
-- #4: '05-08-2016 22:05:22' (Type = DateTime2)
-- #5: '05-08-2016 22:05:22' (Type = DateTime2)
-- Executing at 06-08-2016 00:06:00 +02:00
Error:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Outage_OutageType". The conflict occurred in database "ONSP", table "dbo.OutageType", column 'Id'.
I was not able to reproduce on my local machine, maybe you guys have experienced this before. Many thanks.
After my colleague and me digging around, we found the root cause of this problem.
First -1, is not a valid value for a byte and direct conversion will not throw exception but will yield to byte.MaxValue
void Main()
{
var type = OUTAGE_TYPE.Unknown;
var result = (byte)type;
//result = 255!!!
Console.WriteLine(result);
}
public enum OUTAGE_TYPE
{
Unknown = -1,
SwitchingPlan = 1,
PlannedIncident = 2,
UnplannedIncident = 3
}
In PostgreSQL: I convert string to timestamp with to_timestamp():
select * from ms_secondaryhealthcarearea
where to_timestamp((COALESCE(update_datetime, '19900101010101'),'YYYYMMDDHH24MISS')
> to_timestamp('20121128191843','YYYYMMDDHH24MISS')
But I get this error:
ERROR: syntax error at end of input
LINE 1: ...H24MISS') >to_timestamp('20121128191843','YYYYMMDDHH24MISS')
^
********** Error **********
ERROR: syntax error at end of input
SQL state: 42601
Character: 176
Why? How to convert a string to timestamp?
One too many opening brackets. Try this:
select *
from ms_secondaryhealthcarearea
where to_timestamp(COALESCE(update_datetime, '19900101010101'),'YYYYMMDDHH24MISS') >to_timestamp('20121128191843','YYYYMMDDHH24MISS')
You had two opening brackets at to_timestamp:
where to_timestamp((COA.. -- <-- the second one is not needed!
#ppeterka has pointed out the syntax error.
The more pressing question is: Why store timestamp data as string to begin with? If your circumstances allow, consider converting the column to its proper type:
ALTER TABLE ms_secondaryhealthcarearea
ALTER COLUMN update_datetime TYPE timestamp
USING to_timestamp(update_datetime,'YYYYMMDDHH24MISS');
Or use timestamptz - depending on your requirements.
Another way to convert a string to a timestamp type of PostgreSql is the above,
SELECT to_timestamp('23-11-1986 06:30:00', 'DD-MM-YYYY hh24:mi:ss')::timestamp without time zone;
I had the same requirement as how I read the title. How to convert an epoch timestamp as text to a real timestamp. In my case I extracted one from a json object. So I ended up with a timestamp as text with milliseconds
'1528446110978' (GMT: Friday, June 8, 2018 8:21:50.978 AM)
This is what I tried. Just the latter (ts_ok_with_ms) is exactly right.
SELECT
data->>'expiration' AS expiration,
pg_typeof(data->>'expiration'),
-- to_timestamp(data->>'expiration'), < ERROR: function to_timestamp(text) does not exist
to_timestamp(
(data->>'expiration')::int8
) AS ts_wrong,
to_timestamp(
LEFT(
data->>'expiration',
10
)::int8
) AS ts_ok,
to_timestamp(
LEFT(
data->>'expiration',
10
)::int8
) + (
CASE
WHEN LENGTH(data->>'expiration') = 13
THEN RIGHT(data->>'expiration', 3) ELSE '0'
END||' ms')::interval AS ts_ok_with_ms
FROM (
SELECT '{"expiration": 1528446110978}'::json AS data
) dummy
This is the (transposed) record that is returned:
expiration 1528446110978
pg_typeof text
ts_wrong 50404-07-12 12:09:37.999872+00
ts_ok 2018-06-08 08:21:50+00
ts_ok_with_ms 2018-06-08 08:21:50.978+00
I'm sure I overlooked a simpler version of how to get from a timestamp string in a json object to a real timestamp with ms (ts_ok_with_ms), but I hope this helps nonetheless.
Update: Here's a function for your convenience.
CREATE OR REPLACE FUNCTION data.timestamp_from_text(ts text)
RETURNS timestamptz
LANGUAGE SQL AS
$$
SELECT to_timestamp(LEFT(ts, 10)::int8) +
(
CASE
WHEN LENGTH(ts) = 13
THEN RIGHT(ts, 3) ELSE '0'
END||' ms'
)::interval
$$;