I need to convert string for example '0.600 KG' to real. I have tried convert and cast but it generate error message. The example is as bellow.
select cast('0.600 KG' as real)
Error converting data type varchar to real.
Please Help to fix the problem.
Assuming the units always appear at the end of the string, you could try taking an appropriate substring, e.g.
DECLARE #mass VARCHAR(50);
SET #mass = '0.600 KG';
SELECT CAST(SUBSTRING(#mass, 1, CHARINDEX(' ', #mass) - 1) AS real);
Of course, the notion of units would then be lost, and you would just have a real number.
Demo
Related
I have this stored procedure:
Dbo.SprocName (#Id UNIQUEIDENTIFIER,
#ResponseCode INT OUTPUT,
#ResponseDescription VARCHAR(500) OUTPUT)
And it returns a dataset called say Result as a nvarchar(MAX) (always a single row).
I've tried OLE and ADO connections and as well as result sets. I've tried creating a table variable and storing the value there.
Nothing works.
I can see in the database that it's running successfully then it fails when returning the result data set.
I’ve done some debugging and I can assure the result string is returned as should be. The problem is that I don’t know how to handle this on SSIS.
The error that I get is:
Input string was not in a correct format
I appreciate any ideas.
Thanks.
EDIT: I have tried using a table variable again and it works. I guess I didn't do it well first time. sorry about that. Thanks!
One potential cause for your problem could be a mismatch in data types between SSIS and SQL Server.
An SSIS GUID data type does not match a SQL Server uniqueidentifier - the SSIS GUID has curly braces (e.g., {00000000-0000-0000-0000-000000000000}), while the SQL value does not. SQL cannot recognize the value as a unique identifier, and fails to convert.
To pass down a GUID, you will need to remove those curly braces, either in SSIS or in SQL. One approach I've used it to send it across as a VARCHAR and then strip out the curly braces, e.g.,
DECLARE #GUID VARCHAR(40) = '{00000000-0000-0000-0000-000000000000}'
DECLARE #CnvtGUID UNIQUEIDENTIFIER = REPLACE(REPLACE(#GUID, '}', ''), '{', '')
SELECT #GUID, #CnvtGUID
I need help. I keep getting a "Conversion failed when converting date and/or time from character string" error right before the INSERT statement. I can see that the problem stems from either the [nowtime] or the [a_Timestamp] because of the datatypes, and I have to convert them to the same type. However somehow I can't get it to work no matter what I try. In the background [a_Timestamp] is CHAR(13) and #nowtime is CONVERT(time,CONVERT(char(8), GETDATE(), 108)).
The purpose of the full query is to monitor a DBs past 1, 5 and 10 seconds of data, so the time operations are vital and must be quick. If you need more of the code (or all of it) im happy to provide!
CREATE TABLE #base
(
[date] CHAR(8),
[a_MemberId] CHAR(5),
[a_Timestamp]CHAR(8),
[nowTime] TIME
)
INSERT INTO #base
([date],
[a_MemberId],
[a_Timestamp],
[nowTime])
SELECT [date] AS [date],
[a_MemberId] AS [a_MemberId],
SUBSTRING([a_Timestamp],0,7) AS [timeStamp],
#nowTime AS [nowTime]
FROM [ObserverDB].[dbo].[onti_ord] WITH (NOLOCK)
WHERE [date] = #TodaysDateTEST AND [a_Timestamp] < #nowTime
ORDER BY [date]
This is probably culture related. Your string is converted implicitly and your culture does not fit to the stored format. It is always a bad idea to store date and/or time values as text.
There is only one sure format: ISO8601 which means (one of many examples)
yyyy-mm-ddThh:mm:ss.ttt (e.g. 2016-08-17T13:11:23)
In this line you are comparing your values. Obviously one of them is treated as string and one as date-time value
WHERE [date] = #TodaysDateTEST AND [a_Timestamp] < #nowTime
You must make sure (by using CONVERT (details here) with the proper format number), that a date and/or time value is converted to string exactly to the same format as your strings stored (sargable, therefore faster), or to convert your stored values to real date and/or time values and compare them type safe (much cleaner).
Of course the advise should be: Change the database to store this propperly, but - as you stated in comment - you have to deal with this...
Good luck :-)
I'm attempting to create a function with 2 params.
1st param is the date
2nd is an int
What i want the function to do:
Function('2014-04-20', 5) results in '2013-11-20'
Function('2013-11-10', 2) results in '2013-09-10'
CREATE FUNCTION MonthDiff(
#in_date DATE
,#in_mn_adjust INT)
RETURNS DATE
AS
BEGIN
DECLARE #in_new DATE;
SET #in_new = DATEADD(MM, -#in_mn_adjust, #in_date);
RETURN #in_new;
END;
I ran 'select dbo.monthdifference('2014-04-22', 3). The error that I got is Conversion failed when converting date and/or time from character string.
What am I doing wrong?
Use below code to solve your problem.
Declare #str Varchar(10)
Declare #result Varchar(10)
set #str= '2012/10/23'
set #result = DATEADD(MONTH,-2,CONVERT(date,#str,102))
Sometimes SQL Server does silly things and misinterprets some date strings1. The following formats are always safe. I believe there are some other formats that are also safe when dealing with the newer date, time and datetime2 types but I tend to play it safe:
YYYYMMDD
YYYY-MM-DD'T'hh:mm:ss
YYYY-MM-DD'T'hh:mm:ss.mil
So try:
select dbo.monthdifference('20140422', 3)
(The 'T' in my example formats is the literal character T, being used as a separator, instead of a space, and instead of the other characters in those format strings which are placeholders, so e.g. 2014-04-24T09:23:00 is in one of my listed safe formats)
1For example, if your language settings are British it interprets nnnn-nn-nn, where all ns are digits, as a YYYY-DD-MM format - despite the fact that I doubt that anyone in Britain would attempt such an interpretation.
I am currently trying to convert an int field into a Uniqueidentifier, doing so by first converting the int into a (DT_WSTR, 100)
(Note: i have also tried (DT_STR) )
i have tried both the Derived Column and the Convert Data Transformations, but i always get an error when converting the then String to a GUID.
Here is what I have Already Tried:
NameOfDer.C Expression Data Type
1)ID_String (DT_WSTR, 100)ID_Int Unicode character string
-Below is where the error occurs-
2) GUIDTESTEST (DT_GUID)("{" + [ID_String] + "}") UniqueIdentifier
i have also tried it like this: (DT_GUID) [ID_String] (-Without the curly braces), but it failed just as bad.
Does anyone have an idead of what is Happening, i have also looked the data up on DataViewer after they are converted to Strings, and they all seem fine.
In SSIS you can't convert integer to GUID.
GUID has format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. So integer 5 can't be fit in that format.
Some guy did it with sql server, but if this don't gonna loose information?
The best thing what you can do, just generate real GUID for your rows.
This is really simple. Does SQL SERVER 2008 auto convert values to string for you ?
I tried this Select * from Staff Where Division = 5
If I try to insert it, it works too.
If I change 5 for five I get and error though. Invalid column name five.
Division is a NVARCHAR. Shoudn't the 5 be within single quotes ?
Yes, SQL Server does an implicit conversion of int to nvarchar. It can also implicitly convert nvarchar to int (and other numeric types).
For more details: http://msdn.microsoft.com/en-us/library/ms191530.aspx
There are a lot of cases where implicit conversion takes place. For example:
EXEC sp_who2 active;
Shouldn't you need to delimit active as a string? Yes, but the syntax is forgiving.
My suggestion: always delimit data types correctly, and ignore the exceptions. The chances they'll ever get comprehensively fixed are close to nil.