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 |
|
jonasdavedomingo
Starting Member
33 Posts |
Posted - 2004-10-10 : 21:52:35
|
Let's say i have a column named log_Date and has 1 row which is 10/11/04when you code a sql query like this:"SELECT * FROM tblName WHERE LEFT(log_Date, 2) = '10'"; you return that row 10/11/04.for month, we use LEFT(log_Date, 2), what if i want to check for the day and year? what will be the code for that? |
|
|
jbkayne
Posting Yak Master
100 Posts |
Posted - 2004-10-10 : 23:16:21
|
| Look up DATEPART in Books Online....One example: WHERE DATEPART("dd",log_date) = ? AND DATEPART("yyyy", log_date) = ? |
 |
|
|
GunZ
Starting Member
29 Posts |
Posted - 2004-10-11 : 02:40:26
|
jbkayne,he stored the date as character instead of the normal sql datetime... jonasdavedomingo,see if you can cast it as a date on a variable, then apply what jbkayne said...mabuhay Australia.NSW.Sydney.GunZ |
 |
|
|
jen
Master Smack Fu Yak Hacker
4110 Posts |
Posted - 2004-10-11 : 04:35:06
|
| but doesn't sql treat date strings as dates? |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2004-10-11 : 09:00:26
|
| Implicit conversion occurs from char datatypes do datetime datatype.I suggest that jonasdavedomingo uses a datatime column to store the date.Or do something like:month(cast(log_date as datetime))day(cast(log_date as datetime))year(cast(log_date as datetime))rockmoose/* Chaos is the nature of things...Order is a lesser state of chaos */ |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2004-10-11 : 09:05:39
|
quote: Originally posted by jonasdavedomingo Let's say i have a column named log_Date and has 1 row which is 10/11/04when you code a sql query like this:"SELECT * FROM tblName WHERE LEFT(log_Date, 2) = '10'"; you return that row 10/11/04.for month, we use LEFT(log_Date, 2), what if i want to check for the day and year? what will be the code for that?
ok,RIGHT(log_Date,2) as yearSUBSTRING(log_Date,4,2) as dayif you figured out LEFT(log_Date, 2) as month, You should have been able to figure out the rest !rockmoose/* Chaos is the nature of things...Order is a lesser state of chaos */ |
 |
|
|
|
|
|
|
|