Conversion error with NULL column and SELECT INTO - sql-server-2008

I'm experimenting with temporary tables and running into a problem.
Here's some super-simplified code of what I'm trying to accomplish:
IF(Object_ID('tempdb..#TempTroubleTable') IS NOT NULL) DROP TABLE #TempTroubleTable
select 'Hello' as Greeting,
NULL as Name
into #TempTroubleTable
update #TempTroubleTable
set Name = 'Monkey'
WHERE Greeting = 'Hello'
select * from #TempTroubleTable
Upon attempting the update statement, I get the error:
Conversion failed when converting the varchar value 'Monkey' to data type int.
I can understand why the temp table might not expect me to fill that column with varchars, but why does it assume int? Is there a way I can prime the column to expect varchar(max) but still initialize it with NULLs?

You need to cast null to the datatype because by default its an int
Select 'hello' as greeting,
Cast (null as varchar (32)) as name
Into #temp

Related

Insert date in table using json_populate_recordset

I'm trying insert data in table using this query
INSERT INTO table (
url,
v_count,
v_date)
SELECT
url,
v_count,
v_date FROM json_populate_recorset(null::record,
'[{"url_site":"test.com","visit_count":1,"visit_date":"2022-08-31"},
{"url_site":"dev.com","visit_count":2,"visit_date":"2022-08-31"}]'::json)
AS ("url" varchar(700), "v_count" integer, "v_date" date)
And I'm getting this error:
null value in column "v_date" of relation table violates not null constraint
Since my json could be hundreds of entries at some times,
how should I send the date in my json ?
There is another (efficient) way to insert this data in the table ?
Edit: in postico 1.5.20 my example above works as long as I have the json key named the same as the table columns, how can I reference differents names in my json keys?
Since v_date can resolve to null, you'll need to either skip them or provide a value when null appears.
To skip the null values, you may want to add a WHERE v_date NOTNULL clause to your SELECT statement.
Otherwise, you can use COALESCE() to assign a value when v_date is null. For example ... SELECT url, v_count, COALESCE(v_date,now()) FROM json_populate_recordset...

get 0 when emptyornull values else max(id) when column datatype is numeric in sql server

Hi I have one doubt in sql server
get 0 when emptyornull values else max(id) when column datatype i is numeric in sql server
Table : empid
CREATE TABLE [dbo].[empid](
[id] [numeric](11, 0) NULL
) ON [PRIMARY]
GO
INSERT [dbo].[empid] ([id]) VALUES (NULL)
GO
INSERT [dbo].[empid] ([id]) VALUES (CAST(6 AS Numeric(11, 0)))
GO
based on above data I want output like below
id
6
I tried like below
select case when isnull( max(id),'')='' then cast (0 as numeric) else max(id end test from )
[Test].[dbo].[empid]
but above query is getting error
Msg 8114, Level 16, State 5, Line 9
Error converting data type varchar to numeric.
suppose no records in table then maxid will get 0
please tell me how to write a query to achive this task in sql server
You should use the COALESCE function that gives you the ability to replace a potential NULL with whatever you wish (0 in your current case) as so:
select coalesce(id, 0) as id from [dbo].[empid];
Why use ''? Just use 0:
SELECT ISNULL(id,0) AS test
FROM dbo.empid;
ISNULL returns the datatype of the first parameter, and with the SQL you had, you were therefore implicitly trying to convert '' to a numeric, which was failing.
The better way to do this is to use COALESCE function provided by SQL
COALESCE(arg1, arg2[, argN])
what it does is it takes N arguments
arg1 is a column that can be null
arg2 is a column that can be null
argN is the value that you want to replace it with
Query could be
SELECT COALESCE(Id, 0) as Id FROM [dbo].[empid]

Insert empty value into mysql date field?

I need to insert an empty value into a date field.
I've read how to insert an empty value in mysql date type field? which states to set the field to allow null. I've done that.
It also states to isnert null rather than an empty value. Unfortunatly that's not possible with the current set up - is there a way to allow it to input an empty string?
Say, your table is:
CREATE TABLE MyTable (A INT ,
B INT ,
C INT
) ;
The statement:
INSERT INTO MyTable (B,C) VALUES (3,4) ;
will leave A null.

SQL Server thinks i'm trying to convert varchar to int when using a hypen

When I run this SQL statement:
SELECT commentor, comment, cdate, ddate
FROM delivery_comments
WHERE Jobno = '93388-01' and ddate = '2016-03-11'
ORDER BY cdate
I get this error:
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value '93388-01' to data type int.
I'm not trying to convert it to an int. I want it to stay varchar. How do I do this?
I am totally agree with #xQbert, why are you comparing type of varchar with int along with '-' (hyphen) when its sure that int never has such value...
you can do the comparison like as follows :
Cast(Jobno As Varchar(20)) = '93388-01'
still you never get the result as '-' (hyphen) never exists in your data.

Conversion failed when converting varchar to data type int

CREATE PROCEDURE [dbo].[GetIssuedGatePassByGatepassType]
(
#GatePass_Type int,
#plant int
)
AS
SET NOCOUNT ON;
SELECT
GatePass_Type, Batch_No, Issued, Batch_Id
FROM
GatePass_Batch
WHERE
(Issued = 0)
AND (GatePass_Type = #GatePass_Type)
AND Location_Id = #plant
ORDER BY
CAST(Batch_No AS INT) ASC
Batch_no is of data type varchar in table
This gives me an error like
conversion failed varchar to data type int
Please give answer
CAST'ing a VARCHAR column to an INT will result in an error if any records contains anything but numbers in the VARCHAR column. So if your VARCHAR column have alphabetical or special characters, it will fail when CAST'ing to an INT. Even when you're just trying to sort by that column.
To work around this, you could perform a check on whether the content of the column is purely numeric, before casting:
ORDER BY
CASE WHEN ISNUMERIC(Batch_No) = 1 THEN CAST(Batch_No AS INT) ELSE 99999999 END ASC
Here, I'm sorting by Batch_No as an integer only when Batch_No is numeric. In case Batch_No is not numeric, I'm sorting by some large number, to make sure that the non-numeric Batch_No records appear at the end of the dataset.
Note that ISNUMERIC is not perfect. It will return 1 in some very special cases, where CAST(Batch_No AS INT) will still fail. For example: SELECT ISNUMERIC('$') will return 1, but SELECT CAST('$' AS INT) will result in an error. Check out the remarks here for more information. See also this SO question.