i trying to build the following query to select * from table where the minDate is 03-02-2014 and the maxDate is 01-03-2014
but something i missing.
hope that someone can help me with this.
SELECT * From table Where
SUBSTRING(mydate, 1, 10) >= REPLACE('03-02-2014','-','/') AND
SUBSTRING(mydate, 1, 10) <= REPLACE('01-03-2014','-','/')
Note:
My Date column is of type varchar with a value like this --> 03/02/2014 18:13:16
im working in sql server management studio (t-sql)
From your comments, it seems that the mydate column of your table is in the British format.
Read this article about date conversion in SQL SERVER to understand more about date conversions.
Also updated my answer with the date conversions for this format.
Try something like
SELECT * FROM table
WHERE CONVERT(DATE, SUBSTRING(mydate, 1, 10), 103) >= CONVERT(DATE, '03/02/2014', 103)
AND CONVERT(DATE, SUBSTRING(mydate, 1, 10), 103) <= CONVERT(DATE, '01/03/2014', 103)
you can do something like:
Select * From Table
Where CONVERT( Datetime, mydate ,110 ) between CONVERT( Datetime, #min ,110 ) and between CONVERT( Datetime, #max ,110 )
I don't have SSMS available right now, but for a quick try you might try this
SELECT * From table Where
CAST( SUBSTRING(mydate, 1, 10) as date) BETWEEN CAST( SUBSTRING('START DATE', 1, 10) as date)
AND CAST( SUBSTRING('END DATE', 1, 10) as date)
Related
I have a character field representing a date in the following format:
'yyyyMMdd-000000'
I'm not completely sure what the 0's represent, but I'm trying to strip those off then check if the date is the same as today's date:
SELECT
acctnum,
acctname
FROM
[Server].[dbo].[Table1]
Where
CAST(LEFT(myDate,8) AS DATE) = CAST(GetDate() as Date)
When executing that statement I get this error:
Conversion failed when converting date and/or time from character string.
What am I doing wrong and how could I fix this?
Use try_cast() instead:
Where TRY_CAST(LEFT(myDate, 8) AS DATE) = CAST(GetDate() as Date)
Then, you can find the bad data using:
select myDate
from [Server].[dbo].[Table1]
Where TRY_CAST(LEFT(myDate, 8) AS DATE) is null;
You can attempt to find the bad data with something like:
select myDate
from [Server].[dbo].[Table1]
where mydate not like '[12][90][0-9][01][0-9][0-9][1-3][0-9]%'
This doesn't find all bad examples, but if something is glaring it will pop.
declare #table1 table
(
acctnum int,
acctname varchar(10),
myDate varchar(20)
)
insert into #table1(acctnum, acctname, myDate)
values(1, 'A', '20200401-000000'),
(2, 'B', convert(varchar(20), getdate(), 112) + '-000000'),
(3, 'C', convert(varchar(20), getdate(), 112) + '-000000'),
(4, 'D', 'abcd-0000');
select *
from #table1;
select *, case isdate(stuffdate) when 1 then cast(stuffdate as date) end
from
(
select *, stuff(stuff(stuff(myDate, 14, 0, ':'), 12, 0, ':'), 9, 1, ' ') as stuffdate
from #table1
where myDate like convert(varchar(20), getdate(), 112)+'%'
) as t;
select *
from
(
select *, stuff(stuff(stuff(myDate, 14, 0, ':'), 12, 0, ':'), 9, 1, ' ') as stuffdate
from #table1
) as t
where case isdate(stuffdate) when 1 then cast(stuffdate as date) end = convert(varchar(20), getdate(), 112);
I have an SSRS report where I am using below query.
(This query works fine in SQL server, problem is only in SSRS report)
--DECLARE #Range Number = 10;
SELECT * FROM TBL1 WHERE
USERNAME = 'MIKE'
AND
(
#Range = '10'
and
Convert(datetime, MyDate, 120) <= GETDATE()
)
or
(
#Range IN ('20','30')
and
DATEDIFF(DD, Convert(datetime, MyDate, 120) , GETDATE()) <= #Range
)
Unfortunately the myDate Column coming from database is a varchar column.
The SSRS throws an out-of-range exception.
Then I tried converting Getdate to Convert(varchar(10), getdate(), 120) and compare with myDate (without conversion as myDate is already in YYYY-MM-DD format but as a varchar in database)it still throws error. I assume this time coz SSRS is not able to process datediff in varchar columns.
When I run these queries individually, it works fine. i.e -
Declare #Range Number = 10;
Select * from tbl1
where username = 'MIKE' and
(
#Range = '10'
and
convert(datetime, MyDate, 120) <= getdate()
Has anyone faced similar issue in SSRS ???
Try this!
SELECT * FROM TBL1 WHERE
USERNAME = 'MIKE'
AND
(
#Range = '10'
and
cast(myDate as datetime) <= GETDATE()
)
or
(
#Range IN ('20','30')
and
cast(myDate as datetime) , GETDATE()) <= #Range
)
Presumably, your problem is that some values of myDate are not in the correct format. If you are using a more recent version of SQL Server, the function try_convert() can be a big help.
The reason it is failing is because of your logic. The second condition after the or is looking at all records, not just Mike's. I think you intend for the where clause to be:
SELECT *
FROM TBL1 WHERE
WHERE USERNAME = 'MIKE' AND
((#Range = '10' and Convert(datetime, MyDate, 120) <= GETDATE()
) or
(#Range IN ('20','30') and
DATEDIFF(DD, Convert(datetime, MyDate, 120) , GETDATE()) <= #Range
)
);
Note the extra set of parentheses. Now, this will probably help for this particular query for MIKE. But it won't help overall. Finding the value(s) that fail conversion can be daunting. If you are lucky, they fail easily. You can look for them with starting with:
select MyDate
from tbl1
where MyDate not like '[0-9][0-9][0-9][0-9]-[0-2][0-9]-[0-3][0-9]%';
If you are lucky, then this will find the offending values. Otherwise, you'll have to dive more deeply into the date formats, looking at the particular month and day (and potentially hour, minute, and second) values.
Moral: Store dates as dates. Don't store them as varchar().
hi i wrote this query in SqlServer 2008
but some thing goes wrong
select * from News_Table
where (DATEDIFF( DAY ,convert(datetime, NewsDate) , convert(datetime,#Todaydate )) <= #Count)
that #NewsDate and #Todaydate are two nvarchar parameters that are saved like this 2014/11/16
running this query give me an error:
Conversion failed when converting date and/or time from character string
Try adding the correct style parameter to your convert function (see MSDN: link )
ie CONVERT(DATETIME, NewsDate, 111) (111 is the style for YYYY/MM/DD)
Then you get:
SELECT *
FROM News_Table
WHERE (DATEDIFF( DAY ,
CONVERT(DATETIME, NewsDate, 111) ,
CONVERT(DATETIME,#Todaydate, 111)
) <= #Count)
use Convert(datetime, #yourvalue, 111)
select * from News_Table
where (DATEDIFF( DAY ,convert(datetime, #NewsDate, 111) , convert(datetime,#Todaydate, 111 )) <= #Count)
http://www.sqlusa.com/bestpractices/datetimeconversion/
To know more click here
SELECT convert(datetime, '2014/11/16', 111) as datetime
OP
So your query would be like this
Select * from News_Table
where (DATEDIFF( DAY ,convert(datetime, '2014/11/16', 111) , convert(datetime,#Todaydate,111 )) <= #Count)
Try like this
SELECT *
FROM News_Table
WHERE (DATEDIFF(DAY,CAST(NewsDate AS Datetime),CAST(#Todaydate AS Datetime)) <= #Count)
You will need to do something like this to convert that string into DATETIME datatype
DECLARE #Date NVARCHAR(20) = '2013/11/16'
SELECT CAST((LEFT(#Date, 4) + SUBSTRING(#Date, 6 ,2) + RIGHT(#Date, 2)) AS DATETIME)
for your query
select * from News_Table
where (DATEDIFF( DAY , CAST((LEFT(NewsDate, 4) + SUBSTRING(NewsDate, 6 ,2) + RIGHT(NewsDate, 2)) AS DATETIME)
, CAST((LEFT(#Todaydate, 4) + SUBSTRING(#Todaydate, 6 ,2) + RIGHT(#Todaydate, 2)) AS DATETIME)
) <= #Count)
Note
If variable #Todaydate is actually storing today's date then why not use simply GETDATE() function.
i have a DB with a atribute "Date" which is a string ...
its format is:
"Jan 5 2014 6:26 PM"
and would like to get the number of rows where the date is this year.
already know how to convert the date:
SELECT convert(datetime, 'Oct 23 2012 11:01AM')
i found this code but I do not know how to join the two
select * from datetimes2
where dtm2 >= CAST(CURRENT_TIMESTAMP AS DATE)
and dtm2 < DATEADD(DD, 1, CAST(CURRENT_TIMESTAMP AS DATE))
;
Now I do not know how to do what I want :(
Is this what you looking for ?
DECLARE #datetime DATETIME = 'Jan 5 2014 6:26 PM'
SELECT
*
FROM datetimes2
WHERE [Date] >= CONVERT(DATETIME, CAST(DATEPART(YEAR, #datetime) AS VARCHAR) + '-01-01')
AND [Date] <= CONVERT(DATETIME, CAST(DATEPART(YEAR, #datetime) AS VARCHAR) + '-12-31')
This will get the records for complete year 2014. Hope this helps!
As simple as (assuming SQL server, given convert() )
select * from mytable where year( convert(datetime, date) ) = year( getDate() )
?
I have 2 columns in a table of varchar datatype.
date and type are the column names in table.
the data present in the table looks like this
date time
20090610 132713
20090610 132734
i need ms sql server query to concatenate these 2 columns data and display as datetime format.
Note :
1. the datatype of those 2 columns cannot be changed now.
2. i tried
select convert(datetime,date + time)
it says "Conversion failed when converting date and/or time from character string."
Suggest the possible solution.
This will return a datetime. The bottom line is to be replaced by your table
select convert(datetime,date,112)+
coalesce(stuff(stuff(rtrim(time), 5,0,':'), 3,0,':'), '') newdate
from
(VALUES ('20090610','132713'),('20090610', '132734'),('20090610', ' ')) yourtable(date,time)
Result:
newdate
2009-06-10 13:27:13.000
2009-06-10 13:27:34.000
2009-06-10 00:00:00.000
You can get it using
SELECT
convert(varchar, convert(datetime, date), 111)
+ ' ' + substring(time, 1, 2)
+ ':' + substring(time, 3, 2)
+ ':' + substring(time, 5, 2)
CREATE TABLE #Table
(
[date] VARCHAR(100),
[time] VARCHAR(100)
)
INSERT INTO #Table VALUES
('20090610','132713'),
('20090610','132734')
;WITH Bits_CTE
AS
(
SELECT
[Date],
[Time],
[hrs] = CONVERT(INT,SUBSTRING([Time], 1, 2)),
[mns] = CONVERT(INT,SUBSTRING([Time], 3, 2)),
[secs] = CONVERT(INT,SUBSTRING([Time], 5, 2))
FROM #Table
)
SELECT
[Date],
[Time],
DATEADD(HOUR,[hrs],
DATEADD(MINUTE,[mns],
DATEADD(SECOND,[secs],[Date])))
FROM Bits_CTE
CREATE FUNCTION [dbo].[DateTimeAdd]
(
#datepart date,
#timepart time
)
RETURNS datetime2
AS
BEGIN
RETURN DATEADD(dd, DATEDIFF(dd, 0, #datepart), CAST(#timepart AS datetime2));
END
Sorry - Missed the bit in your question about storing the date and time as varchars. You would therefore still need to convert these data itemsbefore using this function.