I am trying to change the value of the data that comes out of the SQL. Where it usually has the value of 4 in SAL_ClientTypeID, I would like it to say 'Private'. The error I am currently getting is
"Msg 156, Level 15, State 1, Line 3 Incorrect syntax near the keyword
'as'."
Initially I tried the following
CASE
WHEN SAL_ClientTypeID = 4 THEN 'Private'
ELSE SAL_ClientTypeID
END,
but I got this error
"Conversion failed when converting the varchar value 'Private' to data
type int."
SELECT SAL_Account_Ref AS 'Account Ref',
CASE
WHEN SAL_ClientTypeID = 4 THEN cast (sal_clienttypeID as nvarchar(20)) as 'Private'
ELSE SAL_ClientTypeID
END
FROM sales;
I expect the value of output where SAL_ClientTypeID = 4 to be 'Private'
SELECT SAL_Account_Ref AS 'Account Ref',
CASE
WHEN SAL_ClientTypeID = 4 THEN 'Private'
ELSE cast (sal_clienttypeID as nvarchar(20))
END
FROM sales;
There are two problems with your query:
Msg 156, Level 15, State 1, Line 3 Incorrect syntax near the keyword 'as'.
Comes from having as followed by a string literal, this should be followed by column name:
SELECT SAL_Account_Ref AS Account_Ref
The second error is because you return two datatypes on the case - string if it equals to 4, int otherwise, you should use casting to make sure it's always the same:
SELECT SAL_Account_Ref AS Account_Ref,
CASE
WHEN SAL_ClientTypeID = 4 THEN 'Private'
ELSE cast (sal_clienttypeID as nvarchar(20))
END
FROM sales;
You can't mix datatypes in a CASE... try
SELECT SAL_Account_Ref AS 'Account Ref',
CASE
WHEN SAL_ClientTypeID = 4 THEN 'Private'
ELSE CAST(SAL_ClientTypeID, as nvarchar(20))
END
FROM sales;
There is a datatype priority in tsql. Number types have higher priority than text types. Thus, when your case includes both text like 'Private' and number types like SAL_ClientTypeID, the number overrides. When the case of 'Private' comes, tsql tries to convert it to a number and throws the error you see.
If you are OK with having the SAL_ClientTypeID as text, use one of the other answers. Otherwise, you'll have to rethink your specification.
I try to get result from Hibernate HQL query:
from StopsRegister s where s.startTime>='2018-04-25 07:59:00.0' and s.endTime<='2018-04-26 07:59:00.0'
from StopsRegister s where s.startTime between '2018-04-25 08:42:00' and '2018-04-26 08:42:00'
The issue starts when a add a secound date: and s.endTime<='2018-04-26 07:59:00.0' With only one value everything works correct.
But get the SQL Error:
The conversion of a varchar data type to a smalldatetime data type resulted in an out-of-range value.
That is the way i get the date values:
Timestamp.valueOf(LocalDateTime.of(toDatePicker.getDate(), toTimePicker.getTime()));
data types is: java.sql.Timestamp;
database: MS SQL Server.
In me opinion date format is correct, I do not understan this error.
That solve the issue:
Session session = HibernateUtil.getSessionFactory().openSession();
Query q = session.createQuery("from StopsRegister s where s.startTime >= :from and s.startTime <= :to");
q.setTimestamp("from", from);
q.setTimestamp("to", to);
stopRegisterList = (ArrayList<StopsRegister>) q.list();
Sql server query shows error :
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to numeric.
Here is the query:
WITH employee AS (
SELECT distinct vendoritemnumber,VendorItemId,
VendorItemDescription,
VendorItemDescriptionAlias,VendorId,BrgItemId,itemconversionFactor,
orderbyuomid,pricebyuomid,vendorcasedescription,manufacturernumber,skunumber,[weight],
averageweight,currentprice,taxable,[status],createddate,inactivedate,lastpurchaseddate,
lastupdatedby,lastupdateddate,vendorpercent,vendorfreight,brandid,pack,size,inventorycategoryid,
binlocation,inventorylocation,inventorystatus,physicalinventoryconversionfactor,stateswhereused,
conceptwhereused,priceupdatedate,size_uom,pack_uom,isdeleted,'' as id,'' itemid,'' as oldprice,
'' as newprice,'' as dateupdated,'' as weekenddate,'' as lastpurdate
FROM VendorItems WHERE BrgItemId=6056)
SELECT * FROM employee
UNION ALL
SELECT '' as ven,v.VendorItemId,
v.VendorItemDescription,v.VendorItemDescriptionAlias,v.VendorId,v.BrgItemId,v.itemconversionFactor,
v.orderbyuomid,v.pricebyuomid,v.vendorcasedescription,v.manufacturernumber,v.skunumber,v.[weight],
v.averageweight,v.currentprice,v.taxable,v.[status],v.createddate,v.inactivedate,v.lastpurchaseddate,
v.lastupdatedby,v.lastupdateddate,v.vendorpercent,v.vendorfreight,v.brandid,v.pack,v.size,v.inventorycategoryid,
v.binlocation,v.inventorylocation,v.inventorystatus,v.physicalinventoryconversionfactor,v.stateswhereused,
v.conceptwhereused,v.priceupdatedate,v.size_uom,v.pack_uom,v.isdeleted,ph.id,ph.itemid,ph.oldprice,
ph.newprice,ph.dateupdated,ph.weekenddate,brg.lastpurchasedate
from [dbo].[VendorItems] v join PriceHistory ph
on ph.ItemId=v.VendorItemId join brgitems brg on
brg.brgitemid=v.BrgItemId
WHERE v.BrgItemId=6056
group by vendoritemnumber,v.VendorItemId,v.VendorItemDescription,v.VendorItemDescriptionAlias,v.VendorId,v.BrgItemId,v.itemconversionFactor,
v.orderbyuomid,v.pricebyuomid,v.vendorcasedescription,v.manufacturernumber,v.skunumber,v.[weight],
v.averageweight,v.currentprice,v.taxable,v.[status],v.createddate,v.inactivedate,v.lastpurchaseddate,
v.lastupdatedby,v.lastupdateddate,v.vendorpercent,v.vendorfreight,v.brandid,v.pack,v.size,v.inventorycategoryid,
v.binlocation,v.inventorylocation,v.inventorystatus,v.physicalinventoryconversionfactor,v.stateswhereused,
v.conceptwhereused,v.priceupdatedate,v.size_uom,v.pack_uom,v.isdeleted,ph.id,ph.itemid,ph.oldprice,
ph.newprice,ph.dateupdated,ph.weekenddate,brg.lastpurchasedate
I want to show single vendoritemnumber in duplicate rows..Any suggestion??
Where is the error??
SQL Server cannot convert an empty string to NUMERIC data type. For example, the following will yield the same error:
SELECT CONVERT(NUMERIC, '')
Replace empty strings ('') with 0 or NULL.
#manoj .Just FYI to know exact error point , double click on the error in message window .It shows the exact line of code on error, Also try to check if any empty values are there for that column and as mentioned by #Serge SQL throws error while any data type is converted to numeric and if its value is empty. Best solution is to add WHERE clause and filter those cases.
I am trying to insert an value as '019393' into a table with a CHAR(10) column.
It is inserting only '19393' into the database
I am implementing this feature in a stored procedure, doing some manipulation like incrementing that number by 15 and saving it back with '0' as the prefix
I am using SQL Server database
Note: I tried CASTING that value as VARCHAR before saving to the database, but even that did not get the solution
Code
SELECT
#fromBSB = fromBSB, #toBSB = toBSB, #type = Type
FROM
[dbo].[tbl_REF_SpecialBSBRanges]
WHERE
CAST(#inputFromBSB AS INT) BETWEEN fromBSB AND toBSB
SET #RETURNVALUE = #fromBSB
IF(#fromBSB = #inputFromBSB)
BEGIN
PRINT 'Starting Number is Equal';
DELETE FROM tbl_REF_SpecialBSBRanges
WHERE Type = #type AND fromBSB = #fromBSB AND toBSB = #toBSB
INSERT INTO [tbl_REF_SpecialBSBRanges] ([Type], [fromBSB], [toBSB])
VALUES(#type, CAST('0' + #fromBSB + 1 AS CHAR), #toBSB)
INSERT INTO [tbl_REF_SpecialBSBRanges] ([Type], [fromBSB], [toBSB])
VALUES(#inputBSBName, #inputFromBSB, #inputToBSB)
END
Okay, without knowing the column datatypes, I would suggest trying this:
Change from
CAST('0'+#fromBSB+1 AS CHAR)
To
'0'+CAST(#fromBSB+1 AS CHAR(10))
But if the columns are integers this won't make a difference.