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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 How to check for the value of....

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/04

when 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) = ?
Go to Top of Page

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
Go to Top of Page

jen
Master Smack Fu Yak Hacker

4110 Posts

Posted - 2004-10-11 : 04:35:06
but doesn't sql treat date strings as dates?
Go to Top of Page

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 */
Go to Top of Page

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/04

when 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 year
SUBSTRING(log_Date,4,2) as day

if 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 */
Go to Top of Page
   

- Advertisement -