December 13, 2009 at 7:47 pm
Hi friends
I have year and period values in my table like this
2005 5
2001 12
2006 11
I want to show 20050501,20011201,20061101
any ideas please?
December 13, 2009 at 7:49 pm
Are these stored in one field or multiple fields?
separate fields, use the + to contatinate values
select year + month + '01'
December 13, 2009 at 7:56 pm
multiple fields,
i tried your solution but its giving inaccurate results like this
@year=2004
@period=9
select year+period+'01'
result: 2014
i wnat like this 20040900
December 13, 2009 at 8:00 pm
If you are storing these as numerical values you will need to CAST them as strings.
select cast( year as char(4)) + cast( period as char(2)) + '01'
In the future, please include the DDL for the table.
December 13, 2009 at 8:13 pm
Thanks Steve..
It works perfectly..awesome
this is final code..
cast( trn.TGJTYR as char(4)) + RTRIM(cast( trn.TGJTPD as char(2)))+ '01'
December 13, 2009 at 8:38 pm
I wouldn't expect that to work...? You need to prefix with zeroes in some circumstances. One solution is:
declare @year int,
@month int;
set @year = 2009;
set @month = 1
print right(10000 + @year, 4) + right(100 + @month, 2) + '01';
December 14, 2009 at 12:01 pm
DECLARE @StartDate datetime, @EndDate datetime, @StartHour datetime, @EndHour datetime,
@liTimeOffset INT
SET @liTimeOffset = 120
SELECT DateAdd(n, @liTimeOffset, LocationDateTime)
FROM mytable
WHERE
LocationDateTime >= (CONVERT(VARCHAR(10), @StartDate, 120) + RIGHT(CONVERT(VARCHAR, @StartHour, 100),7))
AND
LocationDateTime < (CONVERT(VARCHAR(10), @EndDate, 120) + RIGHT(CONVERT(VARCHAR, @EndHour, 100),7))
LocationDateTime = '2009-11-01 08:13:02' (this is an example).
@StartHour and @EndHour is input parameters for stored procedure. When entering
2009-11-29 (@StartDate), 2009-11-30 (@EndDate), 22:00 (@StartHour), 03:00 (@EndHour)
the above works perfectly but I have one problem, if I want to use it as input parameters for SQL reporting services report it gives no result, if I specify @StartHour and @EndHour as datetime or Integer. If I specify as string it does return results, but more rows are returned that it should.
How do I resolve issue ?
December 14, 2009 at 9:50 pm
clive-421796 (12/14/2009)
DECLARE @StartDate datetime, @EndDate datetime, @StartHour datetime, @EndHour datetime,@liTimeOffset INT
SET @liTimeOffset = 120
SELECT DateAdd(n, @liTimeOffset, LocationDateTime)
FROM mytable
WHERE
LocationDateTime >= (CONVERT(VARCHAR(10), @StartDate, 120) + RIGHT(CONVERT(VARCHAR, @StartHour, 100),7))
AND
LocationDateTime < (CONVERT(VARCHAR(10), @EndDate, 120) + RIGHT(CONVERT(VARCHAR, @EndHour, 100),7))
LocationDateTime = '2009-11-01 08:13:02' (this is an example).
@StartHour and @EndHour is input parameters for stored procedure. When entering
2009-11-29 (@StartDate), 2009-11-30 (@EndDate), 22:00 (@StartHour), 03:00 (@EndHour)
the above works perfectly but I have one problem, if I want to use it as input parameters for SQL reporting services report it gives no result, if I specify @StartHour and @EndHour as datetime or Integer. If I specify as string it does return results, but more rows are returned that it should.
How do I resolve issue ?
DECLARE @StartDate datetime, @EndDate datetime, @StartHour datetime, @EndHour datetime,
@liTimeOffset INT
SET @liTimeOffset = 120
Since all the parameters are datetime...
SELECT DateAdd(n, @liTimeOffset, LocationDateTime)
FROM mytable
WHERE
LocationDateTime >= @StartDate+@StartHour
AND
LocationDateTime < @EndDate+@EndHour
--Jeff Moden
Change is inevitable... Change for the better is not.
December 16, 2009 at 1:16 pm
Thanks Jeff, your solution worked.
December 16, 2009 at 4:53 pm
You're welcome. Thanks for the feedback, Clive.
--Jeff Moden
Change is inevitable... Change for the better is not.
December 17, 2009 at 3:36 pm
Hi Jeff, the query which I included in my post also worked.
WHERE LocationDateTime >= (CONVERT(VARCHAR(10), @StartDate, 120) + RIGHT(CONVERT(VARCHAR, @StartHour, 100),7))
AND LocationDateTime < (CONVERT(VARCHAR(10), @EndDate, 120) + RIGHT(CONVERT(VARCHAR, @EndHour, 100),7))
In SQL Reports, you need to specify the @StartHour (19:00) and @EndHour (07:00) values as string (not datetime). This sorted the issue. I anyways used your method since its less code.
Thanks again.
December 17, 2009 at 6:49 pm
clive-421796 (12/17/2009)
Hi Jeff, the query which I included in my post also worked.WHERE LocationDateTime >= (CONVERT(VARCHAR(10), @StartDate, 120) + RIGHT(CONVERT(VARCHAR, @StartHour, 100),7))
AND LocationDateTime < (CONVERT(VARCHAR(10), @EndDate, 120) + RIGHT(CONVERT(VARCHAR, @EndHour, 100),7))
In SQL Reports, you need to specify the @StartHour (19:00) and @EndHour (07:00) values as string (not datetime). This sorted the issue. I anyways used your method since its less code.
Thanks again.
Exactly... less code. Also, fewer conversions.
--Jeff Moden
Change is inevitable... Change for the better is not.
December 17, 2009 at 7:00 pm
Jeff Moden (12/17/2009)
Exactly... less code. Also, fewer conversions.
And more index-friendly 😉
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply