CONVERT varchar to money
Can someone help me with the syntax on how to convert data
type varchar to data type money. I'm receiving the following error
message:
[Macromedia][SQLServer JDBC Driver][SQLServer]Disallowed
implicit conversion from data type varchar to data type money,
table 'tableName', column 'columnName'. Use the CONVERT function to
run this query.
Any help would be much appreciated! Thanks.
SLL
Within my main page I call all the data from a particular
table along with a link to edit that particular record. Clicking
edit takes you to the "edit.cfm page".
The edit.cfm page displays the data from the particular
record you clicked.
When you make changes (or dont make changes) you can update
the record by clicking a button that takes you to a processor page.
That runs the following code:
<!-- Edit or Update -->
<cfif IsDefined("form.ID")>
<!--Update-->
<cfquery datasource="db_name">
UPDATE dbo.tblName
SET Type='#form.Type#',
Price='#form.Price#',
Category='#form.Category#'
WHERE ID=#form.ID#
</cfquery>
</cfif>
The error that I receive is:
[Macromedia][SQLServer JDBC Driver][SQLServer]Disallowed
implicit conversion from data type varchar to data type money,
table 'tblName', column 'Price'. Use the CONVERT function to run
this query.
Similar Messages
-
I have created the following trigger that fires instead of an UPDATE statement and copies the altered data over to an Audit table before proceeding with the requested UPDATE. After doing so, I wrote a simple UPDATE statement for testing:
UPDATE Products
SET ProductCode = 'XYZ 2014', ProductName = 'Special Edition Tamborine', ListPrice = 74.99, DiscountPercent = .5
WHERE ProductID = 28;
When I run this, I get the following message:
Msg 245, Level 16, State 1, Procedure Products_UPDATE, Line 14
Conversion failed when converting the varchar value 'X' to data type int.
Here is my trigger:
IF OBJECT_ID('Products_UPDATE') IS NOT NULL
DROP TRIGGER Products_UPDATE; -- LINE 14
GO
CREATE TRIGGER Products_UPDATE
ON Products
INSTEAD OF UPDATE
AS
DECLARE @ProductID int, @CategoryID int, @ProductCode varchar, @ProductName varchar,
@Description varchar, @ListPrice money, @DiscountPercent money, @DateUpdated datetime
BEGIN
SELECT @ProductID = ProductID, @CategoryID = CategoryID, @ProductCode = ProductCode,
@ProductName = ProductName, @Description = Description, @ListPrice = ListPrice,
@DiscountPercent = DiscountPercent, @DateUpdated = GetDate()
FROM deleted;
INSERT INTO ProductsAudit
VALUES (@ProductCode, @CategoryID, @ProductCode, @ProductName, @ListPrice,
@DiscountPercent, @DateUpdated);
SELECT @ProductID = ProductID, @CategoryID = CategoryID, @ProductCode = ProductCode,
@ProductName = ProductName, @Description = Description, @ListPrice = ListPrice,
@DiscountPercent = DiscountPercent, @DateUpdated = GetDate()
FROM inserted;
UPDATE Products
SET CategoryID = @CategoryID, ProductCode = @ProductCode,
ProductName = @ProductName, Description = @Description,
ListPrice = @ListPrice, DiscountPercent = @DiscountPercent
WHERE ProductID = (SELECT ProductID FROM inserted)
END
I am confused a bit by (1) the location of the error and (2) the reason. I have looked over my defined data types and they all match.
Could someone offer a second set of eyes on the above and perhaps guide me towards a solution?
Thank you.This is an issue in this trigger. It will work for single record updates alone. Also for your requirement you dont need a INSTEAD OF TRIGGER at all.
You just need this
IF OBJECT_ID('Products_UPDATE') IS NOT NULL
DROP TRIGGER Products_UPDATE;
GO
CREATE TRIGGER Products_UPDATE
ON Products
AFTER UPDATE
AS
BEGIN
INSERT INTO ProductsAudit
SELECT ProductID,
CategoryID,
ProductCode,
ProductName,
ListPrice,
DiscountPercent,
GetDate()
FROM deleted;
END
Now, in your original posted code there was a typo ie you repeated the column ProductCode twice
I think one should be ProductID which is why you got error due to data mismatch (ProductID is int and ProductName is varchar)
see below modified version of your original code
IF OBJECT_ID('Products_UPDATE') IS NOT NULL
DROP TRIGGER Products_UPDATE; -- LINE 14
GO
CREATE TRIGGER Products_UPDATE
ON Products
INSTEAD OF UPDATE
AS
DECLARE @ProductID int, @CategoryID int, @ProductCode varchar, @ProductName varchar,
@Description varchar, @ListPrice money, @DiscountPercent money, @DateUpdated datetime
BEGIN
SELECT @ProductID = ProductID, @CategoryID = CategoryID, @ProductCode = ProductCode,
@ProductName = ProductName, @Description = Description, @ListPrice = ListPrice,
@DiscountPercent = DiscountPercent, @DateUpdated = GetDate()
FROM deleted;
INSERT INTO ProductsAudit
VALUES (@ProductID, @CategoryID, @ProductCode, @ProductName, @ListPrice,
@DiscountPercent, @DateUpdated);
SELECT @ProductID = ProductID, @CategoryID = CategoryID, @ProductCode = ProductCode,
@ProductName = ProductName, @Description = Description, @ListPrice = ListPrice,
@DiscountPercent = DiscountPercent, @DateUpdated = GetDate()
FROM inserted;
UPDATE Products
SET CategoryID = @CategoryID, ProductCode = @ProductCode,
ProductName = @ProductName, Description = @Description,
ListPrice = @ListPrice, DiscountPercent = @DiscountPercent
WHERE ProductID = (SELECT ProductID FROM inserted)
END
I would prefer doing it as former way though
Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs -
I have 13 dollars left on an apple store gift card, how can I convert this into money for my iTunes account?
You can't.
-
How to convert varchar to date datatype while insert or update in table
Hai All
I need to convert to varchar to date.
I have two Tables T1,T2
T1 Structure
Code varchar
Time varchar
Date varchar
T2 Structure
Empname var
Empcode var
Intime date
Outtime date
Intrin date
Introut date
Att_date
Now i need to move Time form T1 to T2 Intime,outtime,intrin,introut according some condition
So now i need to convert Varchar to Date while insert or update
I have tried something
Insert into T1 (code,intime,att_date)values
(code,To_date(Date||time,'dd-mon-yyyy hh24mi'),att_date);
OR While update
Update T2 set Outtime=To_date(Date||time,'dd-mon-yyyy hh24mi') where...
I got an error Ora-01861
Regards
Srikkanth.MYou didn't show any example of your date or time values, butyou might need to add a space between them, like
To_date(Date || ' ' || time,'dd-mon-yyyy hh24mi') -
Convert varchar(20) date to date that looks like mm/dd/yyyy
Good afternoon,
I am a total novice at SQL and have been tasked to change a varchar(20) date column (shows up as m/dd/yy) to show up as mm/dd/yyyy. I've looked at the previous posts, but being a novice, it's difficult to wade through the non-applicable stuff. The column
that needs to be changed is field2, but I'm not sure how to construct a statement to reflect my objective.
Thank you in advance.The sample uses a table variable. You can program it the same way for a table:
DECLARE @t TABLE (dt VARCHAR(20))
INSERT INTO @t
VALUES ('1/2/12')
,('3/10/10')
,('7/23/9')
,('aa')
,('12/31/99')
,('10/20/00')
,('10/20/01')
SET DATEFORMAT mdy
SELECT dt
,CONVERT(VARCHAR(10), CASE
WHEN isdate(dt) = 0
THEN NULL
ELSE cast(dt AS DATE)
END, 101) AS newDt
FROM @t;/******** TRY_CONVERT() method ************/
SELECT dt, newDt=CONVERT(varchar(10),TRY_CONVERT(date,dt),101) FROM @t;
GO
dt newDt
1/2/12 01/02/2012
3/10/10 03/10/2010
7/23/9 07/23/2009
aa NULL
12/31/99 12/31/1999
10/20/00 10/20/2000
10/20/01 10/20/2001
dt newDt
1/2/12 01/02/2012
3/10/10 03/10/2010
7/23/9 07/23/2009
aa NULL
12/31/99 12/31/1999
10/20/00 10/20/2000
10/20/01 10/20/2001
Starting with SQL Server 2012 you can use TRY_CONVERT().
You can start studying datetime functions here:
http://www.sqlusa.com/bestpractices/datetimeconversion/
Kalman Toth Database & OLAP Architect
T-SQL Scripts at sqlusa.com
New Book / Kindle: Exam 70-461 Bootcamp: Querying Microsoft SQL Server 2012 -
Convert varchar(255) to varchar(50)
I want to transfer data from a staging table to another table. The staging table has columns with datatypes I don't always want, and I'm converting them like so:
INSERT INTO [dbo].[my_table]
SELECT CONVERT(datetime,TimeIndex,103),
CONVERT(decimal(6,3),Network_In,3),
CONVERT(decimal(6,3),Network_Out,3),
CONVERT(decimal(6,3),Network_Total,3)
FROM [dbo].[staging_table]
How would I convert varchar(255) to varchar(50)?
Thanks in advance
AdamUsing substring is returning the column so thanks but now I can't convert datetime for some reason:
INSERT INTO [dbo].[mytable]
SELECT SUBSTRING(ClusterName,1,50) AS 'ClusterName',
CONVERT(datetime,TimeIndex,103),
ClusterID,
CONVERT(decimal(3,2),Memory,3),
CONVERT(decimal(3,2),CPU,3),
CONVERT(int,CPUTotal),
CONVERT(int,MemoryTotal)
FROM [dbo].[stagingtable]
GO
Now I get an error I didn't get before:
Implicit conversion from data type datetime to decimal is not allowed. Use the CONVERT function to run this query.
But if I run the datetime convert on it's own as a single column select then it works okay. Any ideas where I've gone wrong there?
To sum up - the above fails but the below works:
SELECT CONVERT(datetime,TimeIndex,103) AS 'TimeIndex'
FROM [dbo].[stagingtable]
Thanks
Adam -
Replace and convert varchar to decimal
Hi all
What could be the easiest way to replace and convert varchar(50) to decimal
My string '21 807,00' Need to have it in db as 21807.00
Thanks
AKEOn any version you can use somple convert with replace:
declare @MyStr varchar(10) = '21 807,00'
SELECT CONVERT(decimal(10,2), REPLACE(REPLACE(@MyStr, ',','.'),' ',''))
but if there is a different format and the convert will not work, then you will get error. Therefore it is best (as mentioned) to use TRY_CONVERT on newer server or use Try block on old servers
[Personal Site] [Blog] [Facebook] -
Converting varchar to datetime 103
Hi Guys
Im making a select query where in a colomn the date is yyyy-mm-dd, i need dd-mm-yyyy (103). A simple convert query does not work because the colomn is not set as a datetime but as a varchar. Its not possible to change the tables at the source.
The colomn name is DATE_IN
I think the following post could be helpfull but i dont know how to fix the given statements into the select query
http://stackoverflow.com/questions/1509977/convert-varchar-into-datetime-in-sql-server> in a colomn the date is yyyy-mm-dd, i need dd-mm-yyyy (103)
As suggested above, this is string date format to string date format conversion, purely string operations.
If you convert to DATE / DATETIME first (universal internal binary representation), then it may fail if invalid date.
You can use the ISDATE or TRY_CONVERT functions to check if the string date is valid.
DATETIME functions:
http://www.sqlusa.com/bestpractices/datetimeconversion/
Kalman Toth Database & OLAP Architect
SQL Server 2014 Design & Programming
New Book / Kindle: Exam 70-461 Bootcamp: Querying Microsoft SQL Server 2012 -
How to convert varchar to int in MSSQL?
Hi,
I am using mssql. how can i convert varchar to int type.
for eg.
id (int) txt(varchar)
1 ---------10
2 ---------10a
3 ---------10b
i want to convert varchar to int. HOw? pls explain with query.
Thanks
edward"10a" wouldn't normally be considered a number. So what sort of number do you expect it to be?
Other than that you can look at the substring and convert/cast functions. -
I need my code to add the varchar CPUTM field + varchar CPUZIPTIM field which both has time values to see if greater than 2 hours. How do I do this when both fields are varchar. The value in CPUTM field is
335:55:20.97 duration time. My code is below.
CPUTM = 335:55:20.97 duration time
CPUZIPTM = 0:00:01.96
duration time
select * FROM [SMF_DATA].[dbo].[System_Management_Facility]
WHERE ((convert(varchar(13), CONVERT(time, CPUTM) + CONVERT(time, CPUZIPTM))) > '02:00:00.00')Well, there is no time span in T-SQL, so you need either to store it in the smallest necessary unit (seconds, milliseconds) or to convert ít on the fly.
E.g.
DECLARE @CPUTM VARCHAR(255) = '335:55:20.97';
DECLARE @CPUZIPTM VARCHAR(255) = '00:00:01.96';
DECLARE @T BIGINT = PARSENAME(REPLACE(@CPUTM, ':', '.'), 1) +
PARSENAME(REPLACE(@CPUTM, ':', '.'), 2) * 100 +
PARSENAME(REPLACE(@CPUTM, ':', '.'), 3) * 6000 +
PARSENAME(REPLACE(@CPUTM, ':', '.'), 4) * 360000 +
PARSENAME(REPLACE(@CPUZIPTM, ':', '.'), 1) +
PARSENAME(REPLACE(@CPUZIPTM, ':', '.'), 2) * 100 +
PARSENAME(REPLACE(@CPUZIPTM, ':', '.'), 3) * 6000 +
PARSENAME(REPLACE(@CPUZIPTM, ':', '.'), 4) * 360000;
SELECT @T / 360000,
(@T % 360000) / 6000,
(@T % 6000) / 100,
(@T % 100); -
Convert varchar to decimal - arithmetic overflow
I'm using SQL Server 2014 and I'm trying to convert data from a staging table over to a production table. I seem to be getting an Arithmetic overflow error converting varchar to numeric on the decimal conversion. I'm sure I've overlooked something with the
syntax of the CONVERT.
This is the staging table:
CREATE TABLE [dbo].[staging_table](
[TimeIndex] [varchar](100) NULL,
[Cluster] [varchar](100) NULL,
[AvgMem] [varchar](100) NULL,
[AvgCPU] [varchar](100) NULL,
[TotalMemory] [varchar](100) NULL,
[TotalCPU] [varchar](100) NULL,
[Datacenter] [varchar](100) NULL,
[vCenter] [varchar](100) NULL
) ON [PRIMARY]
This is the prod table I'm moving it to:
CREATE TABLE [dbo].[Clusters](
[ClusterID] [int] IDENTITY(1,1) NOT NULL,
[ClusterName] [varchar](25) NULL,
[DatacenterName] [varchar](25) NULL,
[TimeIndex] [datetime] NULL,
[AvgCPU] [decimal](5, 2) NULL,
[AvgMem] [decimal](5, 2) NULL,
[TotalCPU] [decimal](8, 2) NULL,
[TotalMem] [decimal](8, 2) NULL,
[vCenterID] [int] NULL,
CONSTRAINT [PK_Clusters_1] PRIMARY KEY CLUSTERED
[ClusterID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
and here's an example INSERT INTO statement throwing the error:
INSERT INTO [dbo].[Clusters] (ClusterName,DatacenterName,TimeIndex,AvgCPU,AvgMem,TotalCPU,TotalMem,vCenterID)
SELECT SUBSTRING(Cluster,1,25) AS ClusterName,
SUBSTRING(Datacenter,1,25) AS DatacenterName,
CONVERT(datetime,TimeIndex,103) AS TimeIndex,
CONVERT(decimal(5,2),AvgCPU) AS AvgCPU,
CONVERT(decimal(5,2),AvgMem) AS AvgMem,
CONVERT(decimal(8,2),TotalCPU) AS TotalCPU,
CONVERT(decimal(8,2),TotalMemory) AS TotalMem,
'3' FROM [dbo].[staging_table]
Sample data is 0.00 to 100.00 in fields AvgCPU and AvgMem, and TotalCPU and TotalMem usually goes up to about 7 digits with no decimal (eg. 7543253) but could be 8 and although I've never seen a decimal I wouldn't rule it out so decided to account for it.
I assume it's something I've overlooked with the syntax but any ideas would help.
Thanks
AdamThe problem is your precision and scale you are assigning to your decimals.
decimal(5,2) = this is a total of 5 digits, 3 digits for the whole number and 2 for the fractional.
decimal(8,2) = this is a total of 8 digits, 6 digits for the whole number and 2 for the fractional.
So converting a varchar of 7 or 8 digits for TotalCPU or TotalMem will give you an error because your definition will actually only allow for 6 digits of storage. You could test this by doing decimal(8,0) or decimal(10,2) both which will allow for up to
8 whole numbers.
If you are worried about space Sql Server will allocate a set number of bytes for ranges based on the precision (first number in the parenthesis). See this page which explains in detail how much space each range takes up and also further details on
decimal and numerics.
-Igor -
Hi
im trying to convert varchar to date.
So, i used To_date ( cilumn, 'DD-MM-YYYY') , Since i have to order the out put in Decending Order i used the same convertion function with Order by clause.
The problem is ... the out put is not ordered,
<sql>
select date_created
from
select distinct
to_date(created_date,'DD-MM-YYYY') as date_created
from schedule_backup
order by date_created desc
<sql>
out put is
=================
DATE_CREA
26-NOV-09
23-NOV-09
19-NOV-09
16-NOV-09
11-NOV-09
04-NOV-09
30-OCT-09
11-SEP-09
04-MAR-10
01-MAR-10
25-FEB-10
DATE_CREA
22-FEB-10
17-FEB-10
15-FEB-10
11-FEB-10
09-FEB-10
03-FEB-10
27-JAN-10
20-JAN-10
15-JAN-10
04-JAN-10
thanks
rajHi, Raj,
When you store dates in a VARCHAR2 column, you're asking for touble, so it seems that everything is working according to plan.
Even aside from that, your query works perfectly for me.
In the output you posted, the order is correct within each year, and you're only showing 2 digits of the year. Are you sure the centuries are right in the schedule_backup table? Perhaps everything that was supposed to be '2010' got enetereed as '1910' or '0010'.
Post some sample data (CREATE TABLE and INSERT statements) that gets the wrong results with your query.
For example:
CREATE TABLE schedule_backup
( created_date VARCHAR2 (10)
INSERT INTO schedule_backup (created_date) VALUES ('11-11-2009');
INSERT INTO schedule_backup (created_date) VALUES ('04-11-2009');
INSERT INTO schedule_backup (created_date) VALUES ('30-10-2009');
INSERT INTO schedule_backup (created_date) VALUES ('11-09-2009');
INSERT INTO schedule_backup (created_date) VALUES ('04-03-2010');
COMMIT; -
Convert VARCHAR to date - don't know format in advance
Hi,
If I want to convert varchar to date, but I don't kniow the format of the varchar in advance. For example, it may be '20-MAY-99', '2009-01-10 10:01:00', but it is not known in advance. How can I convert it to date?
Many thanksYou'll have to define a hierarchy of date formats to try. There are some strings that map to many different valid dates depending on the format. The string '01/02/03' might translate to January 2, 2003, January 2, 1903, February 1, 2003, February 1, 1903, February 3, 2001, February 3, 1901, etc.
Assuming you define a hierarchy of formats, you can write a function that tries each format in turn, i.e.
CREATE FUNCTION my_to_date( p_string IN VARCHAR2 )
RETURN DATE
IS
l_date_variable DATE;
BEGIN
l_date_variable := to_date( p_string, <<format 1>> );
return l_date_variable;
EXCEPTION
WHEN OTHERS THEN
BEGIN
l_date_variable := to_date( p_string, <<format 2>> );
return l_date_variable;
EXCEPTION
WHEN OTHERS THEN
END;You'll need to define the N date formats to try and the order in which to try them.
Justin -
Convert varchar to date in OBIEE-sql server database
Hi we have SQL server database,
and we need to convert varchar to to_date .
In OBIEE we use evaluate,cast etc on Oracle......Not sure about sql server..please helpi am usiing below formula in my reports-answer side:
Cast((case when "Job CV Fact"."Total Cv Sent" is not null then "DATE DIM".Date end)as Date)
below error is coming:
A general error has occurred. [nQSError: 46046] Datetime value 01/01/08 does not match the specified format. (HY000)
cast("DATE DIM".Date as date) the above error comes :) -
In OBIEE rpd convert varchar to datetime
Hello,
Can anyone help one how can we convert a column that is set to varchar to datetime in obiee;
Suppose COLUMN_A is varchar and keeps value like '2008-12-30 10:11:22'
and we want to convert it DATETIME.
Thanks and Regardsuse cast(column as TIMESTAMP)
Maybe you are looking for
-
Error while deploying a webdynpro application
Hi, Iam getting an error while deploying the application in nwds the error is like this com.sap.tc.webdynpro.services.exceptions.WDRuntimeException: Failed to get deployable object part info for component com.labprocess.LabProcess1 what is the remedy
-
Steps to create a query in development
Could anyone tell me as how to create a query ? Nishi
-
This is very odd. Most files that get saved onto my Desktop folder by applications simply doesn't appear there - not on the desktop, not in the desktop folder in the finder, nowhere. But the file's there, 'cos in the terminal 'ls' can see it fine. So
-
Every time I've tried to log into iCloud this afternoon and then selected "Mail," the system logs me out right away. The other iCloud applications work perfectly well. Only Mail seems to be working weirdly. Anyone else experiencing this/know why this
-
HP L7780 Freezes up after new wireless network
This printer was working fine as a copier/fax for several months until we decided to add as wireless printer. After setting up the wireless network, the message appears that it was connected successfully. Then it freezes. No button does anything. Unp