Please Help! Converting From Unix Time

Nov 1, 1999

I have a date field from a Unix machine (ex. 917478174). I need to convert this date to 1/1/1999.

Any help?

Thanks!

-aw

View 1 Replies


ADVERTISEMENT

UNIX Time Conversion Functions

May 28, 2006

A common problem in moving data between SQL Server and UNIX systems is converting to/from the SQL Server datetime format to the UNIX time format.

There are several UNIX time formats, but the most common is a signed 32-bit integer that represents time as the number of seconds between 1970-01-01 00:00:00 and a given time. The functions in the script can be use to convert between SQL Server datetime and this UNIX time format.

For more information on UNIX Time, please read this link:
http://en.wikipedia.org/wiki/Unix_time


For more information about SQL Server date/time conversions, refer to this link:
Date/Time Info and Script Links
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=64762



The conversion of UNIX Time to SQL Server datetime is fairly trivial using the SQL Server DATEADD function, and this is the logic used by the F_UNIX_TIME_TO_DATETIME function in the script:

declare @UNIX_TIME int
select @UNIX_TIME = 1111111111
-- Using dateadd to add seconds to 1970-01-01
select [Datetime from UNIX Time] = dateadd(ss,@UNIX_TIME,'1970-01-01')

Results:

Datetime from UNIX Time
------------------------------------------------------
2005-03-18 01:58:31.000

(1 row(s) affected)


The conversion of SQL Server datetime to UNIX Time is more complex. SQL Server datetime is accurate to milliseconds so is necessary to either truncate or round off the time to a whole second. The function in the F_DATETIME_TO_UNIX_TIME script rounds the time down if milliseconds is less than 500 and up otherwise. A second problem is that UNIX Time is an integer, so it can only represent time from 1901-12-13 20:45:52 through 2038-01-19 03:14:07. The range of SQL Server datetime is 1753-01-01 through 9999-12-31, so the function in the script has logic to return a NULL if the datetime is outside the valid UNIX Time range. Another minor issue is that the SQL Server DATEDIFF function will not cover the full range of an integer value with seconds, so it is necessary to have additional logic in the function do cover the time from 1901-12-13 20:45:52 to 1901-12-14 00:00:00.

The function names created by this script are:
dbo.F_DATETIME_TO_UNIX_TIME( @DAY )
dbo.F_UNIX_TIME_TO_DATETIME( @UNIX_TIME )

The script also includes code to test and demo the functions.




if objectproperty(object_id('dbo.F_DATETIME_TO_UNIX_TIME'),'IsScalarFunction') = 1
begin drop function dbo.F_DATETIME_TO_UNIX_TIME end
go
create function dbo.F_DATETIME_TO_UNIX_TIME
( @DAY datetime )
returns int
as
/*
Function: F_DATETIME_TO_UNIX_TIME

Finds UNIX time as the difference in seconds between
1970-01-01 00:00:00 and input parameter @DAY after
rounding @DAY to the neareast whoie second.

Valid datetime range is 1901-12-13 20:45:51.500 through
2038-01-19 03:14:07.497. This range is limited to the smallest
through the largest possible integer.

Datetimes outside this range will return null.
*/
begin
declare @wkdt datetime

-- Return null if outside of valid UNIX Time range
if @DAY < '1901-12-13 20:45:51.500' or @DAY > '2038-01-19 03:14:07.497'
return null

-- Round off datetime to nearest whole second
select @wkdt = dateadd(ms,round(datepart(ms,@DAY),-3)-datepart(ms,@DAY),@DAY)

-- If date GE 1901-12-14
if @wkdt >= 712return datediff(ss,25567,@wkdt)

-- Handles time GE '1901-12-13 20:45:52.000 and LT 1901-12-14
return -2147472000-datediff(ss,@wkdt,712)

end
go
if objectproperty(object_id('dbo.F_UNIX_TIME_TO_DATETIME'),'IsScalarFunction') = 1
begin drop function dbo.F_UNIX_TIME_TO_DATETIME end
go
create function dbo.F_UNIX_TIME_TO_DATETIME
( @UNIX_TIME int )
returns datetime
as
/*
Function: F_UNIX_TIME_TO_DATETIME

Converts UNIX time represented as the difference
in seconds between 1970-01-01 00:00:00 to a datetime.

Any valid integer -2,147,483,648 through 2,147,483,647
can be converted to datetime.
*/
begin

return dateadd(ss,@UNIX_TIME,25567)

end
go

go
/*
Demo functions F_DATETIME_TO_UNIX_TIME and
F_UNIX_TIME_TO_DATETIME by converting a datetime
to UNIX time and back to datetime.
*/

select
[Input Datetime] = convert(varchar(23),DT,121),
[UNIX Time] = dbo. F_DATETIME_TO_UNIX_TIME(a.dt),
[Datetime from UNIX Time] =
-- Convert datetime to UNIX time an back to Datetime
convert(varchar(23),
dbo. F_UNIX_TIME_TO_DATETIME(dbo. F_DATETIME_TO_UNIX_TIME(a.dt)),121),
Note = .a.note
from
(
selectDT = getdate(),
Note = 'Current date'
union all
selectDT = dateadd(ms,500,getdate()),
Note = 'Current date + 500 ms'
union all
selectDT = dateadd(ms,750,getdate()),
Note = 'Current date + 750 ms'
union all
selectDT = '1901-12-13 20:45:51.500',
Note = 'Earliest datetime function can convert'
union all
selectDT = '2038-01-19 03:14:07.497',
Note = 'Last datetime function can convert'
union all
selectDT = '2001-09-09 01:46:40',
Note ='UNIX time 1000000000'
union all
selectDT = '2005-03-18 01:58:31',
Note = 'UNIX time 1111111111'
union all
selectDT = '2009-02-13 23:31:30',
Note ='UNIX time 1234567890'
union all
selectDT = '1901-12-14 00:00:00.000',
Note = 'Date time dateadd second limit'
union all
selectDT = '1901-12-13 23:59:59.000',
Note = 'Date time dateadd outside second limit'
union all
selectDT = '1901-12-13 20:45:51.497',
Note = 'Date time function cannot convert - low end'
union all
select
DT = '2038-01-19 03:14:07.500',
Note = 'Date time function cannot convert - high end'
union all
select
DT = '1753-01-01 00:00:00.000',
Note = 'Min Datetime'
union all
select
DT = '9999-12-31 23:59:59.997',
Note = 'Max Datetime'
) a


Results:

Input Datetime UNIX Time Datetime from UNIX Time Note
----------------------- ----------- ----------------------- --------------------------------------------
2006-05-29 23:34:11.517 1148945652 2006-05-29 23:34:12.000 Current date
2006-05-29 23:34:12.017 1148945652 2006-05-29 23:34:12.000 Current date + 500 ms
2006-05-29 23:34:12.267 1148945652 2006-05-29 23:34:12.000 Current date + 750 ms
1901-12-13 20:45:51.500 -2147483648 1901-12-13 20:45:52.000 Earliest datetime function can convert
2038-01-19 03:14:07.497 2147483647 2038-01-19 03:14:07.000 Last datetime function can convert
2001-09-09 01:46:40.000 1000000000 2001-09-09 01:46:40.000 UNIX time 1000000000
2005-03-18 01:58:31.000 1111111111 2005-03-18 01:58:31.000 UNIX time 1111111111
2009-02-13 23:31:30.000 1234567890 2009-02-13 23:31:30.000 UNIX time 1234567890
1901-12-14 00:00:00.000 -2147472000 1901-12-14 00:00:00.000 Date time dateadd second limit
1901-12-13 23:59:59.000 -2147472001 1901-12-13 23:59:59.000 Date time dateadd outside second limit
1901-12-13 20:45:51.497 NULL NULL Date time function cannot convert - low end
2038-01-19 03:14:07.500 NULL NULL Date time function cannot convert - high end
1753-01-01 00:00:00.000 NULL NULL Min Datetime
9999-12-31 23:59:59.997 NULL NULL Max Datetime

(14 row(s) affected)




Edit: Fixed minor bug that caused an overflow, instead of returning NULL, if input to function F_DATETIME_TO_UNIX_TIME was >= 9999-12-31 23:59:59.500.


CODO ERGO SUM

View 5 Replies View Related

Unix, Windows For Unix, And Direct Read Of .mdf File This Is A True Story

Sep 13, 2007

First, let me start by saying I know the answer to this, but due to politics any answer I give will be viewed with some disdain and disbelief.

A Unix/Network [rtdpmin one our sister agencies is trying to solve a problem that really does not need to be solved, but ....

The sister agency uses Information Builder's WebFocus on Unix. When they try to read my data warehouse in SQL Server 2000 it times out on them (on their end and they claim they cannot fix it) (This is using the supplied WebFocus ODBC/JDBC driver for SQL Server). This has lead to accusations of us not letting them read the data.

After much gnashing of teeth over "if you do data and data analysis for a living, why do I have to show you how to set-up an ODBC connection" we have shown them how to connect to the data, read it, and transfer it using MS-ACCESS, EXCEL, and more importantly SPSS. This is not good enough because they cannot figure out how to put it into WebFocus.

Their network person has come-up with the brilliant idea of reading the .mdf directly by installing Windows for Unix on the Unix side and then just pointing to the .mdf file and "reading it ".

Unless I have missed something somwhere (I will admit possible), I know you need a driver to read MS-SQL and that driver in every example I have seen could care less about the physical location of the .mdf file. It wants to know what database, what server (ip or name) and what security/login to use.

Could someone give me a more "technical answer" or even Microsoft's party line so I do not have this person mucking around with my production server trying to acomplish the impossible.

View 5 Replies View Related

Transact SQL :: Converting 24hrs Time To 12 Hours Time Query Format?

Apr 21, 2015

SELECT 
    CONVERT(VARCHAR(10),attnc_chkin_dt,101) as INDATE,
    CONVERT(VARCHAR(10),attnc_chkin_dt,108) as TimePart
FROM pmt_attendance

o/p
indate   04/18/2015
time part :17:45:00

I need to convert this 17:45:00 to 12 hours date format...

View 8 Replies View Related

SQL 2012 :: Converting Time String In Temp Table To Military Time Then Cast As Integer?

Dec 26, 2014

I need to take a temporary table that has various times stored in a text field (4:30 pm, 11:00 am, 5:30 pm, etc.), convert it to miltary time then cast it as an integer with an update statement kind of like:

Update myTable set MovieTime = REPLACE(CONVERT(CHAR(5),GETDATE(),108), ':', '')

how this can be done while my temp table is in session?

View 2 Replies View Related

How Do I Convert A Unix Date/Time Field To A Date When The The SQL DB Stores That Data As Char 11?

Nov 13, 2007

Hi there.
I'm trying to extract data from my SQL server & everything in the script I've got is working (extracting correct data) except for one field - which is for the most part it's off by +2 days (on a few occasions - I see it off by just +1 day or even +3, but it's usually the +2 days).

I'm told that it's due to the conversion formula - but - since SQL is not my native language, I'm at a bit of a loss.

The DB table has the date field stored as a type: CHAR (as opposed to 'DATE')
Can anyone out there help?

Please advise. Thanks.

Best.
K7

View 1 Replies View Related

Converting US Time To IST

Nov 28, 2007

Do we have any function that will convert US time(as in the database) to IST in the select query.

View 1 Replies View Related

Converting SQL Internal Time

May 30, 2000

I am trying to create custom reports with Access by linking to SQL tables in the SMS 2.0 software metering database. The only problem that I am having is trying to convert the internal SQL time/date nine digit format for when programs started or ended to a standard format. Is there a special function within either SQL or Access that will do this? Thanks.

View 2 Replies View Related

Converting Time Format

Jun 14, 2007

Dear all,

i have one table that colum is
164020 ---- that on convert into hh:mm:ss like 16:40:20

advance thanks

View 2 Replies View Related

HELP HELP Converting Date Time

May 30, 2002

I have a table that has a nvarchar field of (12) I need to convert this to a smalldatetimefield.

I get the following message

Error Source: Microsoft Data Transformation Services (DTS) Data Pump
Error Description:Insert error, column 1 ('timeid', DBTYPE_DBTIMESTAMP), status 6: Data overflow.
Error Help File:sqldts80.hlp
Error Help Context ID:30702


Can someone please tell me how to convert this field without getting this message?????

Thanks,
Dianne

View 4 Replies View Related

Converting Decimal To Time

Nov 18, 2005

how do you convert a numeric to time format if it shows hours but a decimal figure for Minutes. For example if I have hours in decimal format like this

28.5000

but I want to show it in this format:

28:30:00

where 28 = hours, 30 = minutes, 00 = seconds

Thanks.

View 4 Replies View Related

Converting Decimal Time To HH:MM

Apr 28, 2008

In Reporting Services, I have a decimal time field (18,2) in my report which I wish to display as HH:MM. Similarly, I req a sum of total hours and minutes as a summary. Whats the expression for doing this. I've googled far and wide and cannot find a definitve solution.

Thanks.

View 2 Replies View Related

Converting Decimal Value To Time

Mar 3, 2008

I have a field that currently is displaying time in decimal form. I would like to convert it to time format. For example: when it displays 6.46 I would like it to be converted to 00:06:27. Can I do this within reporting services?

View 8 Replies View Related

Error Converting Date Time

Apr 26, 2004

Hi i m tring to convert a date time

declare @a datetime
declare @b varchar(10)
set @b='26/04/2004'
set @a= Convert(datetime, @b)

but it gives me this error:

Server: Msg 242, Level 16, State 3, Line 5
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.


I believe is my date format..i want to know how to make sure that i am the dd/mm/yyyy format is correct way?

View 3 Replies View Related

Slow Response Time After Converting DB From SQL 6.5 To 7.0

Feb 10, 2000

We did an in place convertion of our data base from MS SQL Server 6.5 to 7.0.
Our application is much slower now on SQL 7.0. Any idea why?
The following is a sample SQL statement that runs quickly on SQL 6.5 and takes a long time on SQL 7.0 I also attached the query plans from SQL 6.5 and 7.0.




SELECT Person_Name.PerNam_Person_Name_PK ,
Person_Name.PerNam_Row_Status ,
Person_Name.PerNam_Last_Name_Sndx ,
Person_Name.PerNam_Last_Name ,
Person_Name.PerNam_Name_Suffix ,
Person_Name.PerNam_First_Name ,
Person_Name.PerNam_Name_Prefix ,
Person_Name.PerNam_Middle_Name ,
Person_Name.PerNam_Event_Person_FK ,
Event.Evn_Event_Nbr ,
Event.Evn_Event_Type ,
Event_Person.EvnPer_Last_Name ,
Event_Person.EvnPer_First_Name ,
Event_Person.EvnPer_Middle_Name ,
Event_Person.EvnPer_Name_Prefix ,
Event_Person.EvnPer_Name_Suffix
FROM Person_Name , Event , Event_Person
WHERE (Person_Name.PerNam_Agency_ID = "CL")
AND ( Person_Name.PerNam_Event_Person_FK = Event_Person.EvnPer_Event_Person_PK )
and ( Event_Person.EvnPer_Event_FK = Event.Evn_Event_PK )
and (Person_Name.PerNam_Person_Name_PK = 0 or ( Person_Name.PerNam_Event_Person_FK = 581541) )
and ( Person_Name.PerNam_Row_Status <> "D" )




Query plan in SQL 6.5

SQL Server Execution Times:
cpu time = 0 ms. elapsed time = 31250 ms.
STEP 1
The type of query is INSERT
The update mode is direct
Worktable created for REFORMATTING
FROM TABLE
Person_Name
Nested iteration
Index : PK_Person_Name
FROM TABLE
Person_Name
Nested iteration
Index : PerNam_Event_Person_FK
FROM TABLE
Person_Name
Nested iteration
Using Dynamic Index
FROM TABLE
Event_Person
Nested iteration
Index : PK_Event_Person
TO TABLE
Worktable 1
STEP 2
The type of query is SELECT
FROM TABLE
Worktable 1
Nested iteration
Table Scan
FROM TABLE
Event
Nested iteration
Index : PK_Event
SQL Server Parse and Compile Time:
cpu time = 0 ms.
Table: Person_Name scan count 2, logical reads: 6, physical reads: 5, read ahead reads: 0
Table: Event scan count 0, logical reads: 0, physical reads: 0, read ahead reads: 0
Table: Event_Person scan count 0, logical reads: 0, physical reads: 0, read ahead reads: 0
Table: Worktable scan count 0, logical reads: 0, physical reads: 0, read ahead reads: 0
Table: Worktable scan count 1, logical reads: 1, physical reads: 0, read ahead reads: 0

SQL Server Execution Times:
cpu time = 0 ms. elapsed time = 62 ms.



Query plan on SQL 7.0

SQL Server parse and compile time:
CPU time = 431 ms, elapsed time = 535 ms.
.................
Table 'Event'. Scan count 1, logical reads 3, physical reads 3, read-ahead reads 0.
Table 'Event_Person'. Scan count 1, logical reads 6, physical reads 5, read-ahead reads 0.
Table 'Person_Name'. Scan count 1, logical reads 4588, physical reads 2, read-ahead reads 4092.

SQL Server Execution Times:
CPU time = 7921 ms, elapsed time = 13519 ms.

SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 13781 ms.

View 2 Replies View Related

Converting Date And / Or Time From Char

Jun 3, 2014

converting date and/or time from character string.Got this error message: "Msg 241, Level 16, State 1, Line 7...Conversion failed when converting date and/or time from character string."

Here's my query:
DECLARE @StartDate AS varchar(30)
DECLARE @EndDate AS varchar(30)
SET @StartDate = (CONVERT(varchar(20),'01-05-2014', 101))
SET @EndDate = (CONVERT(varchar(20),'31-05-2014', 101))

[code]....

View 2 Replies View Related

Transact SQL :: Converting Text Value To Time

Jun 4, 2015

Table contains field defined as text. First 12 characters will be date format 'mm/dd/yyyy'. then the next 3 characters could be:

9A, 12P, 3P, 7A or LTL

I would like to convert '9A' to be 09:00AM, so that I can sort by that converted field.

'12P to be 12:00PM    and so on....

I have tried various form of case with substring and convert commands, but cant' get the solution.  I can not change text field to varchar in table.

View 13 Replies View Related

Converting Date And / Or Time From Character String

Oct 22, 2013

My Query is as shown below

Declare @FROMDATE DATETIME
Declare @THRUDATE DATETIME
Declare @Parm Varchar(250)
set @FROMDATE = '09/25/2013'
set @THRUDATE = '09/28/2013'
set @Parm = 'FROMDATE=' + @FROMDATE + ';THRUDATE=' + @THRUDATE

I am getting an error at above line saying that

Conversion failed when converting date and/or time from character string.

View 8 Replies View Related

Conversion Failed When Converting Date And Or Time

May 14, 2014

I am getting the following error :

conversion failed when converting date and or time from character string

I am using Sql Server 2008.(database designed for sql 2005 later moved to sql server 2008).

Pickup_time and actual_Pickup_time are varchar(5) in database.

What is wrong with this query?

Query:

SELECT COUNT(Trip_ID) AS OntimePickupCount
FROM MyTABLE
WHERE Start_Dt BETWEEN '01/01/2014' AND '04/30/2014'
AND (DateDiff(minute, CAST (Pickup_Time AS time), CAST (Actual_Pickup AS time )) BETWEEN 0 AND 15
OR DateDiff(minute, CAST (Actual_Pickup AS time), CAST (Pickup_Time AS time) ) BETWEEN 0 AND 15)
AND Actual_Pickup IS NOT NULL AND Actual_Dropoff IS NOT NULL

View 4 Replies View Related

Converting Varchar Time HHMM To 24 Hour HH:MM:SS?

Oct 29, 2014

How can I do this? I have a column that is a varchar and times are stored like this:

0600
1240
0145
2335

How can I get those to HH:MM formats? I've tried this but I don't know if it's the best way, plus there are seven 0s after the MM:

convert(time,LEFT(b.status_time,2) +':'+ RIGHT(b.status_time,2))

View 2 Replies View Related

Problem Converting Smalldatetime To 12hr Time Value

Mar 9, 2007

Hi,

I have two smalldatetime fields starttime and endtime

I want to display them like

select convert(varchar,starttime,108) + ' to ' + convert(varchar,endtime,108) from tbTest

the data in the field is

starttime endtime

1/3/2006 9:00:00 1/3/2006 6:00:00



the result I am getting is

09:00:00 to 18:00:00

Where as I want it something like this

9.00 AM to 6.00 PM

Is it possible? what query should be used for this?

View 5 Replies View Related

Converting Datestamp To Separate Date And Time Stamp

Jan 11, 2007

I need to separate the date stamp (which looks like this 2006-10-05 09:08:41.000) into the date in this format 05OCT2006 and then the date stamp separate in this format 09:08. Thanks!

View 2 Replies View Related

SQL Server 2012 :: Inconsistent Results When Converting To Time?

Jun 5, 2014

I have a lot of rows of hours, set up like this: 0745, 0800, 2200, 1145 and so on (varchar(5), for some reason).

These are converted into a smalldatetime like this:

CONVERT(smalldatetime, STUFF(timestarted, 3, 0, ':')) [this would give output like this - 1900-01-01 11:45:00]

This code has been in place for years...and we stick the date on later from another column.

But recently, it's started to fail for some rows, with "The conversion of a varchar data type to a smalldatetime data type resulted in an out-of-range value".

My assumption is that new data being added in is junk. If I query for these values and just list them (rather than adding a column to convert them also) that's fine, of course. I've checked all the stuffed (but not yet converted - so 11:45 rather than 1145) output to see if it ISDATE(), and it is. There are no times with hours > 23 or minutes greater than 59 either.

If I add the CONVERT in, we see the error message. But here's the oddity, if I place all of the rows into a holding table, and retry the conversion, there is no error. It's this last bit that is puzzling me. Plus I can't see any errors in the hours data that would cause a conversion problem.

I've put the whole of this into a cursor to try to trap the error rows too, but all processes fine. Why would it fail if NOT in a cursor?

View 9 Replies View Related

T-SQL (SS2K8) :: Converting Integer Values To Date-time?

Jul 15, 2014

As a DBA, I am working on a project where an ETL process(SSIS) takes a long time to aggregate and process the raw data.

I figured out few things where the package selects the data from my biggest 200 GB unpartitioned table which has a datekey column but the package converts its each row to an integer value leading to massive scans and high CPU.

Example: the package passed two values 20140714 and 4 which means it wants to grab data from my biggest table which belongs between 20140714 04:00:00 and 20140714 05:00:00.

It leads to massive implicit conversions and I am trying to change this.

To minimize the number of changes, what I am trying to do is to convert 20140714 and 4 to a datetime format variable.

Select Convert(DATETIME, LEFT(20170714, 8)) which gives me a date value but I am stuck at appending time(HH:00:00) to it.

View 4 Replies View Related

Converting UTC Datetime Values To Local Time Zones

May 20, 2008



I would like to convert a UTC datetime value to any Local Time Zone. Below is some code I'm trying to ensure works correctly. Anyone care to comment?

I have used the following lines before the function to obtain the 2nd parameter:


SELECT GETUTCDATE()

SELECT GETDATE()

SELECT DATEDIFF(hh,'2008-05-20 20:08:01.020', '2008-05-20 16:08:01.020')

DECLARE @UTC DATETIME

EXECUTE @UTC = UTCtoLocalDate '2008-05-20 20:08:01.020', -4


-- =============================================

-- Description: <Converts local datetime values to UTC datetime values, using the built-in GETUTCDATE() function.

-- <Parameter two (@TZ) represents the time zone difference from UTC/GMT. To obtain this value

-- <run GETUTCDATE() and GETDATE(), then determine the timezone from the difference between the two.>

-- =============================================

CREATE FUNCTION [dbo].[UTCtoLocalDate] (@UTCDate DATETIME, @TZ INT)

RETURNS DATETIME AS

BEGIN

IF ( DATEPART(hh, @UTCDate) <> 0 )

BEGIN

DECLARE @LocalDate DATETIME

DECLARE @UTCDelta INT

DECLARE @thisYear INT

DECLARE @DSTDay INT

DECLARE @NormalDay INT

DECLARE @DSTDate DATETIME

DECLARE @NormalDate DATETIME

SET @thisYear = YEAR(@UTCDate)

IF (@thisYear < 2007 )

BEGIN

SET @DSTDay = ( 2 + 6 * @thisYear - FLOOR(@thisYear / 4) ) % 7 + 1

SET @NormalDay = ( 31 - ( FLOOR( @thisYear * 5 / 4) + 1) % 7)

SET @DSTDate = '4/' + CAST(@DSTDay AS VARCHAR(2)) + '/' + CAST(@thisYear AS VARCHAR(4)) + ' 2:00:00.000 AM'

SET @NormalDate = '10/' + CAST(@NormalDay AS VARCHAR(2)) + '/' + CAST(@thisYear AS VARCHAR(4)) + ' 2:00:00.000 AM'

END

ELSE

BEGIN

SET @DSTDay = ( 14 - ( FLOOR( 1 + @thisYear * 5 / 4 ) ) % 7 )

SET @NormalDay = ( 7 - ( FLOOR ( 1 + @thisYear * 5 / 4) ) % 7 )

SET @DSTDate = '3/' + CAST(@DSTDay AS VARCHAR(2)) + '/' + CAST(@thisYear AS VARCHAR(4)) + ' 2:00:00.000 AM'

SET @NormalDate = '11/' + CAST(@NormalDay AS VARCHAR(2)) + '/' + CAST(@thisYear AS VARCHAR(4)) + ' 2:00:00.000 AM'

END

IF ((@UTCDate > @DSTDate) AND (@UTCDate < @NormalDate))

BEGIN

SET @UTCDelta = @TZ + 1

END

ELSE

BEGIN

SET @UTCDelta = @TZ

END

-- now convert utc date to local date

SET @LocalDate = DATEADD(Hour, @UTCDelta, @UTCDate)

END

ELSE

BEGIN

SET @LocalDate = @UTCDate

END

RETURN(@LocalDate)

END

GO

View 6 Replies View Related

Trouble Converting Nvarchar(10) To Smalldate Time Where ISDATE()=1

Nov 17, 2007



I have a column which is current stored as nvarchar(10), and all of the enteries are either NULL or mm/dd/yyyy.
I am trying to convert the column to smalldatetime using CONVERT or CAST and each time I get an arithmetic overflow error message.
I also tried selecting just the enteries with ISDATE()=1 and converting those to smalldatetime, and still got an arithmetic overflow message.

(I've run ISDATE() several times on the column, and the only rows without ISDATE()=1 are those which have NULL values).

help!

View 3 Replies View Related

Reporting Services :: Converting Hours In Decimal To Time Format Using Expression

Nov 10, 2015

Is it possible to convert for the following SQL statement into SSRS Expression:

SELECT
RIGHT('00' + CONVERT(VARCHAR(2), FLOOR(SUM(Hours))), 2)
+ ':' + RIGHT('00' + CONVERT(VARCHAR(2), FLOOR((SUM(Hours) - FLOOR(SUM(Hours))) * 60)), 2) + ':' + RIGHT('00' + CONVERT(VARCHAR(2),
FLOOR((SUM(Hours) - FLOOR(SUM(Hours))) * 60 - FLOOR((SUM(Hours) - FLOOR(SUM(Hours))) * 60)) * 60), 2)
FROM TableTime

For example I need SSRS expression for converting 1.75 hours into 01:45:00.

View 3 Replies View Related

Integration Services :: Errors Converting Date Time Formats With SSIS

Jul 10, 2015

I am getting below errors when I try to import data from csv format to a sqlserver table.The csv file has date column that has date with format: 7/10/2015  1:18:39 PM and the sql server is using datetime not null for that field in the table.

[OLE DB Destination [90]] Error: SSIS Error Code DTS_E_OLEDBERROR.  An OLE DB error has occurred. Error code: 0x80004005.
An OLE DB record is available.  Source: "Microsoft SQL Server Native Client 10.0"  Hresult: 0x80004005  Description: "Invalid date format".

[OLE DB Destination [90]] Error: There was an error with input column "Date" (138) on input "OLE DB Destination Input" (103). The column status returned was: "Conversion failed because the data value overflowed the specified type.".

View 3 Replies View Related

CURSOR - Conversion Failed When Converting Date And / Or Time From Character String

May 15, 2015

This is my code and I don't know why this error keeps coming out : PS : I did cursor to execute query.Th error showed is bold:

DECLARE RegCreatedDate CURSOR FOR
SELECT DISTINCT (CONVERT(NVARCHAR,CreatedDate,103)) 
FROM CA_Registration WHERE Month(CreatedDate)= @paMonthIn AND YEAR(CreatedDate)=@paYearIn
OPEN RegCreatedDate
FETCH NEXT FROM RegCreatedDate INTO @RegCreatedDate
WHILE @@FETCH_STATUS = 0

[Code] ....

View 9 Replies View Related

Select Only - Conversion Failed When Converting Date And / Or Time From Character String

Sep 4, 2015

I'm trying to select only July from show_held but I keep on getting the error message saying:

Conversion failed when converting date and/or time from character string.

I get error message after I write this code:

ANDshow.show_held = '&July&'

As you can see from the below code, How do I select July from times_held_in_July?

SELECTevent_name,
DATENAME (MONTH, show_held) AS times_held_in_July
FROMevent,
show
WHEREevent.show_id = show.show_id

Result:

event_name times_held_in_July
DressageJuly
Jumping July
Led in July
Led in September
Led in May
DressageApril
DressageJuly
Flag and PoleJuly
SELECTevent_name,
DATENAME (MONTH, show_held) AS times_held_in_July
FROMevent,
show
WHEREevent.show_id = show.show_id
ANDshow.show_held = '&July&'

Result:

Msg 241, Level 16, State 1, Line 24

Conversion failed when converting date and/or time from character string.

View 6 Replies View Related

DB Design :: Conversion Failed When Converting Date And / Or Time From Character String

Sep 14, 2015

BEGIN TRAN;
INSERT INTO [dbo].[QuestManualProcess]
           ([ProcessFromDate]
           ,[LastProcessedFileDateStamp]
           ,[ProcessedOnDate]
           
[code]....

Conversion failed when converting date and/or time from character string/

View 4 Replies View Related

Record Count - Conversion Failed When Converting Date And / Or Time From Character String

Sep 26, 2014

I am trying to write a stored procedure that loops through the list of user tables, gets the record count for each one and write a record to an audit table with DATE, TABLENAME, RECORDCOUNT.I keep getting an error "Conversion failed when converting date and/or time from character string".Here is the script...

DECLARE @table nvarchar(500)
DECLARE @sql nvarchar(520)
DECLARE CursorSelect CURSOR FOR
select table_name from INFORMATION_SCHEMA.tables where table_name not like 'sys%' order by table_name

[code]....

View 2 Replies View Related

Transact SQL :: Error - Conversion Failed When Converting Date And / Or Time From Character String

Nov 16, 2015

I've imported a CSV file into a table in SQL Server 2012. It's a large file, 140,000+ rows, so I couldn't covert it to Excel first to preserve the date format due to Excel's row limit. In the CSV file, there were 3 column with date data in "31-Aug-09" format, and the import automatically transformed these in "31AUG09" format (varchar(50)) in SQL Server.  Now I need to convert these 3 columns from varchar to datetime so I could work with them in date format.

I've tried several things,e.g,

select
convert(datetime,
right(admdate,4)+left(admdate,2)+substring(admdate,3,3))

or

select
convert(datetime,
substring(admdate,6,4)+substring(admdate,1,2)+substring(admdate,3,3))

but keep getting the same error message (Msg 241) "Conversion failed when converting date and/or time from character string".

View 4 Replies View Related







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