Firstly, thanks for all that answered my pleas for help on this SQL statement – all is working really well now.
Weltchy also asked why I was not formatting the date in application code?
I am actually using the SQL code in a stored procedure and then displaying the results in a net2 grid view. Figured that if I could get the stored proc to out put in the correct format that I wanted – then I could reference the proc in other area of my web page.
I did mention that I am not a programmer – so most likely this sis a more efficient way of doing this but right now the following works well for me.
SELECT TOP (5)
NickName AS Chaser,
Speed,
Direction,
Altitude,
CONVERT(char(5), (getutcdate()- gpsTime), 108) AS [Time ago],
CASE TrackerID
WHEN 'Tracker 1' THEN '1'
WHEN 'Tracker 2' THEN '2'
WHEN 'Tracker 3' THEN '3'
WHEN 'Tracker 4' THEN '4'
WHEN 'Tracker 5' THEN '5'
WHEN 'Tracker 6' THEN '6'
WHEN 'Tracker 7' THEN '7'
WHEN 'Tracker 8' THEN '8'
WHEN 'Tracker 9' THEN '9'
WHEN 'Tracker 10' THEN '10'
END AS [GPS]
FROM TrackerData
WHERE DATEDIFF(hour, gpsTime, GETUTCDATE()) < 12
ORDER BY gpsTime DESC
- Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
- Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!
Reply to: Help with MS SQL DateTime Query
Collapse
You are not logged in or you do not have permission to access this page. This could be due to one of several reasons:
- You are not logged in. If you are already registered, fill in the form below to log in, or follow the "Sign Up" link to register a new account.
- You may not have sufficient privileges to access this page. Are you trying to edit someone else's post, access administrative features or some other privileged system?
- If you are trying to post, the administrator may have disabled your account, or it may be awaiting activation.
Logging in...
Previously on "Help with MS SQL DateTime Query"
Collapse
-
Originally posted by MarillionFanYou bunch of bloody SQL amateurs. I put a question up about SQL no-one apart of Socky can answer it, someone puts a simple question up you all smarm over who can answer it.
Winging it, you're all bloody winging it.
Leave a comment:
-
Originally posted by MarillionFanYou bunch of bloody SQL amateurs. I put a question up about SQL no-one apart of Socky can answer it, someone puts a simple question up you all smarm over who can answer it.
Winging it, you're all bloody winging it.
Seriously tho, wtf am I doing working at 7:15am just 3 days before xmas. If ever there was a working condition that says I'm not a bloody employee, this is it!!!!!
Leave a comment:
-
Originally posted by MarillionFanYou bunch of bloody SQL amateurs. I put a question up about SQL no-one apart of Socky can answer it, someone puts a simple question up you all smarm over who can answer it.
Winging it, you're all bloody winging it.
Leave a comment:
-
You bunch of bloody SQL amateurs. I put a question up about SQL no-one apart of Socky can answer it, someone puts a simple question up you all smarm over who can answer it.
Winging it, you're all bloody winging it.
Leave a comment:
-
You could use datepart as well and extract the hours and minutes. But, the real question is, why are you trying to format date/time at the database level? What application are you using to display the information. You should really be looking to use that API to provide the correct time format!!!Last edited by Weltchy; 21 December 2006, 21:16.
Leave a comment:
-
JB is right:
select CONVERT(char(5), (getutcdate()- gpsTime), 108)
from TrackerData
WHERE gpsTime > (GETUTCDATE() -0.5)
ORDER BY gpsTime DESCLast edited by To BI or not to BI?; 21 December 2006, 19:58.
Leave a comment:
-
Using DATEDIFF is better than the "gpsTime > (GETUTCDATE() -0.5)" expression, e.g. DATEDIFF(hour, gpsTime, GETUTCDATE()) < 12.
As for the just wanting hours and minutes part then you can also use smalldatetime which is accurate only to a minute, or look at casting (CONVERT) and select a output format which suits.
Edit: deleted this thinking you'd found the answer. try char(5) instead
Leave a comment:
-
Originally posted by wxmanHey BI! - thanks for the quick reply - I struggled with datediff.. so I tried convert! but I can not seem to drop off the seconds I only want (hh:mm)
so...
select CONVERT(varchar(8), (getutcdate()- gpsTime), 108)
from TrackerData
WHERE gpsTime > (GETUTCDATE() -0.5)
ORDER BY gpsTime DESC
gives..
[01:09:05] (1 hour, 9 mins, 5 seconds)
I wonder if there is a format code (I am currently using ,108) that just gives hh:mm ?
more google for me I guess...Last edited by To BI or not to BI?; 21 December 2006, 19:43.
Leave a comment:
-
Hey BI! - thanks for the quick reply - I struggled with datediff.. so I tried convert! but I can not seem to drop off the seconds I only want (hh:mm)
so...
select CONVERT(varchar(8), (getutcdate()- gpsTime), 108)
from TrackerData
WHERE gpsTime > (GETUTCDATE() -0.5)
ORDER BY gpsTime DESC
gives..
[01:09:05] (1 hour, 9 mins, 5 seconds)
I wonder if there is a format code (I am currently using ,108) that just gives hh:mm ?
more google for me I guess...
Leave a comment:
-
Help with MS SQL DateTime Query
Firstly I am not an SQL guy (or even pretending to be!) as my background is Active Directory so I apologise in advance if this is a simple answer – 2 hours on Goggle have left me clue less…
I am writing a home application that basically logs GPS movements. I store the movements in a MS 2005 database. I am trying to work out how many HH:MM ago the last GPS signal was received. This value is in UTC time and is stored as follows
gpsTime(datetime) 2006-12-21 18:16:27.000
By using (GETUTCDATE()) – gpsTime I get then how long ago the last report was recived. 1900-01-01 00:01:24.970
But what I really want is just HH:MM 00:01 displayed – i.e. 1 minute ago…
This is what I have so far..
SELECT TOP (5)
(GETUTCDATE()) - gpsTime
As [Time Ago]
FROM TrackerData
WHERE gpsTime > (GETUTCDATE() -0.5) ‘ ignore records over 12 hours old
ORDER BY gpsTime DESC
Thanks in advance for anyone who can help me – this is driving me nuts !!!Tags: None
- Home
- News & Features
- First Timers
- IR35 / S660 / BN66
- Employee Benefit Trusts
- Agency Workers Regulations
- MSC Legislation
- Limited Companies
- Dividends
- Umbrella Company
- VAT / Flat Rate VAT
- Job News & Guides
- Money News & Guides
- Guide to Contracts
- Successful Contracting
- Contracting Overseas
- Contractor Calculators
- MVL
- Contractor Expenses
Advertisers
Contractor Services
CUK News
- Secondary NI threshold sinking to £5,000: a limited company director’s explainer Dec 24 09:51
- Reeves sets Spring Statement 2025 for March 26th Dec 23 09:18
- Spot the hidden contractor Dec 20 10:43
- Accounting for Contractors Dec 19 15:30
- Chartered Accountants with MarchMutual Dec 19 15:05
- Chartered Accountants with March Mutual Dec 19 15:05
- Chartered Accountants Dec 19 15:05
- Unfairly barred from contracting? Petrofac just paid the price Dec 19 09:43
- An IR35 case law look back: contractor must-knows for 2025-26 Dec 18 09:30
- A contractor’s Autumn Budget financial review Dec 17 10:59
Leave a comment: