473,401 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Removing 1 hour from text field

Hello,
I have an SQL Server table with a (text) field in which there is
a data value in this format:
200802290525

I need to build an update query the modifies this value
subtracting 1 (one) hour from this value (if the hour is
00:30 am the day is to be decreased and so on)

How can I sipmly accomplish to this job?

Thanks!!!
Mar 5 '08 #1
5 1581
The problem just illustrates why dates should not be kept as text strings.
Correct calculations with dates can be done easily only when the data type
is DATETIME/SMALLDATETIME.

Here is one way to do the 1 hour subtraction:

SELECT DATEADD(hh,
- 1,
LEFT(d, 8) +
CONVERT(DATETIME, LEFT(RIGHT(d, 4), 2) + ':' +
RIGHT(RIGHT(d, 4), 2),
108))
FROM (SELECT '200802290525' UNION
SELECT '200802290030') AS T(d)

And then it gets even more messy converting back to string format:

SELECT REPLACE(
REPLACE(
REPLACE(
CONVERT(VARCHAR(16),
DATEADD(hh,
- 1,
LEFT(d, 8) +
CONVERT(DATETIME, LEFT(RIGHT(d, 4), 2) + ':' +
RIGHT(RIGHT(d, 4), 2),
108)),
126),
'-', ''),
'T', ''),
':', '')
FROM (SELECT '200802290525' UNION
SELECT '200802290030') AS T(d)

HTH,

Plamen Ratchev
http://www.SQLStudio.com

Mar 5 '08 #2
On 5 Mar, 14:39, "Plamen Ratchev" <Pla...@SQLStudio.comwrote:
The problem just illustrates why dates should not be kept as text strings.
Yes, I know but the problem is that data are "bulk inserted" from text
files
and we have (several) thousand of lines to import every day, but we
have to shift some old data
back of an hour: is a single step operation...
What about building a custom function or a CLR function to accomplish
this job? Do you think it's a good way to proceed?

Thank for your interest and compliments for your site.. I like it...
Maury
Mar 5 '08 #3
In my opinion it is well worth to explore and test a CLR solution. T-SQL is
not best with string manipulations and CLR can give you good performance,
plus the more powerful .NET formatting functions will make it easier.

HTH,

Plamen Ratchev
http://www.SQLStudio.com

Mar 5 '08 #4
maury (ma**************@gmail.com) writes:
What about building a custom function or a CLR function to accomplish
this job? Do you think it's a good way to proceed?
Yes, putting the logic to handle this string in a CLR function is a
good idea. That's exactly the thing that the CLR is good for: complex
operations without data access. The CLR function is likely to perform
a lot better than Plamen's T-SQL expression.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Mar 5 '08 #5
Try this in a query ;) ... see MSDN docs to verify:

declare @mydate datetime

set @mydate = cast('3/25/2008' as datetime)

print DATEADD(hour, -1, @mydate)

print DATEADD(second, -3821, @mydate)

Try and add some time to your date too... and change hour to day or day to
second or second to minute or minute to month and see what happens.

:)

Marc Noon

"maury" <ma**************@gmail.comwrote in message
news:c6**********************************@s13g2000 prd.googlegroups.com...
Hello,
I have an SQL Server table with a (text) field in which there is
a data value in this format:
200802290525

I need to build an update query the modifies this value
subtracting 1 (one) hour from this value (if the hour is
00:30 am the day is to be decreased and so on)

How can I sipmly accomplish to this job?

Thanks!!!


--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDem
Mar 26 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jonathan Daggar | last post by:
Hello, I'm trying to put together a form with a very tight table formatting. However, every time I put an text-type input field in, the browser pads the area to the right of it with space. I've...
1
by: Lauren Wilson | last post by:
Is it possible to create a data input mask for a text box bound to a date/time field so that it will display and guide entry of the time values in 24 hour (military) time format?
6
by: TORQUE | last post by:
Hi, Can anyone tell me how to remove or change the automatic rounding of numbers in a box. The box has already been tested with General Number, Fixed, and Standard Formatting with the decimal...
3
by: Shawn Yates | last post by:
I have a database that I am using as a Time clock where employees can clock in and out. Is there a way to make it so that when they clock out a form would open displaying their work day hour by...
8
by: Ryan | last post by:
Hello, I'm new to Access and DB's in general. I've taken over some light duty support for a lab information system we use in house. Most of our dates are reported as "10/31/2006 12:30:00 PM"...
1
by: nyc10011 | last post by:
I am trying to figure out how to remove a dynamic text field from the stage that plays captions over an .flv from an XML file. I would like to find out how to remove the field before a second .flv...
4
by: sparks | last post by:
Some of the people are requiring a personID to be in the records and of course formatted as 001 002....etc so its a text field. Now they are also skipping this field and putting in stuff and just...
20
omerbutt
by: omerbutt | last post by:
hi there i am making an application in which i have to populate columns that consist of some textfields and some input boxes the problem is at the mozilla's end, it creates a new node and appends the...
5
by: hassanhundal007 | last post by:
In my table the field of DateTime name is TIN. In this field DATE and TIME are shown for date I pass this query: SELECT DAY(TIN) FROM SHIFT WHERE SHID = 1 Then the result is "10". So the same I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.