Showing posts with label function. Show all posts
Showing posts with label function. Show all posts

Thursday, March 29, 2012

Converting DateTime to SqlDateTime format

Hi,

I have a function that generates a range of DateTimes, which I then cast to SqlDateTime to compare with SqlDateTime values in a database.

The problem is my converted DateTimes come out in this type of format "6/2/2006 12:00:00 AM"

wheras my SqlDateTimes in the database are in this format "2006-01-18T00:00:00.0000000-12:00"

Any ideas how I can convert the DateTime values to SqlDateTime correctly so that I can compare them? As I said I tried creating a new SqlDateTime object with the DateTime value ie

DateTime dt = new DateTime("");

SqlDateTime sdt = new SqlDateTime(dt);

But that doesn't work correctly, its still not in the format that is in the database.

Assuming you are using the datetime datatype, the format that it is in the database is not"2006-01-18T00:00:00.0000000-12:00". From Books Online, the database actually stores datetime values as two 4-byte integers. The first 4 bytes store the number of days before or after the base date: January 1, 1900. The other 4 bytes store the time of day represented as the number of milliseconds after midnight.

So, the"2006-01-18T00:00:00.0000000-12:00" is just an output representation of that value.

If you give us a little more information about what you are trying to do we should be better able to help you.

Sunday, March 25, 2012

Converting a type (e.g. Decimal) BEFORE writing to ResultSet ?

As well known a DATE or TIMESTAMP type can be converted (to VARCAHR) after s
election but BEFORE
writing to the ResultSet by using the CONVERT function e.g.
SELECT CONVERT(char(10), MYTIMESTAMP, 101)) FROM ... WHERE ...
Is there something similar if the original field is a DECIMAL/NUMERIC field?
E.g.
SELECT DEC_CONVERT(char(30), MYDECIMAL, '###########0.00') FROM .... WHERE
...
GeorgeGeorge Dainis wrote:
> As well known a DATE or TIMESTAMP type can be converted (to VARCAHR)
> after selection but BEFORE writing to the ResultSet by using the
> CONVERT function e.g.
> SELECT CONVERT(char(10), MYTIMESTAMP, 101)) FROM ... WHERE ...
> Is there something similar if the original field is a DECIMAL/NUMERIC
> field?
> E.g.
> SELECT DEC_CONVERT(char(30), MYDECIMAL, '###########0.00') FROM ....
> WHERE ...
> George
Just use CONVERT or CAST:
Select CONVERT(char(30), MyDecimal) From ...
Select CAST(MyDecimal as char(30)) From ...
David Gugick
Imceda Software
www.imceda.com|||You can also use function STR.
Example:
declare @.d decimal(8, 2)
set @.d = 50.25 / 2.00
select @.d, str(@.d, 8, 2)
go
AMB
"George Dainis" wrote:

> As well known a DATE or TIMESTAMP type can be converted (to VARCAHR) after
selection but BEFORE
> writing to the ResultSet by using the CONVERT function e.g.
> SELECT CONVERT(char(10), MYTIMESTAMP, 101)) FROM ... WHERE ...
> Is there something similar if the original field is a DECIMAL/NUMERIC fiel
d?
> E.g.
> SELECT DEC_CONVERT(char(30), MYDECIMAL, '###########0.00') FROM .... WHER
E ...
> George
>|||>From the documentation ... "CONVERT converts a character string from
one character set to another. The datatype of the returned value is
VARCHAR2." So what you are seeing is an implicit conversion to varchar2
because you are using a function that accepts a char as input.
Look at the functions TO_CHAR(), TO_DATE(), TO_NUMBER() and CAST() for
type conversion ...
http://download-west.oracle.com/doc.../b10759/toc.htm|||Oh that was strange ... I accessed the question through
comp.databases.oracle.misc but google tells me that the answer will get
posted to a sqlserver group? hmmm.|||On 16 Feb 2005 04:51:03 -0800, David Aldridge wrote:

>Oh that was strange ... I accessed the question through
>comp.databases.oracle.misc but google tells me that the answer will get
>posted to a sqlserver group? hmmm.
Hi David,
The orinal question was crossposted to a total of three groups:
* comp.databases.oracle.misc
* microsoft.public.sqlserver.programming
* comp.databases.oracle
The followup-to was set to only the SQL Server group. The use of CONVERT
in the original question suggests that this is indeed a SQL Server related
question. I have no idea why the original poster has included two Oracle
groups in his crossposting.
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||"George Dainis" <george.dainis@.bluecorner.com> wrote in message
news:cuu58t$irh$04$1@.news.t-online.com...
> As well known a DATE or TIMESTAMP type can be converted (to VARCAHR) after
> selection but BEFORE
> writing to the ResultSet by using the CONVERT function e.g.
> SELECT CONVERT(char(10), MYTIMESTAMP, 101)) FROM ... WHERE ...
> Is there something similar if the original field is a DECIMAL/NUMERIC
> field?
> E.g.
> SELECT DEC_CONVERT(char(30), MYDECIMAL, '###########0.00') FROM ....
> WHERE ...
> George
>
Also well known are the TO_CHAR, ROUND, and TRUNC functions -- and don't
forget the CAST operator ;-)
++ mcs

Thursday, March 22, 2012

Converting a number to hexadcimal

Is there any function in SQL-Server which converts a number into its Hexadecimal equivalent..? if not, how should I convert a number to its hexadecimal equivalent..?
Thanks
Jakesomething like:
SELECT CAST( 123456 AS BINARY(4) )|||Originally posted by harshal_in
something like:
SELECT CAST( 123456 AS BINARY(4) )

Thanks Harsh

Converting a Hex string to binary

I am trying to write a function to convert a hex string to binary. I would like it in a function so I can use it on aggregate data in queries (instead of having to cursor through the data). So, I write my function:

CREATE FUNCTION HexToBinary (@.hexstring char(16)) RETURNS binary(8)
AS
BEGIN
declare @.b binary(8)
,@.sql nvarchar(255)

SET @.sql = N'SET @.b = 0x' + @.hexstring
EXEC sp_executesql @.sql,N'@.b binary(8) out',@.b output
RETURN @.b
END

Then, I try to call my function:

SELECT HexToBinary('E0')

...and I get:

Msg 195, Level 15, State 10, Line 1
'HexToBinary' is not a recognized built-in function name.

However, I can get it to work if I use a slightly different syntax:

declare @.b binary(8)
exec @.b = HexToBinary 'E0'
select @.b

Any thoughts as to what might be going on? Obviously, the lower syntax does not help me call this function in queries, which is really my goal.sp_executesql is indeterministic and is not allowed within a UDF.

Here is the UDF that would give you what you want (courtesy of Itzik):

create function dbo.fn_hexstrtovarbin(@.input varchar(8000))
returns varbinary(8000)
as
begin
declare @.result varbinary(8000), @.i int, @.l int

set @.result = 0x
set @.l = len(@.input)/2
set @.i = 2

while @.i <= @.l
begin
set @.result = @.result +
cast(cast(case lower(substring(@.input, @.i*2-1, 1))
when '0' then 0x00
when '1' then 0x10
when '2' then 0x20
when '3' then 0x30
when '4' then 0x40
when '5' then 0x50
when '6' then 0x60
when '7' then 0x70
when '8' then 0x80
when '9' then 0x90
when 'a' then 0xa0
when 'b' then 0xb0
when 'c' then 0xc0
when 'd' then 0xd0
when 'e' then 0xe0
when 'f' then 0xf0
end as tinyint) |
cast(case lower(substring(@.input, @.i*2, 1))
when '0' then 0x00
when '1' then 0x01
when '2' then 0x02
when '3' then 0x03
when '4' then 0x04
when '5' then 0x05
when '6' then 0x06
when '7' then 0x07
when '8' then 0x08
when '9' then 0x09
when 'a' then 0x0a
when 'b' then 0x0b
when 'c' then 0x0c
when 'd' then 0x0d
when 'e' then 0x0e
when 'f' then 0x0f
end as tinyint) as binary(1))
set @.i = @.i + 1
end

return @.result
end
go

Converting a date from one timezone to another timezone

Hi,
I have a requirement for a function which would convert a given date with a present timezone to a new timezone.

For eg : the function would be called lets say TZ

then I can issue the following select statement to get the new timezone

select getdate(), TZ(getdate(), PST, EST)

where PST is the current timezone and EST is the new timezone.

I know that ORACLE has a built in function to do this. Am just wondering if theres some function that I can use to accomplish the task.

Thanks
-soumilyou can write your own function using info from this site (http://wwp.greenwichmeantime.com/info/timezone.htm).|||This is a real pain-in-the-butt to do, not just because of all the time zones, but because even within a time-zone some areas may use Daylight Savings Time, and some may not.

I had to deal with this when I designed an EDI tracking application. In the end we decided it was best to always output the data in GMT and then let the webpage translate it to whatever local time the page was being viewed in. For incoming data, I required all events to include a time stamp from their point of origin, and then I added a local time stamp. By comparing the two values we were able to synchronize events across time-zones without even caring what zone they came from, and it accounted for discrepancies in system-clock times as well.

I hope you can use some work-around like this, or find someone who has already written the procedure/function for you.|||there is one already written by tara check it out:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=28712
harsh.

Tuesday, March 20, 2012

Converted data types

I'm using SQL Servers Import function to import an entire MS Access
database.
Date/time types from Access are defaulting to smalldatetime in SQL
Server, but the actual data is too large and I get an error, or rather
many errors since this happens in many tables.
I can edit the SQL to make these datetime types but, since there are
so many, is there any way to globally change the default data mapping?
What do you mean "the actual data is too large"? What "error" do you get?
Do you really have a lot of dates that fall outside the range supported by
smalldatetime?
Anyway, there is an easy way to generate a script to alter all smalldatetime
columns => datetime.
SELECT 'ALTER TABLE ['+TABLE_SCHEMA+'].[' + TABLE_NAME + '] ALTER COLUMN ['
+ COLUMN_NAME + '] DATETIME'
+ CASE WHEN IS_NULLABLE = 'NO' THEN
' NOT NULL ' ELSE '' END
+ CASE WHEN COLUMN_DEFAULT IS NOT NULL THEN
' DEFAULT ' + COLUMN_DEFAULT ELSE '' END + ';'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE DATA_TYPE = 'smalldatetime';
Aaron Bertrand
SQL Server MVP
http://www.sqlblog.com/
http://www.aspfaq.com/5006
"blindsey" <blindsey@.dsicdi.com> wrote in message
news:1183407383.413284.218870@.n2g2000hse.googlegro ups.com...
> I'm using SQL Servers Import function to import an entire MS Access
> database.
> Date/time types from Access are defaulting to smalldatetime in SQL
> Server, but the actual data is too large and I get an error, or rather
> many errors since this happens in many tables.
> I can edit the SQL to make these datetime types but, since there are
> so many, is there any way to globally change the default data mapping?
>

Converted data types

I'm using SQL Servers Import function to import an entire MS Access
database.
Date/time types from Access are defaulting to smalldatetime in SQL
Server, but the actual data is too large and I get an error, or rather
many errors since this happens in many tables.
I can edit the SQL to make these datetime types but, since there are
so many, is there any way to globally change the default data mapping?What do you mean "the actual data is too large"? What "error" do you get?
Do you really have a lot of dates that fall outside the range supported by
smalldatetime?
Anyway, there is an easy way to generate a script to alter all smalldatetime
columns => datetime.
SELECT 'ALTER TABLE ['+TABLE_SCHEMA+'].[' + TABLE_NAME + '] ALTER COLUMN ['
+ COLUMN_NAME + '] DATETIME'
+ CASE WHEN IS_NULLABLE = 'NO' THEN
' NOT NULL ' ELSE '' END
+ CASE WHEN COLUMN_DEFAULT IS NOT NULL THEN
' DEFAULT ' + COLUMN_DEFAULT ELSE '' END + ';'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE DATA_TYPE = 'smalldatetime';
--
Aaron Bertrand
SQL Server MVP
http://www.sqlblog.com/
http://www.aspfaq.com/5006
"blindsey" <blindsey@.dsicdi.com> wrote in message
news:1183407383.413284.218870@.n2g2000hse.googlegroups.com...
> I'm using SQL Servers Import function to import an entire MS Access
> database.
> Date/time types from Access are defaulting to smalldatetime in SQL
> Server, but the actual data is too large and I get an error, or rather
> many errors since this happens in many tables.
> I can edit the SQL to make these datetime types but, since there are
> so many, is there any way to globally change the default data mapping?
>

Converted data types

I'm using SQL Servers Import function to import an entire MS Access
database.
Date/time types from Access are defaulting to smalldatetime in SQL
Server, but the actual data is too large and I get an error, or rather
many errors since this happens in many tables.
I can edit the SQL to make these datetime types but, since there are
so many, is there any way to globally change the default data mapping?What do you mean "the actual data is too large"? What "error" do you get?
Do you really have a lot of dates that fall outside the range supported by
smalldatetime?
Anyway, there is an easy way to generate a script to alter all smalldatetime
columns => datetime.
SELECT 'ALTER TABLE ['+TABLE_SCHEMA+'].[' + TABLE_NAME + '] ALTER CO
LUMN ['
+ COLUMN_NAME + '] DATETIME'
+ CASE WHEN IS_NULLABLE = 'NO' THEN
' NOT NULL ' ELSE '' END
+ CASE WHEN COLUMN_DEFAULT IS NOT NULL THEN
' DEFAULT ' + COLUMN_DEFAULT ELSE '' END + ';'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE DATA_TYPE = 'smalldatetime';
Aaron Bertrand
SQL Server MVP
http://www.sqlblog.com/
http://www.aspfaq.com/5006
"blindsey" <blindsey@.dsicdi.com> wrote in message
news:1183407383.413284.218870@.n2g2000hse.googlegroups.com...
> I'm using SQL Servers Import function to import an entire MS Access
> database.
> Date/time types from Access are defaulting to smalldatetime in SQL
> Server, but the actual data is too large and I get an error, or rather
> many errors since this happens in many tables.
> I can edit the SQL to make these datetime types but, since there are
> so many, is there any way to globally change the default data mapping?
>

CONVERT() FUNCTION

i want a datetime format as mm/dd/yyyy in dataset i am getting
AcquisitionDate="2005-05-05T00:00:00+05:30"

if i used convert(datetime,AcquisitionDate) function ,the sql is throwing error convertion failed to datetime as character string


any other way to convert this

guide me please

SQL Server's datetime does not store timezone as part of the datetime format.

To store this data, you can either strip off the time zone portion (using SUBSTRING in sql or Substring in .Net) and store as is. If the timezone is important to you, you can normalize the time with respect to a standard time zone and then strip the timezone off.

-Todd

Convert() function

Hi, I have found some ways on using Convert() function for SQL server. But i do not understand some of them.

Example:

CONVERT(VARCHAR(10),column name,108)

What is the 108 for? I see some with 120 and 101. I tried all but only 108 is working for me the way I want..but i do not understand what is that for actually. Can anyone explain it to me? Thank you.

Hi,

You can get the time part of a date column value as varchar by using the convert function with style 108

Say,

select top 1 ApplicationDate from Applications

returns "2003-04-11 15:20:00"

Then

SELECT top 1 CONVERT(VARCHAR(10), ApplicationDate ,108) from Applications

will return

15:20:00

For more information check BOL for CAST and CONVERT

Eralper

http://www.kodyaz.com


|||Also, check out theCast and Converttopic on Books Online. That third parameter to the CONVERTfunction is the style and there are charts on that page which explainthe different possible style values.
The style I use most often is 112, which converts a datetime to an ISO date in the format YYYYMMDD.

Monday, March 19, 2012

Convert varchar to 12 hour time format

A column stores the time as varchar 24 hour format. Is there a way to
change the data using the convert function to 12 hour format from the
sql select statement? Thanks.>A column stores the time as varchar 24 hour format.
Why are you storing time as a varchar? Wouldn't datetime make more sense?
>A column stores the time as varchar 24 hour format. Is there a way to
> change the data using the convert function to 12 hour format from the
> sql select statement?
SELECT [12 hour format] = LTRIM(SUBSTRING(CONVERT(VARCHAR(20),
CONVERT(DATETIME, varchar_column), 22), 10, 5) + RIGHT(CONVERT(VARCHAR(20),
CONVERT(DATETIME, varchar_column), 22), 3))
FROM table
WHERE ISDATE(varchar_column) = 1;
Aaron Bertrand
SQL Server MVP
http://www.sqlblog.com/
http://www.aspfaq.com/5006

Convert varchar to 12 hour time format

A column stores the time as varchar 24 hour format. Is there a way to
change the data using the convert function to 12 hour format from the
sql select statement? Thanks.>A column stores the time as varchar 24 hour format.
Why are you storing time as a varchar? Wouldn't datetime make more sense?

>A column stores the time as varchar 24 hour format. Is there a way to
> change the data using the convert function to 12 hour format from the
> sql select statement?
SELECT [12 hour format] = LTRIM(SUBSTRING(CONVERT(VARCHAR(20),
CONVERT(DATETIME, varchar_column), 22), 10, 5) + RIGHT(CONVERT(VARCHAR(20),
CONVERT(DATETIME, varchar_column), 22), 3))
FROM table
WHERE ISDATE(varchar_column) = 1;
Aaron Bertrand
SQL Server MVP
http://www.sqlblog.com/
http://www.aspfaq.com/5006

Convert varchar to 12 hour time format

A column stores the time as varchar 24 hour format. Is there a way to
change the data using the convert function to 12 hour format from the
sql select statement? Thanks.
>A column stores the time as varchar 24 hour format.
Why are you storing time as a varchar? Wouldn't datetime make more sense?

>A column stores the time as varchar 24 hour format. Is there a way to
> change the data using the convert function to 12 hour format from the
> sql select statement?
SELECT [12 hour format] = LTRIM(SUBSTRING(CONVERT(VARCHAR(20),
CONVERT(DATETIME, varchar_column), 22), 10, 5) + RIGHT(CONVERT(VARCHAR(20),
CONVERT(DATETIME, varchar_column), 22), 3))
FROM table
WHERE ISDATE(varchar_column) = 1;
Aaron Bertrand
SQL Server MVP
http://www.sqlblog.com/
http://www.aspfaq.com/5006

convert UTC date

hi- i'm working with a database that stores dates in UTC format. does
anyone know of, or have, a function to convert from this format?
tia,
jtWell, what is your offset from UTC?
DECLARE @.offset TINYINT;
SET @.offset = ?;
SELECT DATEADD(HOUR, @.offset, datetimeColumn) FROM table;
If you participate in daylight savings time, you will be much better off
using a calendar table.
http://www.aspfaq.com/2519
And yes, I need to update the article to account for the change in DST
timeframes here in the US, that take effect this year IIRC.
A
"JTL" <jliautaud@.hotmail.com> wrote in message
news:OBHy0SuFGHA.1192@.TK2MSFTNGP11.phx.gbl...
> hi- i'm working with a database that stores dates in UTC format. does
> anyone know of, or have, a function to convert from this format?
> tia,
> jt
>|||ok- thanks for the help-
this database stores the number of seconds that have passed since 1/1/1970,
as an integer field. so how do i convert that to the current date?
jt
"Aaron Bertrand [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> wrote in message
news:%23bQMScuFGHA.3532@.TK2MSFTNGP14.phx.gbl...
> Well, what is your offset from UTC?
> DECLARE @.offset TINYINT;
> SET @.offset = ?;
> SELECT DATEADD(HOUR, @.offset, datetimeColumn) FROM table;
> If you participate in daylight savings time, you will be much better off
> using a calendar table.
> http://www.aspfaq.com/2519
> And yes, I need to update the article to account for the change in DST
> timeframes here in the US, that take effect this year IIRC.
> A
>
> "JTL" <jliautaud@.hotmail.com> wrote in message
> news:OBHy0SuFGHA.1192@.TK2MSFTNGP11.phx.gbl...
>|||http://www.aspfaq.com/2451
As an aside, you may want to use BIGINT, if you ever want to store dates
beyond 2038-01-18.
A
"JTL" <jliautaud@.hotmail.com> wrote in message
news:%23YXLk0uFGHA.3856@.TK2MSFTNGP12.phx.gbl...
> ok- thanks for the help-
> this database stores the number of seconds that have passed since
> 1/1/1970, as an integer field. so how do i convert that to the current
> date?
> jt
> "Aaron Bertrand [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> wrote in
> message news:%23bQMScuFGHA.3532@.TK2MSFTNGP14.phx.gbl...
>|||thank you- big help!
jt
"Aaron Bertrand [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> wrote in message
news:upmrY3uFGHA.2704@.TK2MSFTNGP15.phx.gbl...
> http://www.aspfaq.com/2451
> As an aside, you may want to use BIGINT, if you ever want to store dates
> beyond 2038-01-18.
> A
>
>
> "JTL" <jliautaud@.hotmail.com> wrote in message
> news:%23YXLk0uFGHA.3856@.TK2MSFTNGP12.phx.gbl...
>

Convert User Defined Function

We have a User Function that gets called from Access. We now have a need to
use that same User Function in a VB application. Is there a way to convert
the User Function to a Stored Procedure?
I'm pretty green about this, so any help is appreciated.
tia,
--
JMorrellWhy? IIRC, you can call a function from VB (we do that occasionally from C#
and VB.Net).
Anyway, we can't help you convert the function to a procedure unless you
show us what the function does (!).
http://www.aspfaq.com/
(Reverse address to reply.)
"JMorrell" <JMorrell@.discussions.microsoft.com> wrote in message
news:DE9B9D49-E776-4140-AFAA-98E606B4A2DC@.microsoft.com...
> We have a User Function that gets called from Access. We now have a need
to
> use that same User Function in a VB application. Is there a way to
convert
> the User Function to a Stored Procedure?
> I'm pretty green about this, so any help is appreciated.
> tia,
> --
> JMorrell|||Thanks for the reply. We're needing the results of the function within vb.
The function is as follows:
CREATE FUNCTION ufn_GetSubtree
(
@.supervisorid AS int
)
RETURNS @.tree table
(
employeeid varchar(6) NOT NULL,
supervisorid varchar(6) NULL,
lname varchar(25) NOT NULL,
fname varchar(25) not null,
term int not null,
lvl int NOT NULL,
path varchar(900) NOT NULL
)
AS
BEGIN
DECLARE @.lvl AS int, @.path AS varchar(900)
SELECT @.lvl = 0, @.path = '.'
INSERT INTO @.tree
SELECT employeeid, supervisorid, lname, fname, term,
@.lvl, '.' + CAST(employeeid AS varchar(10)) + '.'
FROM tblemp
WHERE term = 0
and employeeid = @.supervisorid
WHILE @.@.ROWCOUNT > 0
BEGIN
SET @.lvl = @.lvl + 1
INSERT INTO @.tree
SELECT E.employeeid, E.supervisorid, E.lname, E.fname, E.term,
@.lvl, T.path + CAST(E.employeeid AS varchar(10)) + '.'
FROM tblEmp AS E JOIN @.tree AS T
ON E.supervisorid = T.employeeid AND T.lvl = @.lvl - 1
END
RETURN
END
--
I'm not sure how to go about using the function as is when in vb. Is there
a source for help in this?
tiaa,
JMorrell
"Aaron [SQL Server MVP]" wrote:

> Why? IIRC, you can call a function from VB (we do that occasionally from
C#
> and VB.Net).
> Anyway, we can't help you convert the function to a procedure unless you
> show us what the function does (!).
> --
> http://www.aspfaq.com/
> (Reverse address to reply.)
>
>
> "JMorrell" <JMorrell@.discussions.microsoft.com> wrote in message
> news:DE9B9D49-E776-4140-AFAA-98E606B4A2DC@.microsoft.com...
> to
> convert
>
>|||(a) I suggest CREATE FUNCTION dbo.ufn_GetSubTree
(b) this would be easy to convert to a stored procedure (though I'm sure
Celko will want to re-educate you on the right way to handle trees, nested
sets and heirarchies etc.).
(c) try this in VB:
dim conn as adodb.connection
conn.open "your connection string"
dim rs as adodb.recordset
rs.open "SELECT * FROM dbo.ufn_GetSubTree"
...
http://www.aspfaq.com/
(Reverse address to reply.)
"JMorrell" <JMorrell@.discussions.microsoft.com> wrote in message
news:FECA6975-BE71-4CAB-8107-67598D6C921D@.microsoft.com...
> Thanks for the reply. We're needing the results of the function within
vb.
> The function is as follows:
> CREATE FUNCTION ufn_GetSubtree
> (
> @.supervisorid AS int
> )
> RETURNS @.tree table
> (
> employeeid varchar(6) NOT NULL,
> supervisorid varchar(6) NULL,
> lname varchar(25) NOT NULL,
> fname varchar(25) not null,
> term int not null,
> lvl int NOT NULL,
> path varchar(900) NOT NULL
> )
> AS
> BEGIN
> DECLARE @.lvl AS int, @.path AS varchar(900)
> SELECT @.lvl = 0, @.path = '.'
> INSERT INTO @.tree
> SELECT employeeid, supervisorid, lname, fname, term,
> @.lvl, '.' + CAST(employeeid AS varchar(10)) + '.'
> FROM tblemp
> WHERE term = 0
> and employeeid = @.supervisorid
> WHILE @.@.ROWCOUNT > 0
> BEGIN
> SET @.lvl = @.lvl + 1
> INSERT INTO @.tree
> SELECT E.employeeid, E.supervisorid, E.lname, E.fname, E.term,
> @.lvl, T.path + CAST(E.employeeid AS varchar(10)) + '.'
> FROM tblEmp AS E JOIN @.tree AS T
> ON E.supervisorid = T.employeeid AND T.lvl = @.lvl - 1
> END
> RETURN
> END
> --
> I'm not sure how to go about using the function as is when in vb. Is
there[vbcol=seagreen]
> a source for help in this?
> tiaa,
> JMorrell
> "Aaron [SQL Server MVP]" wrote:
>
from C#[vbcol=seagreen]
need[vbcol=seagreen]

Convert User Defined Function

We have a User Function that gets called from Access. We now have a need to
use that same User Function in a VB application. Is there a way to convert
the User Function to a Stored Procedure?
I'm pretty green about this, so any help is appreciated.
tia,
--
JMorrellWhy? IIRC, you can call a function from VB (we do that occasionally from C#
and VB.Net).
Anyway, we can't help you convert the function to a procedure unless you
show us what the function does (!).
--
http://www.aspfaq.com/
(Reverse address to reply.)
"JMorrell" <JMorrell@.discussions.microsoft.com> wrote in message
news:DE9B9D49-E776-4140-AFAA-98E606B4A2DC@.microsoft.com...
> We have a User Function that gets called from Access. We now have a need
to
> use that same User Function in a VB application. Is there a way to
convert
> the User Function to a Stored Procedure?
> I'm pretty green about this, so any help is appreciated.
> tia,
> --
> JMorrell|||Thanks for the reply. We're needing the results of the function within vb.
The function is as follows:
CREATE FUNCTION ufn_GetSubtree
(
@.supervisorid AS int
)
RETURNS @.tree table
(
employeeid varchar(6) NOT NULL,
supervisorid varchar(6) NULL,
lname varchar(25) NOT NULL,
fname varchar(25) not null,
term int not null,
lvl int NOT NULL,
path varchar(900) NOT NULL
)
AS
BEGIN
DECLARE @.lvl AS int, @.path AS varchar(900)
SELECT @.lvl = 0, @.path = '.'
INSERT INTO @.tree
SELECT employeeid, supervisorid, lname, fname, term,
@.lvl, '.' + CAST(employeeid AS varchar(10)) + '.'
FROM tblemp
WHERE term = 0
and employeeid = @.supervisorid
WHILE @.@.ROWCOUNT > 0
BEGIN
SET @.lvl = @.lvl + 1
INSERT INTO @.tree
SELECT E.employeeid, E.supervisorid, E.lname, E.fname, E.term,
@.lvl, T.path + CAST(E.employeeid AS varchar(10)) + '.'
FROM tblEmp AS E JOIN @.tree AS T
ON E.supervisorid = T.employeeid AND T.lvl = @.lvl - 1
END
RETURN
END
--
I'm not sure how to go about using the function as is when in vb. Is there
a source for help in this?
tiaa,
JMorrell
"Aaron [SQL Server MVP]" wrote:
> Why? IIRC, you can call a function from VB (we do that occasionally from C#
> and VB.Net).
> Anyway, we can't help you convert the function to a procedure unless you
> show us what the function does (!).
> --
> http://www.aspfaq.com/
> (Reverse address to reply.)
>
>
> "JMorrell" <JMorrell@.discussions.microsoft.com> wrote in message
> news:DE9B9D49-E776-4140-AFAA-98E606B4A2DC@.microsoft.com...
> > We have a User Function that gets called from Access. We now have a need
> to
> > use that same User Function in a VB application. Is there a way to
> convert
> > the User Function to a Stored Procedure?
> >
> > I'm pretty green about this, so any help is appreciated.
> >
> > tia,
> > --
> > JMorrell
>
>|||(a) I suggest CREATE FUNCTION dbo.ufn_GetSubTree
(b) this would be easy to convert to a stored procedure (though I'm sure
Celko will want to re-educate you on the right way to handle trees, nested
sets and heirarchies etc.).
(c) try this in VB:
dim conn as adodb.connection
conn.open "your connection string"
dim rs as adodb.recordset
rs.open "SELECT * FROM dbo.ufn_GetSubTree"
...
--
http://www.aspfaq.com/
(Reverse address to reply.)
"JMorrell" <JMorrell@.discussions.microsoft.com> wrote in message
news:FECA6975-BE71-4CAB-8107-67598D6C921D@.microsoft.com...
> Thanks for the reply. We're needing the results of the function within
vb.
> The function is as follows:
> CREATE FUNCTION ufn_GetSubtree
> (
> @.supervisorid AS int
> )
> RETURNS @.tree table
> (
> employeeid varchar(6) NOT NULL,
> supervisorid varchar(6) NULL,
> lname varchar(25) NOT NULL,
> fname varchar(25) not null,
> term int not null,
> lvl int NOT NULL,
> path varchar(900) NOT NULL
> )
> AS
> BEGIN
> DECLARE @.lvl AS int, @.path AS varchar(900)
> SELECT @.lvl = 0, @.path = '.'
> INSERT INTO @.tree
> SELECT employeeid, supervisorid, lname, fname, term,
> @.lvl, '.' + CAST(employeeid AS varchar(10)) + '.'
> FROM tblemp
> WHERE term = 0
> and employeeid = @.supervisorid
> WHILE @.@.ROWCOUNT > 0
> BEGIN
> SET @.lvl = @.lvl + 1
> INSERT INTO @.tree
> SELECT E.employeeid, E.supervisorid, E.lname, E.fname, E.term,
> @.lvl, T.path + CAST(E.employeeid AS varchar(10)) + '.'
> FROM tblEmp AS E JOIN @.tree AS T
> ON E.supervisorid = T.employeeid AND T.lvl = @.lvl - 1
> END
> RETURN
> END
> --
> I'm not sure how to go about using the function as is when in vb. Is
there
> a source for help in this?
> tiaa,
> JMorrell
> "Aaron [SQL Server MVP]" wrote:
> > Why? IIRC, you can call a function from VB (we do that occasionally
from C#
> > and VB.Net).
> >
> > Anyway, we can't help you convert the function to a procedure unless you
> > show us what the function does (!).
> >
> > --
> > http://www.aspfaq.com/
> > (Reverse address to reply.)
> >
> >
> >
> >
> > "JMorrell" <JMorrell@.discussions.microsoft.com> wrote in message
> > news:DE9B9D49-E776-4140-AFAA-98E606B4A2DC@.microsoft.com...
> > > We have a User Function that gets called from Access. We now have a
need
> > to
> > > use that same User Function in a VB application. Is there a way to
> > convert
> > > the User Function to a Stored Procedure?
> > >
> > > I'm pretty green about this, so any help is appreciated.
> > >
> > > tia,
> > > --
> > > JMorrell
> >
> >
> >

Convert User Defined Function

We have a User Function that gets called from Access. We now have a need to
use that same User Function in a VB application. Is there a way to convert
the User Function to a Stored Procedure?
I'm pretty green about this, so any help is appreciated.
tia,
JMorrell
Why? IIRC, you can call a function from VB (we do that occasionally from C#
and VB.Net).
Anyway, we can't help you convert the function to a procedure unless you
show us what the function does (!).
http://www.aspfaq.com/
(Reverse address to reply.)
"JMorrell" <JMorrell@.discussions.microsoft.com> wrote in message
news:DE9B9D49-E776-4140-AFAA-98E606B4A2DC@.microsoft.com...
> We have a User Function that gets called from Access. We now have a need
to
> use that same User Function in a VB application. Is there a way to
convert
> the User Function to a Stored Procedure?
> I'm pretty green about this, so any help is appreciated.
> tia,
> --
> JMorrell
|||Thanks for the reply. We're needing the results of the function within vb.
The function is as follows:
CREATE FUNCTION ufn_GetSubtree
(
@.supervisorid AS int
)
RETURNS @.tree table
(
employeeid varchar(6)NOT NULL,
supervisorid varchar(6)NULL,
lname varchar(25) NOT NULL,
fname varchar(25) not null,
term int not null,
lvl int NOT NULL,
path varchar(900) NOT NULL
)
AS
BEGIN
DECLARE @.lvl AS int, @.path AS varchar(900)
SELECT @.lvl = 0, @.path = '.'
INSERT INTO @.tree
SELECT employeeid, supervisorid, lname, fname, term,
@.lvl, '.' + CAST(employeeid AS varchar(10)) + '.'
FROM tblemp
WHERE term = 0
and employeeid = @.supervisorid
WHILE @.@.ROWCOUNT > 0
BEGIN
SET @.lvl = @.lvl + 1
INSERT INTO @.tree
SELECT E.employeeid, E.supervisorid, E.lname, E.fname, E.term,
@.lvl, T.path + CAST(E.employeeid AS varchar(10)) + '.'
FROM tblEmp AS E JOIN @.tree AS T
ON E.supervisorid = T.employeeid AND T.lvl = @.lvl - 1
END
RETURN
END
I'm not sure how to go about using the function as is when in vb. Is there
a source for help in this?
tiaa,
JMorrell
"Aaron [SQL Server MVP]" wrote:

> Why? IIRC, you can call a function from VB (we do that occasionally from C#
> and VB.Net).
> Anyway, we can't help you convert the function to a procedure unless you
> show us what the function does (!).
> --
> http://www.aspfaq.com/
> (Reverse address to reply.)
>
>
> "JMorrell" <JMorrell@.discussions.microsoft.com> wrote in message
> news:DE9B9D49-E776-4140-AFAA-98E606B4A2DC@.microsoft.com...
> to
> convert
>
>
|||(a) I suggest CREATE FUNCTION dbo.ufn_GetSubTree
(b) this would be easy to convert to a stored procedure (though I'm sure
Celko will want to re-educate you on the right way to handle trees, nested
sets and heirarchies etc.).
(c) try this in VB:
dim conn as adodb.connection
conn.open "your connection string"
dim rs as adodb.recordset
rs.open "SELECT * FROM dbo.ufn_GetSubTree"
...
http://www.aspfaq.com/
(Reverse address to reply.)
"JMorrell" <JMorrell@.discussions.microsoft.com> wrote in message
news:FECA6975-BE71-4CAB-8107-67598D6C921D@.microsoft.com...
> Thanks for the reply. We're needing the results of the function within
vb.
> The function is as follows:
> CREATE FUNCTION ufn_GetSubtree
> (
> @.supervisorid AS int
> )
> RETURNS @.tree table
> (
> employeeid varchar(6) NOT NULL,
> supervisorid varchar(6) NULL,
> lname varchar(25) NOT NULL,
> fname varchar(25) not null,
> term int not null,
> lvl int NOT NULL,
> path varchar(900) NOT NULL
> )
> AS
> BEGIN
> DECLARE @.lvl AS int, @.path AS varchar(900)
> SELECT @.lvl = 0, @.path = '.'
> INSERT INTO @.tree
> SELECT employeeid, supervisorid, lname, fname, term,
> @.lvl, '.' + CAST(employeeid AS varchar(10)) + '.'
> FROM tblemp
> WHERE term = 0
> and employeeid = @.supervisorid
> WHILE @.@.ROWCOUNT > 0
> BEGIN
> SET @.lvl = @.lvl + 1
> INSERT INTO @.tree
> SELECT E.employeeid, E.supervisorid, E.lname, E.fname, E.term,
> @.lvl, T.path + CAST(E.employeeid AS varchar(10)) + '.'
> FROM tblEmp AS E JOIN @.tree AS T
> ON E.supervisorid = T.employeeid AND T.lvl = @.lvl - 1
> END
> RETURN
> END
> --
> I'm not sure how to go about using the function as is when in vb. Is
there[vbcol=seagreen]
> a source for help in this?
> tiaa,
> JMorrell
> "Aaron [SQL Server MVP]" wrote:
from C#[vbcol=seagreen]
need[vbcol=seagreen]

Convert to SQL Function! Help!

Can u help me transform this code into sql function?

/* Append modulus 11 check digit to supplied string of digits. */
function GenMOD11( $base_val )
{
$result = "";
$weight = array( 2, 3, 4, 5, 6, 7,
2, 3, 4, 5, 6, 7,
2, 3, 4, 5, 6, 7,
2, 3, 4, 5, 6, 7 );

/* For convenience, reverse the string and work left to right. */
$reversed_base_val = strrev( $base_val );
for ( $i = 0, $sum = 0; $i < strlen( $reversed_base_val ); $i++ )
{
/* Calculate product and accumulate. */
$sum += substr( $reversed_base_val, $i, 1 ) * $weight[ $i ];
}

/* Determine check digit, and concatenate to base value. */
$remainder = $sum % 11;
switch ( $remainder )
{
case 0:
$result = $base_val . 0;
break;
case 1:
$result = "n/a";
break;
default:
$check_digit = 11 - $remainder;
$result = $base_val . $check_digit;
break;
}Owh..i forget.. I'm using SQL Server 2000|||moving thread to SQL Server forum|||Can you explain to us what the above function actually does (step by step preferred) and then we can see if we can help! :)|||The reversing confuses me slightly. Is the last digit always multiplied by 2?|||May I contribute:-- ptp 20070806 SQL Server mod-11 function
-- See http://www.dbforums.com/showthread.php?t=1621130 for discussion
-- Note: Mod-11 was once a well known checkdigit algorithm. It was implemented in hardware
-- on the 129 keypunch, and it still used in ISBN and banking applications in 2007

CREATE FUNCTION dbo.fMod11(@.pcFundus VARCHAR(20))
RETURNS VARCHAR(21) AS

BEGIN
DECLARE @.iAccumulator BIGINT -- Accumulator for weighted sum
, @.iDigits INT -- Digit place value
, @.iNoise INT -- Noise characters ignored
, @.cChar CHAR(1) -- Current working character
, @.cResult VARCHAR(21) -- Result value to return
, @.cWork VARCHAR(21) -- Scratch buffer

SET @.cResult = @.pcFundus -- Assume we return what we got
SET @.cWork = Reverse(@.pcFundus) -- Reverse to make string handling simpler
SET @.iAccumulator = 0 -- Accumulator starts at zero
SET @.iDigits = 1 -- 1 is offest for the check digit

WHILE 0 < Len(@.cWork) -- Loop to process all characters
BEGIN
SET @.cChar = Left(@.cWork, 1) -- Current char is leftmost
SET @.cWork = SubString(@.cWork, 2, 21) -- then peel it off the buffer

IF 0 < CharIndex(@.cChar, '0123456789') -- Digit character
BEGIN
SET @.iDigits = 1 + @.iDigits -- bump digit count
SET @.iAccumulator = @.iDigits * (Ascii(@.cChar) - 48) + @.iAccumulator
END
ELSE IF 0 < CharIndex(@.cChar, ' -') -- Defined "noise" character?
SET @.iNoise = 1 + @.iNoise
ELSE -- Garbage, bail out!
BEGIN
SET @.cResult = NULL
SET @.cWork = ''
END
END

RETURN @.cResult + SubString('0123456789XX', 12 - @.iAccumulator % 11, 1)
END
GO
SELECT d1 + d0, dbo.fMod11(d1 + d0) -- Prove that we've got it correct
FROM (
SELECT 0 AS d0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) AS z0
CROSS JOIN (
SELECT 0 AS d1 UNION SELECT 10 UNION SELECT 20 UNION SELECT 30 UNION SELECT 40
UNION SELECT 50 UNION SELECT 60 UNION SELECT 70 UNION SELECT 80 UNION SELECT 90) AS z1
ORDER BY d1 + d0
GO
DROP FUNCTION dbo.fMod11 -- Tidy up after we've played in the sandbox-PatP|||Pat, I don't believe that your @.iDigits does the same thing as massspectrometry's weight array as the string gets longer than 6 characters.
You seem to multiply the 7th number by 8, and mass's function multiplies it by 2.|||I'll conceed that my Transact-SQL function and the PHP function aren't identical.

My algorithm implements Mod-11 as it is used for ISBN, banking, etc. It specifically allows for "noise characters" that are permissible in those uses, and it correctly computes the checksum for a fundus value with a value of zero or a remainder of ten (using an X for the checksum character).

-PatP|||Can you explain to us what the above function actually does (step by step preferred) and then we can see if we can help! :)

Hai..thank you for offering.. I'm trying to make a sql function that will perform similarly like this url http://www.eclectica.ca/howto/modulus-11-self-check.php

I have 7 digits data (SERIALNO), and i want to get the modulus 11 from this 7 digits data..

For example..
SERIALNO || MODULO 11
1000001 || 10000017
1000002 || 10000025
1000003 || 10000033
1000004 || 10000041

Can it be done? How?|||Here is the code with the 5250 bug faithfully re-implemented:-- ptp 20070806 SQL Server implemented of IBM 5250 mod-11 function
-- See http://www.dbforums.com/showthread.php?t=1621130 for discussion
-- Note: Mod-11 was once a well known checkdigit algorithm. A derivative of mod-11
-- was implemented in hardware on the 5250 terminal

CREATE FUNCTION dbo.fMod11(@.pcFundus VARCHAR(20))
RETURNS VARCHAR(21) AS

BEGIN
DECLARE @.iAccumulator BIGINT -- Accumulator for weighted sum
, @.iDigits INT -- Digit place value
, @.iNoise INT -- Noise characters ignored
, @.cChar CHAR(1) -- Current working character
, @.cResult VARCHAR(21) -- Result value to return
, @.cWork VARCHAR(21) -- Scratch buffer

SET @.cResult = @.pcFundus -- Assume we return what we got
SET @.cWork = Reverse(@.pcFundus) -- Reverse to make string handling simpler
SET @.iAccumulator = 0 -- Accumulator starts at zero
SET @.iDigits = 1 -- 1 is offest for the check digit

WHILE 0 < Len(@.cWork) -- Loop to process all characters
BEGIN
SET @.cChar = Left(@.cWork, 1) -- Current char is leftmost
SET @.cWork = SubString(@.cWork, 2, 21) -- then peel it off the buffer

IF 0 < CharIndex(@.cChar, '0123456789') -- Digit character
BEGIN
SET @.iDigits = -- Next 5250 digit weight
CASE
WHEN 7 = @.iDigits THEN 2
ELSE 1 + @.iDigits
END
SET @.iAccumulator = @.iDigits * (Ascii(@.cChar) - 48) + @.iAccumulator
END
ELSE IF 0 < CharIndex(@.cChar, ' -') -- Defined "noise" character?
SET @.iNoise = 1 + @.iNoise
ELSE -- Garbage, bail out!
BEGIN
SET @.cResult = NULL
SET @.cWork = ''
END
END

RETURN @.cResult + SubString('0123456789XX', 12 - @.iAccumulator % 11, 1)
END
GO
SELECT dbo.fMod11('100000' + d)
FROM (SELECT '0' AS d UNION SELECT '1' UNION SELECT '2' UNION SELECT '3' UNION SELECT '4' UNION SELECT '5') AS z

SELECT d1 + d0, dbo.fMod11(d1 + d0) -- Prove that we've got it correct
FROM (
SELECT 0 AS d0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) AS z0
CROSS JOIN (
SELECT 0 AS d1 UNION SELECT 10 UNION SELECT 20 UNION SELECT 30 UNION SELECT 40
UNION SELECT 50 UNION SELECT 60 UNION SELECT 70 UNION SELECT 80 UNION SELECT 90) AS z1
ORDER BY d1 + d0
GO
DROP FUNCTION dbo.fMod11 -- Tidy up after we've played in the sandbox-PatP|||Erm.. i feel really shy of asking this.. how to call the function? (embarrassed)|||See the sample code at the end of the snippet that I posted. Your test is in there.

-PatP|||Thank you pat phelan.. in the code here :

SELECT dbo.fMod11('100000' + d)
FROM (SELECT '0' AS d UNION SELECT '1' UNION SELECT '2' UNION SELECT '3' UNION SELECT '4' UNION SELECT '5') AS z

For example if i have 100,000 data.. starting from 1000001 until 1100000.. How am i to code it? Is it one by one?|||FYI - http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=87357|||None of the code or links worked for my situation. I'm posting what I hope is a generic mod 11 user defined function. I'm sure there are improvements that could be made to it, but it works for my need (10 numeric digits, last digit being a mod11 check digit).

/*
Checks if a number passes the Mod11 checksum algorithm.

Input:
@.NumberToCheck = the number to check for validity (last digit is the check digit)

Returns:
1 = the number provided passes the Mod11 checksum algorithm
0 = the number provided does not pass the Mod11 checksum algorithm
*/

CREATE Function dbo.ufn_Mod11(@.NumberToCheck as varchar(10))
Returns bit As
Begin
/*
-- FOR TESTING --------
Declare @.NumberToCheck as varchar(10)
Set @.NumberToCheck = '7830834260'
Set @.NumberToCheck = '7806812519'
-- FOR TESTING --------
*/
Declare @.CheckDigit int
Declare @.Counter int
Declare @.IsValid bit
Declare @.Product int
Declare @.Sum int

-- Assume it is not valid.
Set @.IsValid = 0

-- Number must not be null, must be numeric, must be ten digits.
If @.NumberToCheck Is Not Null And IsNumeric(@.NumberToCheck) = 1 And Len(@.NumberToCheck) = 10
Begin
Set @.Counter = 1
Set @.Sum = 0

-- Reverse the number being checked.
Set @.NumberToCheck = Reverse(@.NumberToCheck)

-- Iterate through all digits except the last digit.
While @.Counter <= Len(@.NumberToCheck)
Begin
-- Multiply the digit by its position, starting with the second one.
If @.Counter > 1
Begin
Set @.Product = SubString(@.NumberToCheck, @.Counter, 1)

Set @.Product = @.Product * @.Counter

-- Sum the current product.
Set @.Sum = @.Sum + @.Product
End

Set @.Counter = @.Counter + 1
End

Set @.CheckDigit = @.Sum % 11

-- If the check digit is ten, just set it to zero.
If @.CheckDigit = 10
Begin
Set @.CheckDigit = 0
End

-- Compare the calculated check digit to the original last digit,
-- which is now the first since it was reversed.
If @.CheckDigit = Left(@.NumberToCheck, 1)
Begin
Set @.IsValid = 1
End
End

Return @.IsValid
End

Sunday, March 11, 2012

Convert Time Zone:

Convert Time Zone:

I have 05/02/2007 10:00AM CST, how can I convert this to EST in SQL Server 2000 function?

Code Snippet

--USAGE:

select dbo.udf_ConvertTime('January 1, 2005 12:00 PM','PDT','EST')

go

create function udf_ConvertTime(

@.TimeToConvert datetime

,@.TimeZoneFrom varchar(4)

,@.TimeZoneTo varchar(4)

)

returns DateTime

as

begin

declare @.dtOutput datetime, @.nAdjust smallint

declare @.temp table(TimeZone varchar(4),nOffset smallint)

insert into @.Temp select 'A',60

insert into @.Temp select 'ACDT',630

insert into @.Temp select 'ACST',570

insert into @.Temp select 'ADT',-180

insert into @.Temp select 'AEDT',660

insert into @.Temp select 'AEST',600

insert into @.Temp select 'AKDT',-480

insert into @.Temp select 'AKST',-540

insert into @.Temp select 'AST',-240

insert into @.Temp select 'AWDT',540

insert into @.Temp select 'AWST',480

insert into @.Temp select 'B',120

insert into @.Temp select 'BST',60

insert into @.Temp select 'C',180

insert into @.Temp select 'CDT',-300

insert into @.Temp select 'CEDT',120

insert into @.Temp select 'CEST',120

insert into @.Temp select 'CET',60

insert into @.Temp select 'CST',630

insert into @.Temp select 'CST',570

insert into @.Temp select 'CST',-360

insert into @.Temp select 'CXT',420

insert into @.Temp select 'D',240

insert into @.Temp select 'E',300

insert into @.Temp select 'EDT',-240

insert into @.Temp select 'EEDT',180

insert into @.Temp select 'EEST',180

insert into @.Temp select 'EET',120

insert into @.Temp select 'EST',660

insert into @.Temp select 'EST',600

insert into @.Temp select 'EST',-300

insert into @.Temp select 'F',360

insert into @.Temp select 'G',420

insert into @.Temp select 'GMT',0

insert into @.Temp select 'H',480

insert into @.Temp select 'HAA',-180

insert into @.Temp select 'HAC',-300

insert into @.Temp select 'HADT',-540

insert into @.Temp select 'HAE',-240

insert into @.Temp select 'HAP',-420

insert into @.Temp select 'HAR',-360

insert into @.Temp select 'HAST',-600

insert into @.Temp select 'HAT',-150

insert into @.Temp select 'HAY',-480

insert into @.Temp select 'HNA',-240

insert into @.Temp select 'HNC',-360

insert into @.Temp select 'HNE',-300

insert into @.Temp select 'HNP',-480

insert into @.Temp select 'HNR',-420

insert into @.Temp select 'HNT',-210

insert into @.Temp select 'HNY',-540

insert into @.Temp select 'I',540

insert into @.Temp select 'IST',60

insert into @.Temp select 'K',600

insert into @.Temp select 'L',660

insert into @.Temp select 'M',720

insert into @.Temp select 'MDT',-360

insert into @.Temp select 'MESZ',120

insert into @.Temp select 'MEZ',60

insert into @.Temp select 'MST',-420

insert into @.Temp select 'N',-60

insert into @.Temp select 'NDT',-150

insert into @.Temp select 'NFT',690

insert into @.Temp select 'NST',-210

insert into @.Temp select 'O',-120

insert into @.Temp select 'P',-180

insert into @.Temp select 'PDT',-420

insert into @.Temp select 'PST',-480

insert into @.Temp select 'Q',-240

insert into @.Temp select 'R',-300

insert into @.Temp select 'S',-360

insert into @.Temp select 'T',-420

insert into @.Temp select 'U',-480

insert into @.Temp select 'UTC',0

insert into @.Temp select 'V',-540

insert into @.Temp select 'W',-600

insert into @.Temp select 'WEDT',60

insert into @.Temp select 'WEST',60

insert into @.Temp select 'WET',0

insert into @.Temp select 'WST',540

insert into @.Temp select 'WST',480

insert into @.Temp select 'X',-660

insert into @.Temp select 'Y',-720

insert into @.Temp select 'Z',0

select @.nAdjust = nOffset

from @.Temp

where timeZone = @.TimeZoneFrom

select @.nAdjust = @.nAdjust - nOffset

from @.Temp

where timeZone = @.TimeZoneTo

set @.dtOutput = dateadd(n,@.nAdjust,@.TimeToConvert)

return @.dtOutput

end

|||

Thanks you for your response. Can you give me some explanation about the nOffset, what is the logic behind these numbers and if this would work for daylight saving time period?

|||

Use DATEADD()

SELECT dateadd( hour, -1, CSTColumn ) AS 'EST'

FROM MyTable

|||

nOffset is the "minutes from GMT" for each time zone. I convert to minutes because world-wide, there are several time zones that are x Hours and y Minutes from GMT. So, I've converted everything to minutes.

EST is -300 minutes from GMT ( -5:00)

EDT is -240 Minutes from GMT ( -4:00)

You have to already know whether your source time is Daylight time or not. (EDT is different than EST).

So, you pass in a "Source Time" and a Source Time Zone and a Target Time Zone.

Convert Time

Hi,

Hopefully this is an easy one. I need a function to call from my page footer that takes the NOW date/time value and converts it from Eastern Standard Time to Mountain Standard Time. (My server is on EST time and my users are all on MST time) Of course the function needs to handle daylight savings as well.

thanks!

Why don't you just subtract two hours from the NOW date/time value to get MST.....

Regardless of where your server is, you should be able to convert the time value to any timezone in the world.

|||

That would work until daylight savings...then it's 3 hours difference. I was really hoping to get a custom function from someone...Thx

|||

So you're saying that NOW doesn't adjust for daylight savings?

|||

I'm saying the following:

If NOW is 2pm EST then MST is 12PM ( subtracting 2 hours from EST will work)

When daylight savings kicks in then NOW is 3pm EST and MST is still 12pm (subtracting 2 hours from EST give me 1pm which is incorrect.)

When I say MST I mean arizona time which does not change for daylight savings.

Hope that makes sense..

|||

Yes that makes sense. After a brief research of the forums and google, I've turned up empty handed. Hopefully someone else knows how to do it.

I am assuming that you can't simply change your server's time to MST.

|||

R.K.S. wrote:

I'm saying the following:

If NOW is 2pm EST then MST is 12PM ( subtracting 2 hours from EST will work)

When daylight savings kicks in then NOW is 3pm EST and MST is still 12pm (subtracting 2 hours from EST give me 1pm which is incorrect.)

When I say MST I mean arizona time which does not change for daylight savings.

Hope that makes sense..

You are not looking for DateTime but TimeZone there are only work around solutions but coming in VS2008 try the thread below for your options.

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1860968&SiteID=1

|||

Thanks Caddre, Not the answer i was hoping for but at least i know i wasn't overlooking something simple.

What about this: I already have a function written in C# that will do the conversion. Can't i call a custom function like this in a page footer? Can the custom function exist in an assembly or in the custom code window...

Thx

|||

The Report properties includes code box what I don't know is if you can run it in a footer, you could also check the ReportViewer control it may have a way to use .NET code much easier than normal reports.

|||

If anyone is interested you can use this function to convert to whatever local time your users are on (assuming all your users are on a single timezone) So below, my server is EST and my users are all on MST

Public Shared Function AdjustDateTime(ByVal dt as DateTime)

Dim tz as System.TimeZone
tz = System.TimeZone.CurrentTimeZone

Dim utc as System.DateTime
utc = tz.ToUniversalTime(dt)

'Convert EST to MST
if Not tz.IsDaylightSavingTime(dt) then
AdjustDateTime =utc.ToLocalTime().AddHours(-2)
else
AdjustDateTime =utc.ToLocalTime().AddHours(-3)
end if

End Function