Friday, March 30, 2012
How Can I Convert Decimal To Hexadecimal
I'm looking for a SQL FUnction that convert a decimal to Hexadecimal and
Hexadecimal to decimal data.
I know the way to convert for. But not with a SQL Function. certainly I
need to know How to express an Exponential Function.
Thank's.Hi!!!!!
I'm looking for a SQL FUnction that convert a decimal to Hexadecimal and
Hexadecimal to decimal data.
I know the way to convert for. But not with a SQL Function. certainly I
need to know How to express an Exponential Function.
Thank's.
check this...
/* User Defined Function To Convert HexaDecimal Value To Decimal Value
Input: HexaDecimal Value In String Format
Output: Decimal Value
*/
CREATE FUNCTION [dbo].[Fn_HEXCONV] (@.HEXVAL as VARCHAR(25)) RETURNS DECIMAL(20,0)
AS BEGIN
/* Declarations Of Variables Two Decimal Values To Store The Intermdeiate & Final Result,
String Value To Store The Hexadecimal Value During The Process,Two Counter Variables*/
DECLARE @.position int, @.INTVAL INT , @.CMDSTR NVARCHAR( 255 ) ,@.DECVAL DECIMAL(20,0),@.DECVALUE DECIMAL(20,0)
/* Initialising Variables */
SET @.position = 1
SET @.DECVAL=0
WHILE @.position <= DATALENGTH(REVERSE(@.HEXVAL)) /* Looping Through The String Until It Reaches The 0th Position */
BEGIN
/* Store The Decimal Value If the Hexa Value is Between A-F */
SET @.CMDSTR=CASE UPPER(SUBSTRING(REVERSE(@.HEXVAL) ,@.position,1)) WHEN 'A' THEN '10' WHEN 'B' THEN '11' WHEN 'C' THEN '12' WHEN 'D' THEN '13' WHEN 'E' THEN '14' WHEN 'F' THEN '15' ELSE SUBSTRING(REVERSE(@.HEXVAL) ,@.position,1) END
SET @.INTVAL=CAST(@.CMDSTR as INT) /* Casting The String To Integer */
SET @.DECVALUE=@.INTVAL
SET @.DECVAL=@.DECVAL+((@.DECVALUE)*POWER(CAST(16 AS BIGINT),@.position-1))/* Finding The Corresponding Decimal Value & Adding it To The Result */
SET @.position=@.position+1 /* Incrementing The Counter */
End
return CAST(@.DECVAL as Decimal(20,0)) /* Return The Converted Decimal Value Back */
End
Hope it will help you.
Joydeep ;)|||That functionality is built in, you don't need a function for it.DECLARE @.d DECIMAL(4)
SET @.d = 128
SELECT CAST(@.d AS VARBINARY(8)), CAST(0x0400000101000000 AS DECIMAL(4))-PatP
Friday, March 23, 2012
How can I call a VB function from SQL Server SP?
I need to use the VB function "strconv" in my SP.
Is it possible?
Can anyone help?
Best regards
David LauWhy? What's so special about that particular string that converting it can't
be done in T-SQL, yet you decided it should be done on the server?
The sp_OA* procedures let you invoke ActiveX components from SQL, but there
are several downsides to this. The two worst IMHO are: possible negative
impact on server stability, and performance issues.
ML
http://milambda.blogspot.com/|||David L. wrote:
> Hi
> I need to use the VB function "strconv" in my SP.
> Is it possible?
> Can anyone help?
> Best regards
> David Lau
AFAIK all the features of strconv are supported by native TSQL
equivalents. It would therefore be simpler and much more efficient to
use the TSQL version rather than make an external call to VB. Take a
look at the string functions in SQL Server Books Online.
The answer to the question in your subject depends on the version of
SQL Server you are using. In SQL 2000 your options are limited to COM
automation using the sp_OA procs or making a call via an extended proc.
Usually it would be better to utilize client-side code or TSQL.
In 2005 you have the option of putting .NET code directly in a proc.
David Portas
SQL Server MVP
--|||don't do that
"David L." <DavidL@.discussions.microsoft.com> wrote in message
news:A3447E24-AD32-42D0-A834-9C8977D5596C@.microsoft.com...
> Hi
> I need to use the VB function "strconv" in my SP.
> Is it possible?
> Can anyone help?
> Best regards
> David Lau|||Hello David L.,
> I need to use the VB function "strconv" in my SP.
If you're just doing UpperCase/LowerCase, there's the Upper() and Lower()
functions in T-SQL. If your wanting ProperCase and you're using SQL2005,
you can wrap the function with SQLCLR, ala:
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Imports Microsoft.SqlServer.Server
Partial Public Class StringConversionFunctions
<Microsoft.SqlServer.Server.SqlFunction()> _
Public Shared Function ProperCase(ByVal Source As SqlString) As SqlString
Return New SqlString(StrConv(Source.Value, VbStrConv.ProperCase))
End Function
End Class
Call it via:
select 'Proper',dbo.ProperCase('ThE QUick BROWN fox jumpEd oVeR thE laZY
DOG')
Thank you,
Kent Tegels
DevelopMentor
http://staff.develop.com/ktegels/|||Thank you Kent and everyone who has replied this email.
Let's say I would like to do the Narrow for Japanese charactors.
Is there any similar function in TSQL?
Best regards
David
"Kent Tegels" wrote:
> Hello David L.,
>
> If you're just doing UpperCase/LowerCase, there's the Upper() and Lower()
> functions in T-SQL. If your wanting ProperCase and you're using SQL2005,
> you can wrap the function with SQLCLR, ala:
> Imports System
> Imports System.Data
> Imports System.Data.SqlClient
> Imports System.Data.SqlTypes
> Imports Microsoft.SqlServer.Server
> Partial Public Class StringConversionFunctions
> <Microsoft.SqlServer.Server.SqlFunction()> _
> Public Shared Function ProperCase(ByVal Source As SqlString) As SqlStr
ing
> Return New SqlString(StrConv(Source.Value, VbStrConv.ProperCase))
> End Function
> End Class
> Call it via:
> select 'Proper',dbo.ProperCase('ThE QUick BROWN fox jumpEd oVeR thE laZY
> DOG')
>
> Thank you,
> Kent Tegels
> DevelopMentor
> http://staff.develop.com/ktegels/
>
>|||Hello David L.,
> Thank you Kent and everyone who has replied this email.
> Let's say I would like to do the Narrow for Japanese charactors.
> Is there any similar function in TSQL?
Not that I'm immediately aware since it's not really clear to me what Narrow
actually does. Is it something like this?
declare @.m1 nvarchar(50)
set @.m1 = N'? 150 ?'
select cast(@.m1 as varchar(50))
Either way, you should still be able to use the code previously shown with
a different enumeration value.
Domo,
Kentsql
Sunday, February 19, 2012
HostName Function with Access2K front end
identify which user has suppied a record to a table that holds
temporary data.
I'm using an Access2K front end.
Code:
Alter procedure SPName
@.parameter1 int
AS
Set nocount On
Set xact_abort off
Declare @.myHost nvarchar(50)
Set @.myHost = Host_Name()
Insert Into tblMyNameTEMP(UniqueID, AnyField1, AnyField2,
myMachineName)
SELECT tblMyName.UniqueID, AnyField1, AnyField2, @.myHost
FROM tblMyName
WHERE tblMyName.UniqueID = @.parameter1
I have two problems.
In some cases (and only on one or two of maybe about 250 client
workstations) Host_Name() returns the name of MY machine. I'm thinking
this is because I developed the app and distributed it and for some
reason it's retaining the info contained in my original connection
setup set in the MS-Access Connection window?
Also, on some occasions, I get blocking messages in my trace log
following this operation.
Any help on these two issues is appreciated.
lq"Lauren Quantrell" <laurenquantrell@.hotmail.com> wrote in message
news:47e5bd72.0404100634.3ab98872@.posting.google.c om...
> In an Insert Into statement I have used the Host_Name() function to
> identify which user has suppied a record to a table that holds
> temporary data.
> I'm using an Access2K front end.
> Code:
> Alter procedure SPName
> @.parameter1 int
> AS
> Set nocount On
> Set xact_abort off
> Declare @.myHost nvarchar(50)
> Set @.myHost = Host_Name()
> Insert Into tblMyNameTEMP(UniqueID, AnyField1, AnyField2,
> myMachineName)
> SELECT tblMyName.UniqueID, AnyField1, AnyField2, @.myHost
> FROM tblMyName
> WHERE tblMyName.UniqueID = @.parameter1
> I have two problems.
> In some cases (and only on one or two of maybe about 250 client
> workstations) Host_Name() returns the name of MY machine. I'm thinking
> this is because I developed the app and distributed it and for some
> reason it's retaining the info contained in my original connection
> setup set in the MS-Access Connection window?
> Also, on some occasions, I get blocking messages in my trace log
> following this operation.
> Any help on these two issues is appreciated.
> lq
As a complete guess, there may be some network name resolution issue which
means that the server sometimes resolves workstation names incorrectly. You
should be able to investigate this with your network admin if you can pin it
down to just a couple of machines.
As for the blocking issue, it's hard to say without more information, such
as the text of the errors.
Simon