Converting 78.3 F (nvarchar) to 78.3 (numeric) - sql-server

Good afternoon, all!
Trying to get temperature off a database on my SQL server stored as "78.3 F" and make it only the number "78.3". The database has this field as nvarchar. I tried:
select
Cast(Left(Paint_Environ.Booth1_Temp, Len(Paint_Environ.Booth1_Temp) - 2) As NUMERIC(4,1)) As Temp
from Paint_Environ
but get an errors saying "Error converting data type nvarchar to numeric" Is there a better way to convert nvarchar to something numeric???
Thanks in advance!
Matt

As I mentioned in the comments, your code should work with data '78.3 F'
You may try this
SELECT
CAST(REPLACE(Booth1_Temp,'F','') AS NUMERIC(4,1)) As Temp
FROM Paint_Environ
UPDATE:
You may start debug with this to see which data format give you error
SELECT Booth1_Temp
FROM Paint_Environ
WHERE ISNUMERIC(REPLACE(Booth1_Temp,'F','')) <> 1

Related

Casting varchar to bigint in SQL Server 2016

I am trying to concat two integers together and then cast it as bigint, but SQL Server 2016 is throwing an error:
Error converting data type varchar to bigint
My table has two columns idnum int not null and innumdb not null.
In my select query, I am doing the following:
SELECT CAST(CONCAT(idnum, idnumdb) AS BIGINT)
FROM TABLE
When I execute the query, some data will be display but then while the query is displaying data, I get the error mentioned above.
I don't understand why since both idnum and idnumdb are int datatype in the table. I am assuming the concat function is messing up the data. Any ideas what's going on here? Thanks in advance
thanks everyone for helping out. i tried Udai solution of executing
SELECT CONCAT(idnum, idnumdb) FROM TABLE WHERE ISNUMERIC(CONCAT(idnum, idnumdb) ) <> 1
it turns out that idnumdb has a - sign for a few rows. i test on idnum only and that was fine. idnumdb was the problem. thanks all

Error in stored procedure but no error when running it manually

I have a stored procedure that creates a xml file. When I run it, I get an error
Conversion failed when converting the varchar value 'some text here' to int
If I put the same code in the query window in SSMS and execute it, it works fine - no error.
Can someone tell me why could that happen? The procedure is long enough to post it here, it's mainly a huge select with lots of unions and joins.
Thank you!
Somewhere in one of the columns in your join is comparing a varchar to an integer, and at least one of the varchar values is not an integer.
So try the following test:
select *
from table
where myCol NOT LIKE '%[^0-9]%'
and perhaps
select *
from table
where CONVERT (bigint, myCol) NOT BETWEEN -2147483648 AND 2147483647
Shake the tree and see what falls off.

fetching data from SQL based on a given range of dates throwing conversion error

In my application i am trying to fetch data from the database where in the records are to be fetched within a given range of dates.
Below given is the query i am using to fetch the data and the error i am getting from SQL server.
Query
select * from table_name
where RequestCreatedOn BETWEEN '31-08-2015 12:00:00.000' and '15-09-2015 12:00:00.000'
Error
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
The values in the table are as follows :
The datatype of the column is varchar.
I tried many solutions but could not find any relevant answer. Please if anybody could help me with this issue.
Thanks in Advance!
Try to use convertion before comparison:
SELECT
*
FROM table_name
WHERE RequestCreatedOn BETWEEN CONVERT(DATETIME, '31-08-2015 12:00:00.000',105)
AND CONVERT(DATETIME, '15-09-2015 12:00:00.000',105)
Your problem appears to be that your SQL Server is using US dates m/d/y. You should use the standard format yyyy-mm-dd which should always be valid.
So for your query try:
select *
from table_name
where CAST(RequestCreatedOn as DateTime) BETWEEN '2015-08-31 12:00:00.000' AND '2015-09-15 12:00:00.000'
You should also use the most appropriate data type o I would be tempted to change the datatype in your table to datetime.

MS Access / Excel SQL Server query producing Conversion error DATETIME

I've looked around for a solution for this, but none of them seem to fix my problem
SELECT TOP 10 [Appointment_Date]
FROM dbo.RF_Performance_Referrals_Main
WHERE (([Appointment_Date]) < '7/21/2014')
ORDER BY [Appointment_Date] DESC
Above is the Simplified version of the query I am trying to. I keep get an error
[Microsoft][ODBC SQL Server Driver][SQL Server]Then Conversion of a
varchar datatype to a datetime data type resulted in an out of range
value
I thought maybe there is a problem with my connection...
so I tried it on Excel and got the exact same error...
I checked the SQL Server table that particular field is set to DATETIME
So why is it giving an error?
I tried all sorts of stuff on Cast/Convert, but none of them seem to work i still get the same error. I don't really get why.
Please help if fix this.
Thanks a lot in advance
SQL Server's default date time syntax is YYYY-MM-DD HH:MM:SS as in (1900-01-01 00:00:00)
You either have to cast your string to a date or reformat your input to the expected datetime defaults:
http://msdn.microsoft.com/en-us/library/ms187819.aspx: Assuming US Local and standard defaults.
Change to
SELECT TOP 10 [Appointment_Date]
FROM dbo.RF_Performance_Referrals_Main
WHERE (([Appointment_Date]) < #2014/07/21#)
ORDER BY [Appointment_Date] DESC

Conversion failed when converting the nvarchar value 'quiz.jpg' to data type int

I am getting this error in my logs and I really do not know how to solve it.
"Conversion failed when converting the nvarchar value 'quiz.jpg' to data type int."
I am currently using ColdFusion 9. It seems that for whatever reason the site is trying to parse an image file into the database or something like that.
Any help will be appreciated.
Thanks
Are you using <cfqueryparam>?
I would guess that you may have something like this:
<cfquery param value="#myVar#" cfsqltype="cf_sqltype_integer" />
the cfsqltype should be cf_sqltype_varchar.
If that is not the case, then please update your post with the offending code. That would be very helpful.
I think we would need more of the error details like the line of code on which the error occurs or maybe the whole template to be much help. In the absence of more information, Jason's answer seems likely.
This sounds to me like you may have a query where you are trying to concatenate a nvarchar column with an int column.
Such as
SELECT MyString + MyInt FROM MyTable
Unfortunatly SQL Server does not implicitly convert integers to varchar for concatenation, so you will need to convert your integer.
SELECT MyString + Convert(varchar(20), MyInt) FROM MyTable
or
SELECT MyString + Cast(MyInt AS varchar(20)) FROM MyTable

Resources