If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > how to get month + year from a date which is of type varchar(20)?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-02-11, 05:45
navedjobs navedjobs is offline
Registered User
 
Join Date: Sep 2011
Posts: 35
how to get month + year from a date?

hi guyz , i have a requirement
what i want is ,
in my table i m using varchar to save a date like this 2-dec-2011
now i have a requirement where i need to select employee who where present in a month so for that i need a quary so that i can pull out all the employees who are present in dec like
Code:
select * from emp_attendence where date = 'dec-2011'
but this returns nothing do you have any ways to achieve what i want to
thanks

Last edited by navedjobs; 12-02-11 at 06:40.
Reply With Quote
  #2 (permalink)  
Old 12-02-11, 09:31
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,606
It will perform poorly, but what you want is a LIKE expression.

-PatP
__________________
In theory, theory and practice are identical. In practice, theory and practice are unrelated.
Reply With Quote
  #3 (permalink)  
Old 12-02-11, 09:32
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,606
If you store the dates as dates, then you have much better choices available because you can use indexes and you can also use date arithmetic.

-PatP
__________________
In theory, theory and practice are identical. In practice, theory and practice are unrelated.
Reply With Quote
  #4 (permalink)  
Old 12-02-11, 09:51
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
couldn't you use
Code:
CONVERT(DATE,REPLACE([date],,'-',' '),106) AS actual_date
put this into a view and then create an index on that column in the view

then
Code:
WHERE actual_date >= '2011-12-01'
  AND actual_date  < '2012-01-01'
will return rows for december efficiently
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 12-02-11, 10:03
navedjobs navedjobs is offline
Registered User
 
Join Date: Sep 2011
Posts: 35
Quote:
Originally Posted by Pat Phelan View Post
It will perform poorly, but what you want is a LIKE expression.

-PatP
Code:
like
seems perfert but return an empty resultSet in java jdbc .
Reply With Quote
  #6 (permalink)  
Old 12-02-11, 10:11
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by navedjobs View Post
seems perfert but return an empty resultSet in java jdbc .
what expression did you use?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 12-02-11, 10:29
navedjobs navedjobs is offline
Registered User
 
Join Date: Sep 2011
Posts: 35
Quote:
Originally Posted by r937 View Post
what expression did you use?
its an PreparedStatement i m doing every thing right but , im not getting the right output , is there is any special way to use LIKE in java
Code:
String listByEmpIdQuary = "select * from emp_att WHERE Emp_id = ? and Date LIKE ?";
Reply With Quote
  #8 (permalink)  
Old 12-02-11, 10:55
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
what value did you give for that second question mark?

what i'm trying to find out is whether you used the right wildcard characters
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 12-02-11, 11:49
navedjobs navedjobs is offline
Registered User
 
Join Date: Sep 2011
Posts: 35
Quote:
Originally Posted by r937 View Post
what value did you give for that second question mark?

what i'm trying to find out is whether you used the right wildcard characters
yes , i m using the right one which is '%'
like %-dec-2011
in which user will give dec and 2011 and this two will always bind with % implicitily
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On