Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
mugen2005
Starting Member
16 Posts |
Posted - 2011-07-12 : 20:51:20
|
| Hi Guys Im a newbie at all of this, I have been messing around for about 2 months making reports and building my db. However, I recently ran into a problem.Im trying to write some statements that pull only the current month without having to change them each month. I was looking around the web and found where MONTH (column name) = Getdate() and YEAR (column name) = Getdate()However, these will not work for me as the column in the table was created as a VARCHAR, and when I try to convert it to Date it says this is not possible. So this leaves me thinking I have to do a convert the column first. The column date is in the format of 201107312359. If anyone could help that would be great. Thank you in advance |
|
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2011-07-12 : 21:00:16
|
| [code]WHERE LEFT(dt, 4) = DatePart(year, getdate())And SUBSTRING(dt, 5, 2) = DatePart(month, getdate())[/code] |
 |
|
|
mugen2005
Starting Member
16 Posts |
Posted - 2011-07-12 : 21:03:31
|
| RussellThank you for the info, that statement looks good. Ill plug her in tomorrow and report back.thanks again |
 |
|
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2011-07-12 : 21:09:36
|
Great. Let us know how it works out! |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2011-07-12 : 23:50:52
|
If you want to use an existing index over [column name] column, tryselect * from dbo.table1where [column name] >= dateadd(month, datediff(month, 0, getdate()), 0)and [column name] < dateadd(month, datediff(month, -1, getdate()), 0) N 56°04'39.26"E 12°55'05.63" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2011-07-12 : 23:51:46
|
Your REAL problem is that someone convinced you to use varchar datatype to store datetime information. N 56°04'39.26"E 12°55'05.63" |
 |
|
|
mugen2005
Starting Member
16 Posts |
Posted - 2011-07-13 : 07:14:13
|
quote: Originally posted by SwePeso Your REAL problem is that someone convinced you to use varchar datatype to store datetime information. N 56°04'39.26"E 12°55'05.63"
Your exactly right. Unfortunately I inherited these data bases once our "DBA" left. They have been around for about 2 years now and are huge, about 7 million rows. Maybe the first of the year ill purge the data and correct the column types. |
 |
|
|
mugen2005
Starting Member
16 Posts |
Posted - 2011-07-13 : 07:34:14
|
quote: Originally posted by russell
WHERE LEFT(dt, 4) = DatePart(year, getdate())And SUBSTRING(dt, 5, 2) = DatePart(month, getdate())
Russell, Looks like you nailed it, works like a Champ. I appreciate it. SwePeso, yours would of worked if my columns were formatted correctly, I recieved errors in coverting from VARCHAR to date. Thanks for your input though. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2011-07-13 : 08:53:46
|
You can make two calculated columns (and persisted) and make the query work against them.Your queries will be at least one magnitude faster. N 56°04'39.26"E 12°55'05.63" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2011-07-13 : 09:14:11
|
See this very simple repro and if it can make you change your mind.set nocount oncreate table #sample ( id int identity(1, 1) primary key clustered, data varchar(20), dt as convert(date, left(data, 8), 112) persisted )-- populate 100k sample rowsinsert #sample select top(1000000) replace(replace(replace(convert(varchar(16), dateadd(minute, checksum(newid()) / 10, '24000101'), 120), ' ', ''), ':', ''), '-', '')from master..spt_values as v1 inner join master..spt_values as v2 on v2.type = 'P'where v1.type = 'p'create nonclustered index ix_dt on #sample (dt) include(data)declare @now date = (select dt from #sample where id = 500000)SET STATISTICS IO ONSET STATISTICS TIME ON-- Originalprint '-- This is the original query'select data from #sample where LEFT(data, 4) = DatePart(year,@now) And SUBSTRING(data, 5, 2) = DatePart(month, @now)-- Pesoprint '-- This is Peso query'select data from #sample where dt >= dateadd(month, datediff(month, 0, @now), 0)and dt < dateadd(month, datediff(month, -1,@now), 0)SET STATISTICS TIME OFFSET STATISTICS IO OFF drop table #sample/*-- This is the original queryTable 'Worktable'. Scan count 1, logical reads 3600, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. SQL Server Execution Times: CPU time = 187 ms, elapsed time = 181 ms.-- This is Peso queryTable 'Worktable'. Scan count 1, logical reads 5, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. SQL Server Execution Times: CPU time = 0 ms, elapsed time = 0 ms.*/ N 56°04'39.26"E 12°55'05.63" |
 |
|
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2011-07-13 : 11:05:22
|
| mugen2005, glad it worked for you. Do have a close look at Peso's example though. His solution is far more efficient. |
 |
|
|
|
|
|
|
|