Convert Times in SQL so I can Subtract in Excel - sql

A timeclock table in SQL is storing TimeIn/TimeOut as 1:00p in a format which when i export into excel will not let me subtract and get the time difference.
I was trying to run a query in SQL and convert the times within SQL so that when i export i can subtract. In Access i was using CVDate.
This is the sql code i tried using:
Convert (varchar(6), dbo.tblPayrollFlashPunch.TIME_IN, 108) as TimeIn,
CONVERT (varchar(6), dbo.tblPayrollFlashPunch.TIME_OUT, 108) as TimeOut,
Any help will be appreciated, btw I am very new to SQL and we are running SQL 2008.
Below is a snapshot of sample data.
(Original here: http://i.stack.imgur.com/AS8BS.gif )
Edit: I was hoping SQL could do the conversion so that i could pull the data through a view directly into excel and have the correct format so that my formulas work. Also, I've tried changing the formats in excel with no success. In MSAccess you can use CVDATE and works great.
Edit 2: Being the noob that I am i just realized we are running SQL Server 2005 which explains why i cant use many of the suggested functions.

Which SQL are you using?
In T-SQL, I believe this would work if your time column is a string/varchar.
CONVERT(TIME, (dbo.tblPayrollFlashPunch.TIME_OUT + 'm'), 108) as TimeOut

What about using DATEDIFF in TSQL to get the difference straight from there?

Related

Converting VBA to SQL - Datediff issue

I am trying to figure out how MS access calculates the following query where contractualDate and ContractualTime are both datetime:
(Max(([contractualDate]+[ContractualTime]))-Min(([contractualDate]+[ContractualTime])))*25,0) AS TrailerCost
The closest I could think of using SQL is
Datediff(m,Min(([contractualDate]+[ContractualTime])),Max(([contractualDate]+[ContractualTime])))*25 AS TrailerCost
This gives me results that differ from the first VBA code, what did I do wrong here?

Pulling a SQL Server date to be used in Oracle query within SSIS environment is ignored in the Oracle query

I am having trouble in my Oracle query that uses a variable stored in SSIS which has a date that is pulled from sql server.
I am using an execute sql task that simply gets a max date from a sql server table and stores it in a variable. E.g.
SELECT MAX(t.Date) FROM table t;
I then want to use that variable in my Oracle query which is an ADO.NET source connection. I noticed you can't parameterize in those connections and found the work around where you use the sql expression with your user variable in it. So now my Oracle source query looks something like this:
"SELECT DISTINCT t.* FROM table t WHERE TO_CHAR(t.LastUpdateDate, 'YYYY-MM-DD') > " + "'#[User::LastUpdateDate]'"
The query syntax itself is fine, but when I run it, it is pulling all rows and seems to be completely ignoring the where clause of the date.
I've tried removing the TO_CHAR from LastUpdateDate.
I've tried adding a TO_CHAR to my user variable #[User::LastUpdateDate].
I've tried using the CONVERSION() function from sql server on #[User::LastUpdateDate].
Nothing seems to work and the query just runs and pulls in all data as if I don't have the WHERE clause on the query.
Does anyone know how to rectify this issue or point out what I might be doing wrong?
Thank you for any and all help!
**EDIT:
My date being pulled from SQL Server is in this format: 2022-09-01 20:17:58.0000000
This is not an answer, just troubleshooting advice
You do not say what data type #[User::LastUpdateDate] is, I'll assume it's a datetime
Ideally all datetime data should be kept in datetime data types, then format becomes completely irrelevant. However since it's difficult to parameterise Oracle queries in SSIS, you have to concoct a string to be submitted. Now date format does become important.
On to something a little different, it is a very good habit performancewise, to not put functions around columns that you are searching on. This is called sargability - look it up.
Given these things, I suggest that you concoct your required SQL query bit by bit and troubleshoot.
First, format your date parameter as an Oracle date literal. Remember this is normally a bad and unecessary thing. We are only doing it because we have to concoct a SQL string.
So create another SSIS variable called strLastUpdateDate and put this hideous expression in it:
RIGHT("0" + (DT_STR,2,1252)DATEPART( "dd" , #[User::LastUpdateDate] ), 2) + '-' +
(DT_STR,3,1252)DATEPART( "mmm" , #[User::LastUpdateDate] ) + '-' +
(DT_STR,4,1252)DATEPART("yyyy" , #[User::LastUpdateDate] )
Yes this is ludicrously long code but it will turn your date variable into a Oracle string literal. You could simplify this by putting it into your original max query but lets not go there. Use whatever debugging technique you have to confirm that it works as expected.
Now you should be able to use this:
"SELECT t.*, '"+#[User::LastUpdateDate]+"' As MyStrDate FROM table t WHERE
t.LastUpdateDate > '" #[User::strLastUpdateDate] + "'"
You can try running that and see if it makes any difference. Make sure you use this https://dba.stackexchange.com/questions/8828/how-do-you-show-sql-executing-on-an-oracle-database to monitor what is actually being submitted to Oracle.
This is all from memory and googling - I haven't done SSIS for many years now
I suspect after all this you may still have the same problem because I recall from many years having the same mysterious issue.

SQL Runner (Google Looker) change date format in query

This topic has been covered several times but I can't find a solution that applies to SQL Runner, which is the custom query portion of Google's Looker platform.
I am attempting to reformat a datetime SELECT statement from yyyy-mm-dd to mm-dd-yyyy.
Currently what I have is:
SELECT
CAST(shift.datetime AS DATE)
FROM table.a
This gives me the yyyy-mm-dd result but so far my efforts to CONVERT have been fruitless. It does not appear that SQL Runner supports the CONVERT command or I am utilizing it incorrectly.
Any thoughts on this one?
I believe sql runner is just gives us a way to directly access the db and it will not change any sql query while communicating with the db directly as long as the timezone of both explore as well as db matches.
Maybe something like this should work for your case
https://sql.tutorialink.com/convert-yyyymmdd-to-mm-dd-yyyy-in-snowflake/
lmk if the above works for your or not!

DATE from DATETIME in SQL SERVER 2005 (Without using VARCHAR)

Our application using SQL SERVER 2005
I need to show only DATE from DateTime in GridView.
I don't want to convert it into any other format like Varchar or something.
It should be only in DateTime format itself, without Time.
Please Help.
In SQL Server, the code is
CAST(datediff(d,0,datetimecol) as datetime)
However, I doubt that does any good for a GridView, which will infer it to be a "datetime" column and show a "date + time" formatting, even if the times are ALL "00:00:00".
Bound the DateField coloumn in Grid like below, then it works...
<asp:BoundField DataField="Your_Date_Column"
HeaderText="Date_Column"
DataFormatString="{0:d}" />
DataFormatString="{0:d}" it display the Date like 3/11/2013
In SQL 2005 there is no way of keeping it in datetime format without the time (time is part of the datetime type), the best you could get would be something like:
2013-04-02 00:00:00.000
If you were willing to convert to VARCHAR, you could use something similar to:
SELECT CONVERT(VARCHAR(10),GETDATE(),111)
Which would display the date portion as you want it. Otherwise, you need to move to SQL 2008+ where there is a proper date type that you can use instead.
If you need to stick with SQL 2005, the best way to achive this is to just format it in the datagrid view itself.
If you want to truncate a datetime so that it only returns the date as at midnight, see the accepted answer to this question - the question relates specifically to SQLServer 2008, but the answer includes details of how to achieve this in SQLServer 2005.
If you want to convert a datetime field into a date-only field - you cannot achieve this in SQLServer 2005 in SQL alone, as there is no date-only datatype - see here.

Formatting strings into dates in mySQL

I am migrating an Access db to mySQL. I am receiving the tables as CSVs and using Excel to drop and format some columns. The dates are coming to me as m/d/yyyy and go as far back as 1702. Since Excel brilliantly won't format any date before 1900 I am going to have to transform them some other way. Now it would be super simple to write a PHP script to iterate over rows and use date('Y-m-d', strtotime($date)) but I was wondering if there was a function or functions in mySQL that would be the equivalent?
Try the STR_TO_DATE function. e.g.
SELECT STR_TO_DATE('4/1/1960', '%m/%d/%Y');