Table Structure
Id No Date Time
0001, 01-05-2009, 040000
0001, 02-05-2009, 020000
0002, 01-05-2009, 060000
0002, 01-05-2009, 180000
Time and Date is nvarchar
I want to get the data between:
Yesterday 030001 to today 030000
Day before yesterday 0300001 to yesterday 030000
I tried the below mentioned query
Select
idno, min (time), max (time)
from
table
where
time between 030001 and 030000
Nothing displayed in the result because it is taking today time from 03.00 am to 03.01am
Exactly I need today 03.00 am to yesterday 03.01 am
I need the sql query for the above condition
Can anyone help me?
It's hard to sort or filter when your date is in a format like DD-MM-YYYY. In such a format, 01-01-2009 comes before 02-01-2000 !
So, first convert your date with the long years first, and the short seconds last:
YYYY-MM-DD HH:MM:SS
A query like this can do that:
select [id no],
substring(date,1,2) + '-' + substring(date,4,2) + '-' +
substring(date,7,4) + ' ' +
substring(time,1,2) + ':' + substring(time,3,2) + ':'
substring(time,5,2) as NormalDate
from YourTable
Now you can easily select all rows for a particular timespan:
select *
from (
select [id no],
substring(date,1,5) + '-' + substring(date,7,4) + ' ' +
substring(time,1,2) + ':' + substring(time,3,2) + ':'
substring(time,5,2) as NormalDate
from YourTable
) sub
where '2009-05-01 03:00:00' < NormalDate
and NormalDate < '2009-05-02 03:00:00'
Related
DD(10)-MM(05)-YYYY(2013)
I have a table with DATE, MONTH, YEAR in separate columns. How I can combine them into a single column Created date?
Output must be: 10-05-2013 (DD-MM-YYYY format)
SELECT RIGHT('00' + DAY, 2) + '-' + RIGHT('00' + MONTH, 2) + '-' + YEAR AS Date
FROM YourTable
I guess you want have a date field instead of a string. So use TO_DATE() function. You can format date anyway you want later.
TO_DATE(YEAR + '/' + MONTH + '/' + DATE, 'yyyy/mm/dd')
You can do it easily:
SELECT to_char(TO_DATE('10 01 2014', 'DD MM YYYY'),'dd-mm-yyyy') AS MYDATE FROM DUAL
To concatenate columns you just need to use "+" in your select statement
SELECT DATE + '-'+ MONTH + '-'+ YEAR FROM table
In a DB2 SQL query, I return two integers: year and month. From this, I need to construct a string of the format dd/mm/yyyy. The date must represent the first day of the NEXT month.
So for example
Year = 2016, Month = 9 would result in the string "01/10/2016"
Currently I use the following (which I inherited) to do this:
substring(cast(DATE(INSERT(INSERT(LEFT(CHAR((Year * 10000) + (Month * 100) + 1),8),5,0,''-''),8,0,''-'')) + 1 month as char(10)),9,2)|| ''/'' ||
substring(cast(DATE(INSERT(INSERT(LEFT(CHAR((Year * 10000) + (Month * 100) + 1),8),5,0,''-''),8,0,''-'')) + 1 month as char(10)),6,2)|| ''/''||
substring(cast(DATE(INSERT(INSERT(LEFT(CHAR((Year * 10000) + (Month * 100) + 1),8),5,0,''-''),8,0,''-'')) + 1 month as char(10)),1,4) as UPDATEDATE
but it seems horrible. Is there a better way to achieve this result?
Thanks
Ron Ventura
try this
select VARCHAR_FORMAT(TIMESTAMP_FORMAT(cast(year*10000+month*100+1 as char(8)), 'YYYYMMDD') + 1 month, 'DD/MM/YYYY') as mydate
from yourtable
I've received a flat file and after parsing and inserting it in a table. I've a column which has dates in a format yyyyMMddhhmmss
Here, yyyy is year, MM is month, dd is day, hh is hours, mm is minute and ss is seconds part
I'm trying to convert this to DateTime type as mentioned below but not working for me
SELECT CAST(StrDate as DateTime) FROM [dbo].[Mytable]
For example:
Column has a value 20150121190941 in Varchar format and it should be converted to DateTime as 2015-01-21 19:09:41.000
I apologize if it's a duplicate one.
You can use select DATETIMEFROMPARTS ( year, month, day, hour, minute, seconds, milliseconds )
declare #dt nvarchar(25) = '20150121190941'
select datetimefromparts(left(#dt,4), substring(#dt,5,2), substring(#dt,7,2),substring(#dt,9,2), substring(#dt,11,2), substring(#dt,13,2), '000')
SQL Server readily recognizes YYYYMMDD as a date. So, converting the first 8 characters to a date is easy. Alas, I don't think there is a time representation without colons.
So, here is one rather brutal method:
select (convert(datetime, left(StrDate, 8)) +
convert(time, substring(StrDate, 9, 2 ) + ':' + substring(StrDate, 11, 2 ) + ':' + substring(StrDate, 13, 2 )
)
)
Declare #dt varchar(50)
set #dt=20150121190941
create table tblDateTbl
(
col1 datetime
)
insert into dt (col1)
select SUBSTRING(#dt,1,4) + '-' + SUBSTRING(#dt,5,2) + '-' + SUBSTRING(#dt,7,2)+ ' ' + SUBSTRING(#dt,9,2)+ ':' + SUBSTRING(#dt,11,2)+ ':' + SUBSTRING(#dt,13,2)+ '.000'
I have a scenario where I have a batch number and I need to validate the the date in the batch number is the same as today's date. I can extract the date out of the batch number but the problem is that the portion of the batch number used to indicate the date is "27.03" or "2703". The year is not shown. How do I go about converting it to a date that I can use to validate? PS. I am using SQL Server 2012
Thank you.
Not sure if I can fully answer the question without more info. You can try to construct the DDMM value using the current date from SQL server, then use that in a where clause.
For example, you can get the ISO Standard date (112 = yyyymmdd) using the following. More info here:
select CONVERT(nvarchar(8),getdate(),112)
Then from the above YYYYMMDD date, you can get the DDMM value and use it in a where clause something similar to the following:
DECLARE #ddmmValue CHAR(6)
SET #ddmmValue='%'+ SUBSTRING(CONVERT(nvarchar(8),getdate(),112),5, 2) +
SUBSTRING(CONVERT(nvarchar(8),getdate(),112),7, 2) + '%'
select * from MyTest
where BatchNumber like #ddmmValue
This also depends on what the rest of your batch number looks like.
Maybe you can just take advantage of what you've got and append the year then use the German date format to convert to a date type. I'm assuming you want your dates to be no later than today's date.
select
case
when convert(
date,
batch_num + '.' + cast(year(current_timestamp) as char(4)),
104
) > cast(current_timestamp as date)
then dateadd(
yy,
-1,
convert(
date,
batch_num + '.' + cast(year(current_timestamp) as char(4)),
104
)
)
else
convert(
date,
batch_num + '.' + cast(year(current_timestamp) as char(4)),
104
)
end
...
Or pretty much the equivalent...
select
dateadd(
yy,
case
when convert(
date,
batch_num + '.' + cast(year(current_timestamp) as char(4)),
104
) > cast(current_timestamp as date)
then -1
else 0
end,
convert(
date,
batch_num + '.' + cast(year(current_timestamp) as char(4)),
104
)
)
Or if you're just looking to compare to today's date and handle an optional period between the day and month (after I re-read your question)...
where
cast(
date,
replace(stuff(batch_num, 3, 0, '.'), '..', '.') +
'.' + cast(year(current_timestamp) as char(4)),
104
) = cast(current_timestamp as date)
Thank you. I used a REPLACE statement as follows:
SET BatchDate1 = SUBSTRING(Scan,6,4)
SET DateNow1 = REPLACE(CONVERT(varchar(5),GETDATE(),103),'/','')
IF BatchDate1 <> DateNow1
And yes, the batch code sometimes is "DDMM" or "DD.MM" but it always has a unique starting character that would tell you what the date format is so I validated for this first and then the date. Thanks again.
Good Day
I am working of a existing SQL Server Database. What the developers did is to keep the Date and time separate. The Date is in DateTime format (what I want) but the time is incorrect. if it is 14:30 it shows as 1430 when its 09:25 shows as 925. I am trying tyo combine the date and time to have a Date Time view for an program I am writing on top of this database.
I have created the date as a normal date like this:
CASE
WHEN LEN(T0.BeginTime) = 3 THEN '0' + LEFT(T0.BeginTime, 1) + ':' + RIGHT(T0.BeginTime, 2)
ELSE LEFT(T0.BeginTime, 2) + ':' + RIGHT(T0.BeginTime, 2)
END AS 'NEW Start Time'`
The date now looks like it's the correct format but when I want to combine the date and time I get VARCHAR to DateTime error.
How can I fix this?
This is the error:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value (ONLY RAN 804 RECORDS)
Thanks
This should do the trick, Hope it helps.
DECLARE #DateTime TABLE (
DateWithTime DATE,
BeginTime INT);
INSERT INTO #DateTime
VALUES ('2014-08-04', '1525'),
('2014-08-04', '525'),
('2014-08-04', '15'),
('2014-08-04', '5'),
('2014-08-04', '0'),
('2014-08-04', '90')
;WITH cte_BeginTimeFix
AS (
SELECT
CONVERT(VARCHAR(10), DateWithTime, 120) AS DateWithTime,
RIGHT('0000' + CAST(BeginTime AS VARCHAR(4)), 4) AS BeginTime
FROM #DateTime
)
, cte_DateString
AS (
SELECT DateWithTime,
BeginTime,
DateWithTime + ' ' + STUFF(STUFF('00:00:00.000', 4, 2, RIGHT(BeginTime, 2)), 1, 2, LEFT(BeginTime, 2)) AS DateTimeStr
FROM cte_BeginTimeFix
)
SELECT DateWithTime,
BeginTime,
CASE
WHEN ISDATE(DateTimeStr) = 1 THEN CAST(DateTimeStr AS DATETIME)
ELSE NULL
END AS DateTimeStr
FROM cte_DateString
A different approach is to convert the time column in minutes and add it to the date
DATEADD(minute, T0.BeginTime / 100 * 60 + T0.BeginTime % 100
, CONVERT(VARCHAR, T0.BeginDate, 112))
with that the length of the time column doesn't matter
This should work:
CONVERT
(
DATETIME,
CONVERT(VARCHAR,T0.Date,112) +
' ' +
CASE
WHEN ISNULL(T0.BeginTime,'0') = '0'
THEN '00:00'
ELSE
RIGHT
(
'00' + LEFT(T0.BeginTime,LEN(T0.BeginTime) - 3),
2
) +
':' +
RIGHT(T0.BeginTime,2)
END
)