SIMPLE Command To Convert String To Number? Not CAST Or CONVERT.

Aug 15, 2006

Dear Experts,

Ok, I hate to ask such a seemingly dumb question, but I've
already spent far too much time on this. More that I
would care to admit.

In Sql server, how do I simply change a character into a number??????

In Oracle, it is:

select to_number(20.55)
from dual

TO_NUMBER(20.55)
----------------
20.55

And we are on with our lives.


In sql server, using the Northwinds database:

SELECT
r.regionid,
STR(r.regionid,7,2) as a_string,
CONVERT(numeric, STR(r.regionid,7,2)) as a_number,
cast ( STR(r.regionid) as int ) as cast_to_number
FROM REGION R

1 1.0011
2 2.0022
3 3.0033
4 4.0044




SELECT
r.regionid,
STR(r.regionid,7,2) as a_string,
CONVERT(numeric, STR(r.regionid,7,2) ) as a_number,
cast (STR(r.regionid,7,2) as numeric ) as cast_to_number
FROM REGION R

1 1.0011
2 2.0022
3 3.0033
4 4.0044



Str converts from number to string in one motion.

Isn't there a simple function in Sql Server to convert
from string to number?

What is the secret?

Thanks

View 4 Replies


ADVERTISEMENT

T-SQL (SS2K8) :: Using CAST Or CONVERT To Change Data From String To Datetime?

Apr 16, 2014

i am trying to take a field that has part of a date in it, so I have to parse it out as follows:

SUBSTRING(a1.Field1, 3, 2) + SUBSTRING(a1.Field1,5,2) + '20' + LEFT(a1.Field1,2)

This is because a date of 04/16/2014 will show as 160416 in the first part of the field I need to parse it out of, thus becoming 04162014.

From there I then need to convert this "date" into a legitimate SQL datetime type, so that I can then run a DATEDIFF to compare it to when the record was actually entered, which is a separate field in the table, and already in datetime format.

When I use the below statement, I am getting the message that, "Conversion failed when converting date and/or time from character string."

CAST((SUBSTRING(a1.Field1, 3, 2) + SUBSTRING(a1.Field1,5,2) + '20' + LEFT(a1.Field1,2)) as datetime)

I also tried CONVERT(datetime, (SUBSTRING(a1.Field1, 3, 2) + SUBSTRING(a1.Field1,5,2) + '20' + LEFT(a1.Field1,2)), and got the same message.

how I can parse that field, then convert it to a datetime format for running a DATEDIFF statement?

View 9 Replies View Related

How To Convert A Number To String In SQL?

Jul 7, 2006

I search the help for T-sql, still don't know how to convert a number, for example 12345 to a string with formating like 12,345
Thanks for your help.

View 1 Replies View Related

How To Convert String To Number

Dec 6, 2014

I have a column which keeps numeric data in form of String, but I want to convert it to number e.g. from "1001" to 1001,

Which function I can use to do it?

[URL] .....

View 2 Replies View Related

How To Convert A Bit String To 0x Number?

Jul 19, 2006

hi,

I have a bit string like '000111010101101110000111' , how to convert it to varbinary format like 0x1D5B83?

Thanks.

View 1 Replies View Related

How To Convert Number Into Fixed Format String.

Sep 14, 2007

I would like to convert a number into fixed format string. Say for example if I have number 5, I would like to show it as four charactered string:

0005.

In case if I have 33, then I would like to have a result like '0033'. Please let me know the string functions with which I can acheive this.

View 5 Replies View Related

Concat With A Loop - Convert Number Into String Character

Apr 5, 2014

I'm new to SQL and I've been trying this for a while now.

I have let's say a loop of numbers from one to ten. It appears like this:

1
2
3
4
5

What I want to do is to have it appear on one column like this: 12345, the problem is I can't seem to figure out if I need to only have one variable or I'm missing something else, I figured you need to convert them into a string character but I'm still unable to do it.

View 7 Replies View Related

Cast/Convert Mmddyy In String To New DB_DATETIMESTAMP Column In Derived Column Transformation

Mar 5, 2007

Hi,
I have dates in "mmddyy" format coming from the sources and they are older dates of mid 80s like 082580 for instance.

When I cast it this way (DT_DBTIMESTAMP) Source_Date , It says ok but throws a runtime error.

When I hardcode a date in same format, (DT_DBTIMESTAMP) "082580" , It becomes red (an indication of syntax error) . Please note that we use double quotes in expressions in Derived Column Transformation; So an anticipation that using double quotes over single ones would be the syntax problem would be wrong.



Any help in this will sincerely be appreciated.


Thanks

View 7 Replies View Related

Cast Or Convert

Feb 18, 2008

 Hi,I want to turn int to double/decimal  in microsoft sqlSHould i use cast or convert?if so, how i do it thanks, 

View 1 Replies View Related

CAST/Convert

Mar 15, 2006

I need Query syntax to cast/convert values as follws.

Val.: 00005000010260002180 - Result must be: 5.1.2600.2180
Val.: 00005000000213400001 - Reslut must be : 5.0.2134.1

Dots must also be contained in result

View 2 Replies View Related

CAST Or Convert

Oct 8, 2014

Have the following in my SELECT statement, which I'm having issues with when I modify it to include a CAST or a CONVERT.

CONCAT(PER.[PERSON-REF],ROW_NUMBER() OVER (PARTITION BY PER.[PERSON-REF] ORDER BY TEN.[tenancy-ref])) AS 'ID'How do I convert or cast it to a varchar (20)?

View 8 Replies View Related

Convert AND Cast

Nov 6, 2007

We have a 3rd party application that connects to our SQL 2005 as a external database source.

Our external database has a 'datein' field that for some reason is stored as a varchar(50).

I can use cast to convert the field to datetime, but how would I then format to the yyyy-mm-dd format in the same select statement(using in view)?

I need to convert this field to a format of YYYY-MM-DD in order for the 3rd party application to search on it correctly.
Below is an example of the data --
FieldName = DateIn
DateType = Varchar(50)
2006-07-06
Sep 12 2007 11:16AM
2007-07-19
Oct 2 2007 12:24PM
2007-08-19
Oct 31 2007 8:00PM
Sep 15 2007 9:25AM
2007-08-18
Oct 16 2007 7:01PM
Oct 30 2007 11:15AM
Aug 29 2007 8:54AM
Aug 29 2007 8:38PM
Aug 25 2007 2:33PM

View 3 Replies View Related

Difference Between Cast And Convert In SQL

Apr 20, 2007

Can anyone explain in simple terms the difference between cast and convert in SQL?

View 2 Replies View Related

Cast/Convert Value To VB Variable

Oct 24, 2005

Hi all,
I have a VB 6.0 application that interacts with an MS Access backend. I am in the process of converting it so that it interacts with SQL Server 2000. The current applications uses Cint, CStr, etc. functions which are MS-Access specific, so I am now switching over to Convert (or Cast) function. The problem is that there are certain places wherein I first need to store the value of the resultant CAST/Convert function in a variable and then use that value in a SQL statement. However, I cant seem to figure out a way of storing the results from CAST/CONVERT functions in the VB 6.0 variable. All the examples on Internet show use of these functions directly in an SQL statement e.g. "Select CAST(title as Int) from xyz", etc.
Can anybody tell me how can i get the values to be stored in the variable? I am really stuck here and cant seem to progress.

Thanks in advance for all your help.

Regards:
Prathmesh

View 2 Replies View Related

Update With Different CAST And CONVERT

Jul 6, 2014

I have a table with all varchar data for all different fields for money and int etc. I want to do a calcuation in one field with the rule as below:

UPDATE [dbo].[tblPayments]
SET [PreInjuryWage]=
CASE
WHEN [WeekNo]<14
THEN ([MaxWorkCover]+ [WeeklyEarnings] )/0.95
ELSE ([MaxWorkCover] + 0.80 * [WeeklyEarnings] ) /0.80
END

I tried this command after many other ways of tries

UPDATE [dbo].[tblPayments1]
SET [PreInjuryWage]=
CASE
WHEN [WeekNo]<14
THEN CAST ( (cast([MaxWorkCover] as money )+ cast([WeeklyEarnings] as money ))/0.95 AS Varchar(10) )
ELSE CAST ( (convert(money, [MaxWorkCover] + (0.80 * convert(money, [WeeklyEarnings] )) )/0.80 ) as varchar(10))
END

The structure of the table ( its a legacy table from 2000 i beleive)

CREATE TABLE [dbo].[tblPayments](
[PaymentID] [int] IDENTITY(1,1) NOT NULL,
[ClaimID] [int] NOT NULL,
[WeekNo] [smallint] NULL,

[code]....

Arithmetic overflow error converting numeric to data type varchar.

View 6 Replies View Related

The Difference Between Cast And Convert

May 14, 2008

Can anyone tell me what is the difference between Cast and Convert...?

BOL tells me that they provide similar functionality; it doesn't say that they provide the same functionality.


What is the purpose of having both... is the reason historical?

Which one performs better?

View 3 Replies View Related

Cast Or Convert -2 To Just Show 2

Mar 24, 2008

ToMatch=58
ToNoMatch=1


select @Agg1= Cast(cast(cast(cast(Max(TotMatch) as decimal) / cast((Max(TotNoMatch) + Max(TotMatch)) as decimal)* 100 as int) - 100 as int) as varchar) + '%' FROM #Rpt;

if i try this it is giving me answer in -2%
how i can remove the - sign



also if I try the same statement in different way. well I am going to post in other post.

View 3 Replies View Related

What Is Diff Betn Cast And Convert

May 22, 2008

 Hi alll,can anyone tell me that In which cases cast is used in db and what is diff betn cast and convert

View 9 Replies View Related

Cast Or Convert And Aggreate Function

Dec 1, 2000

Hi people,
I am trying to use Aggregate function like count or average on a column which has a datatype Varchar. In order to use avg or count on it , I am doing a cast on it.
If somebody has use Cast/Convert with aggregate function please help me
Thanks
Jesal

View 2 Replies View Related

CAST Or CONVERT When Calculating AVG From INTs?

Nov 13, 2013

I have an INT field that I want to take the average value of, to the 2nd decimal. Is there a difference between using CAST vs CONVERT?

CAST

Code:
CAST(Avg(CAST(units AS DECIMAL(10,2))) AS DECIMAL(10,2))
from myTable
CONVERT

Code:
CONVERT(numeric(10,2),avg(CONVERT(numeric(10,2),units)))
from myTable

View 4 Replies View Related

Cast Convert(varchar(12),newDate, 101)

Mar 25, 2008

1)Cast(isnull(Partl,0) as smallint)
2) convert(varchar(12),newDate, 101)

what two lines would do. ??

thanks,

View 1 Replies View Related

Convert Or Cast HexaDecimal To Bigint

Aug 21, 2006

Hi ,

I have a hexadecimal string value. I want to convert it to Bigint in Sql Server 2005.

The Hexadecimal value is '0x000000000000000F'.

How is it possible to convert into Bigint.

Please help me

Thanks in advance

Srinivas





View 6 Replies View Related

SQL Server 2012 :: Using CAST Or Convert Functions

May 31, 2015

How do I use the CAST or CONVERT function in the code below, I require a third column (named Diff) which Minus the StartTime from the EndTime and the result is outputted in the third column (named Diff).

Calculation: @Diff = (@EndTime - @StartTime)

I still want the variables (@StartTime and @EndTime) to remain as nvarchar.

The code:

DECLARE @StartTime nvarchar(10) = '12:10';
DECLARE @EndTime nvarchar(10) = '12:30';
DECLARE @Diff time(1) = '00:00';
SELECT @StartTime AS '@StartTime', @EndTime AS '@EndTimes', @Diff AS '@Diff';

View 7 Replies View Related

Order By Case Cast Convert Error

Jul 23, 2005

I have created a SQL Stored Procedure that uses a Case statement todetermine the Order By. For one of the Case statements I am trying toturn a Char field into Datetime in for the Order By, however I can notget it to work. Can someone please take a look and my code below andtell me what I am doing wrong. Thank you.ORDER BYCASE WHEN @SortBy = 'dttm_stamp' THEN dttm_stamp End,CASE WHEN @SortBy = 'Event_Date1' THEN CAST(CONVERT(char(10),Event_Date1,101) as datetime) End,CASE WHEN @SortBy = 'FullName' THEN Emp_lastname + ', ' +Emp_firstname End,CASE WHEN @SortBy = 'FullName Desc' THEN Emp_lastname + ', ' +Emp_firstname End DESC,CASE WHEN @SortBy = 'Emp_SSN' THEN Emp_SSN End

View 11 Replies View Related

Date Conversion Woes: Cast? Convert?

Nov 7, 2007

I have been having some trouble trying to get the date format YYYYMMDD 00:00:00.00 to convert to MM/DD/YYYY. I am using SQL 2005 and Reporting Services. I attempted several variations to solve this problem as outlined below, with the final working conclusion at the end. I am not saying that this is the BEST or the ONLY way to achieve the solution, but it worked for me.



Code Block
---------------------------------------------------------------------------------------
SELECT DATE_TIME AS DATE1
FROM TRANSLOG
--returns value of 20071027 02:26:24.06
---------------------------------------------------------------------------------------
SELECT CAST(DATE_TIME AS SMALLDATETIME) AS DATE1
FROM TRANSLOG
--returns value of 2007-10-27 02:26:00
---------------------------------------------------------------------------------------
SELECT LEFT(CAST(DATE_TIME AS SMALLDATETIME),11) AS DATE1
FROM TRANSLOG
--returns value of Oct 27 2007
---------------------------------------------------------------------------------------
SELECT CAST(SUBSTRING(DATE_TIME,0,9) AS SMALLDATETIME) AS DATE1
FROM TRANSLOG
--returns value of 2007-10-27 00:00:00
---------------------------------------------------------------------------------------
SELECT CONVERT(VARCHAR(10), DATE_TIME, 103)
FROM TRANSLOG
--although this works with GETDATE(), it does not work with the DATE_TIME field
---------------------------------------------------------------------------------------
SELECT CONVERT(VARCHAR(10), CAST(DATE_TIME AS SMALLDATETIME), 101) AS DATE1
FROM TRANSLOG
--THIS WORKS!!! Displaying 10/27/2007





Hope this helps somebody someday!

-Jody

View 5 Replies View Related

Error When Convert Or Cast Functions From Varchar To XML Datatype

Dec 29, 2007

Hi I have a varchar(8000) and currently XML files are stored in varchar(8000).Some times when i am doing manuplactions in my varchar column i am getting with special characters error. so now i want to keep my column varchar(MAX) and when i am doing calculations i will convert my varchar datatype to xml datatype. By doing this i hope there wont be any special character problems.
When i am doing calculations with the wellformed xml i am getting error for both convert and cast methods as below 
I am trying to do convert(xml,MyVarcharColumn)
Implicit conversion from data type xml to nvarchar is not allowed. Use the CONVERT function to run this query.
Also i tried with casting and getting same problme. is there any way to convert
 
please suggest me
 
Thanks
Dilip

View 1 Replies View Related

Problem With CAST And CONVERT In SQL Server2000 Converting Decimal Places From 4 To 2

May 11, 2005

All of my currency columns are only storing 2 decimal places when I insert into the database but when I pull out the data with a SELECT statement, I always get 4 decimal places instead of the 2 that were inserted. 
For example: 
Database Price            SELECT statement Price
100.56                           100.5600
I have tried to use the CAST and/or CONVERT commands but I cannot get the output to come out as 100.56.  Has anyone had a similar problem?
Thanks

View 5 Replies View Related

CS0266: Cannot Implicitly Convert Type 'object' To 'bool'. An Explicit Conversion Exists (are You Missing A Cast?)

Apr 20, 2008

Using OnSelected on the SqlDataSource----here is my currect code:private void On_Load(Object sender, SqlDataSourceCommandEventArgs e){if (e.Command.Parameters["@DueDate"].Value = "NULL"){e.Command.Parameters["@DueDate"].Value = "0000-00-00 00:00:00.000";}}
I am trying to change the value from  NULL(in a datetime field) to "0000-00-00 00:00:00.000" for use on the page.Any ideas, suggestions, or thoughts would be greatly appreciated!
,Gary"we truly fail, only when we quit."

View 2 Replies View Related

Simple Convert Question!

May 18, 2006

Hello all!

Never did a convert, I been looking for samples but I don't quite get it. I get this error message in my vb.net app, Taking a number value from a textbox, adding it to a table, with a money field.

"Implicit conversion from data type money to nvarchar is not allowed. Use the convert function to run this query"

Here is the SP it's pulling,

CREATE PROCEDURE dbo.AddWg (@UserID NvarChar(50),
@Credits Money)
AS
IF EXISTS (SELECT 1 FROM Bankroll WHERE UserID=@UserID)
begin
Update BankRoll
Set Credits = Credits + CAST(@Credits as money)
where userID = @userID
return -1
end
ELSE
begin
Insert Bankroll (Credits, UserID) Values (@Credits, @UserID)
RETURN @@IDENTITY

end
GO

The store procedure worked when Credits was nvarchar, but then I switched it to money, and now this. How would I use the CONVERT to fix this?



TIA!!!



Rudy

View 3 Replies View Related

Simple Way To Convert '1900-01-01 17:12:00.000' To 5:12pm?

Oct 25, 2006

I have a table called class_time. and the column name is start_time.datatype is datetime.id start_time21900-01-01 15:00:00.00041900-01-01 17:12:00.000i want to the convert the start_time column into a format that onlyhas the hour and minutes plus AM/PM ( for example, 3:00 pm, 5:12pm)id start_time new format21900-01-01 15:00:00.000 3:00 pmis there any simple one step way to do that?(the only way i know is to use the following query, then do a substringwhere the space is. but that's too complicated)select CONVERT(CHAR(20), start_time, 22) from class_timeThank you in advance for your time.

View 2 Replies View Related

SQL Server 7.0 Top Command Convert To Oracle 8i

Jul 26, 2000

I'm trying to convert a simple SQL Statement over to Oracle...

Select top 25 * from Customers

Is there a way to just select the top 25 in Oracle??

Thanks

View 1 Replies View Related

Convert Access DB To MS SQL With Dtswiz(command Line)

Jul 20, 2005

Hi!I' m sitting here now for days and I still get no conclusion on myproblem. I have an MS ACCESS .mdb File and I would like to export itwith dtswiz.exe (from MS SQL Utility Tools) via command line. (If youknow other tools which are able to do that via command line I wouldappreciate any suggestion)I tried to find another Provider Parameter (like MSDASQL) for Accessbut somehow it seems to me that Microsoft only got one. I' m also ableto address a Server which appears in the GUI but I don t need one formy ACCess DB (I only tried, if the command parameters have any effecton the converter).I would be glad if someone could help me on this.Thank youKlaus Baumgarten

View 1 Replies View Related

Combine And Convert Int Fields To Create One Field, Should Be Simple

Oct 19, 2007

I want to combine two of my fields somewhat like when you combine to char fields and concatenate them. But these two fields are int. How do i do that? Here's my query right now:




Code Block
SELECT AutoID,
CONVERT(Varchar(Mars_Calender_Year )+ CONVERT(Varchar(Mars_Calender_Period_Code) as MarsId
FROM NavisionReplication.dbo.Tbl_Mars_Calender
ORDER BY Mars_Calender_Year DESC








what am i doing wrong?

View 5 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved