I have a csv file which I inserted into a database using SSIS, this file contains dates in this format MM / DD / YYYY hh: ss: mmm Am / Pm which I inserted under in varchar because if I do a transformation in date it deforms them. here is my data:
ARRIVAL_DATE_TIME
9/25/2021 11:40:32 AM
9/25/2021 11:41:46 AM
9/25/2021 11:55:35 AM
9/25/2021 11:56:15 AM
9/25/2021 11:56:37 AM
9/25/2021 11:56:48 AM
9/25/2021 12:12:25 PM
10/8/2021 8:05:12 AM
10/8/2021 8:11:05 AM
I would like to display my dates in order, my dates are between 09/25/2021 and 10/19/2021,
I am writing this sql code under sql server:
SELECT
convert(varchar, [ARRIVAL_DATE_TIME], 9) a
FROM [MAKS].[dbo].[Masks] order by a
but the results displayed are not good it confuses between days and months. this is what i get
a
10/1/2021 1:00:00 AM
10/1/2021 1:00:06 PM
10/1/2021 1:00:31 AM
10/1/2021 1:00:52 AM
10/1/2021 1:01:06 PM
9/26/2021 9:16:41 AM
9/26/2021 9:19:43 AM
9/26/2021 9:18:28 AM
9/26/2021 9:16:57 AM
anyone have an idea please
Style 101 is the correct datetime style to use here and it should be datetime to get a correct ordering on datetime:
Select convert(datetime, Arrival_date_time, 101) myDateTime
from myTable
order by myDateTime;
DBFiddle demo
Related
How to split this TimeStamp column without changing data format?
I have a column TimeStamp which is in datetime format, and I just want to keep the date part (in datetime format).
I tried to use
CONVERT(DATE, "TimeStamp")
it shows only the date part, but the format is nvarchar(10).
SQL Code:
SELECT
"TimeStamp",
CONVERT(DATE, "TimeStamp") AS Date
My expected result:
TimeStamp (datetime) Date (datatype: datetime)
------------------------------------------------------
2017-03-10 07:30:25 2017-03-10
2017-03-10 07:30:28 2017-03-10
2017-03-10 07:31:30 2017-03-10
2017-03-10 07:31:39 2017-03-10
Tysss I have made a demo for you please try this, Here I have used GETDATE() for return current date with a timestamp where you have to pass your column name as you showed in the question which is TimeStamp.
SOLUTION 1
SELECT CAST(CONVERT(VARCHAR,GETDATE(),110) AS DATE) AS DATE
OUTPUT
2019-05-17
SOLUTION 2
SELECT CAST(CONVERT(VARCHAR,GETDATE(),110) AS DATETIME) AS DATE
OUTPUT
2019-05-17 00:00:00.000
I think you need the only date from TimeStamp so, you need to change the datatype DATE else if you will keep your datatype DATETIME then it will return date something like 2019-05-17 00:00:00.000
select GETDATE() as TimeStamp,
convert(date, getdate()) as date
SELECT CONVERT(date, getdate());
I have this column with Microsoft SQL Server :
Date
6/19/2019 3:10:12 PM
12/23/2016 5:02:15 AM
13/25/2015 2:15:35 PM
And I would like to get two new column like this :
Date Hour
06/19/2019 15:10:12
12/23/2016 05:02:15
13/25/2015 14:15:35
For the first column I just split the original column and I add a 0 for the first line and for the second I add a 0 and transform 03 to 15 removing PM. Thank you very much for the help !
If I understand correctly, you would just cast to the types you want:
select cast(datetimecol as date) as datecol,
cast(datetimecol as time) as timecol
from t;
You can use try_parse as another option if it is a varchar column.
select try_parse('6/19/2019 3:10:12 PM' as date using 'en-US')
select try_parse('6/19/2019 3:10:12 PM' as time using 'en-US')
Or for your query
select
try_parse('datetimecol' as date using 'en-US'),
try_parse('datetimecol' as time using 'en-US')
I have column where I saved the transaction time, format is HHMMSS
for example:
140159
013115
235900
then I want to convert those time to HH:MM AM/PM
so results would be:
2:01 PM
1:31 AM
11:59 PM
Here are the queries ive tried, but none of them return the results I want..
SELECT TO_CHAR(TO_TIMESTAMP(TRANSTIME,'hh24:mi:ss AM'),'hh12:mi:ss AM')
FROM PRODUCTSALES order by TRANSTIME desc LIMIT 100
SELECT TO_TIMESTAMP(TRANSTIME, 'HH24:MI')::TIME
time data type is just time - not a format. to get time with wanted format use to_char, eg fro your 140159:
t=# select to_char('140159'::time,'HH:MI AM');
to_char
----------
02:01 PM
(1 row)
Mind I first cast as time and only then format it
How do i assign current date with a specific time?
let's say 8:00:00 AM to Column EXIT_DT of datatype datetime??
I have tried GETDATE() AS EXIT_DT but it gives me current datetime. I am using Sql server 2005. Any help?
Lets say Today is 1/3/2013 and i want my result to return as a datetime datatype with value 1/3/2013 8:00:00 AM. If i run the statement ytd, the result will be 1/2/2013 8:00:00 AM
This formula will always produce 08:00 for the day it is called, and avoids string manipulation:
select DATEADD(day,DATEDIFF(day,'20010101',GETDATE()),'2001-01-01T08:00:00')
Try to avoid solutions that convert to and from strings - treating datetime values as strings is one of the largest sources of bugs.
It works by computing the number of days (as an integer) that have elapsed since 1st January 2001. It then adds that same number of days to 08:00 on 1st January 2001.
You can try this :
DECLARE #dt datetime;
SET #dt=CONVERT(DateTime, CONVERT(VARCHAR,GETDATE(),101)+' 8:00:00')
SELECT CONVERT(VARCHAR, #dt, 101)+' '+ LTRIM(RIGHT(CONVERT(VARCHAR(20),#dt, 100), 7))
Visit http://www.sql-server-helper.com/tips/date-formats.aspx for datetime formats.
Use Convert along with getdate() to get specific formats.
ex:
SELECT CONVERT(VARCHAR(30),GETDATE(),113)
This is a bit stupid, but it works
select cast(cast(getdate() as date) as datetime) + '08:00:00'
it casts the getdate() to date thus losing the hours, than it casts it to datetime and adds 8 hours.
If you want to avoid implicit conversion of varchar to datetime, you could use this version:
select cast(cast(getdate() as date) as datetime)
+ convert(datetime,'08:00:00',114)
This is also working. (1). convert today's date to ISO format (yyyymmdd) (2). add the time, (3). convert back to datetime
Select convert(datetime, convert(varchar, getdate(),112) + ' ' + '8:00:00AM')
--Results
2013-01-03 08:00:00.000
If you need in specific format you need to convert back to varchar again.
-- AM/PM --
SELECT TO_CHAR(sysdate, 'MM/DD/YYYY HH:MI:SS AM') FROM dual
/
-- 24 hrs format --
SELECT TO_CHAR(sysdate, 'MM/DD/YYYY HH24:MI:SS') FROM dual
/
i have a column with dates, but it is a varchar:
8/31/2010 9:48
8/31/2010 9:49
8/31/2010 9:51
8/31/2010 9:52
8/31/2010 9:55
8/31/2010 9:59
8/31/2010 10:11
8/31/2010 10:13
8/31/2010 10:16
8/31/2010 10:37
8/31/2010 10:42
i made sure that none of these will be a BAD date:
SELECT *
FROM qcvalues.dbo.batchinfo
WHERE ISDATE(reporttime) <> 1
this returned 0 results
question:
i need to return dates between a certain range:
select rowid from qcvalues.dbo.batchinfo where CONVERT(DATE, Substring( reporttime, 1, LEN(reporttime)), 103)
between cast('2010-08-01' as datetime) and CAST('2010-08-31' as datetime)
and i am getting this error;
Msg 241, Level 16, State 1, Line 2
Conversion failed when converting date and/or time from character string.
what is wrong with my conversion?
If you need to store dates then use a datetime column in the future
does this work?
WHERE CONVERT(DATE,RTRIM(reporttime))
BETWEEN '2010-08-01' and '2010-08-31'
If not use SET DATEFORMAT MDY before running it
And if you have to store it in a varchar column then use YYYYMMDD format...that way you can do
WHERE reporttime like '201008%' if you want August 2010
This will solve your problem:
select rowid
from qcvalues.dbo.batchinfo
where
CONVERT(DATE, reporttime, 101) >= '20100801'
-- style 101, not 103
-- also notice date conversion invariant format YYYYMMDD with no separators
AND CONVERT(DATE, reporttime, 101) < '20100901'
-- using BETWEEN with an end date of '8/31/2010' will skip
-- times between '8/31/2010 00:00:00.003' and '8/31/2010 23:59:59.997'
Try this to see what the problem is:
select convert(datetime, '8/31/2010 9:48', 103)
select convert(datetime, '8/31/2010 9:48', 101)
put SET DATEFORMAT MDY before your query.
This will strip out the time portion too
select rowid
from qcvalues.dbo.batchinfo
Where cast(floor(cast(cast(reportTime as datetime)as float))as datetime)
between cast('2010-08-01' as datetime)
and cast('2010-08-31' as datetime)
Remember, this CAST('2010-08-31' as datetime) will have its time portion as 00:00.
Consider casting your varchar data as smalldatetime, and being specific about the boundaries of times in your between. No need to be converting, substring, etc. Just one CAST will do.
Consider this as a potential solution:
SELECT rowid from qcvalues.dbo.batchinfo
WHERE CAST(reporttime as smalldatetime)
BETWEEN '2010-08-01' AND '2010-08-31 23:59'