all groups > sql server programming > october 2003 >
You're in the

sql server programming

group:

Convert text to MM/YY date in Microsoft SQL


Convert text to MM/YY date in Microsoft SQL damon NO[at]SPAM soho-systems.com
10/21/2003 11:47:23 PM
sql server programming:
I have a table that is used primarily for credit card transaction
processing where the date is stored in a text column in the format
MM/YY. The credit card processing software will not accept it any
other way, there must always be two digits for the months and two for
the year.

Instead of storing the date as a datetime and pulling the relevant
"datepart" the programmer choose to use a text column.

The issue I have now is that I need to create a view where the
accounts that have cards that will expire in the next 2 months are
displayed.

Can someone give me an example of a MS SQL select that will convert
the text value of "MM/YY" to a datetime data type that can be used in
date functions and in the format MM/YY so the CC software does not
puke.

I do have the option to insert the date as a datetime in a new column
and delete the old text column, but I can not get it formated so the
datetime column will accept the data from the text column.

or alternativley

A select that will find dates in the text filed where the right two
characters are less than the year would be in two months from now and
the left two characters are less than the month would be in two months
from now.

Any suggestions?

Convert text to MM/YY date in Microsoft SQL John
10/22/2003 12:28:27 AM
Assuming your data is always in the form mm/yy with mm and
yy always being two characters

convert(datetime,left(vchMMYY,2) + '/01/' + right
(vchMMYY,2))

will give you the first of each month for the expiration
date. You could then use datediff to retrieve the values
you want.

For example:
create table #test (vchMMYY nchar(5))

insert into #test values ('10/03')
insert into #test values ('11/03')
insert into #test values ('12/03')
insert into #test values ('01/03')
insert into #test values ('10/04')
insert into #test values ('11/04')

select *
from #test
where datediff(mm,getdate(),convert(datetime,left
(vchMMYY,2) + '/01/' + right(vchMMYY,2))) between 1 and 2
drop table #test


[quoted text, click to view]
Re: Convert text to MM/YY date in Microsoft SQL edwin_anand
10/22/2003 6:05:13 AM

Greetings Damon,



To Retrieve a Date column as Varchar, in MM/YY Format, use:

SUBSTRING (Convert( varchar(10), <DateCol>, 103 ), 4, 10)



Ex. SUBSTRING (Convert( varchar(10), GetDate(), 103 ), 4, 10)



To convert a text field of MM\YY format into Date, use:

CAST ( '01/'+<DateCol> As DateTime)



Ex. SELECT Cast ( '01/'+ '09/02' As DateTime )



Regards,

Anand.



Originally posted by Damon

[quoted text, click to view]

Thanks.


--
Re: Convert text to MM/YY date in Microsoft SQL damon NO[at]SPAM soho-systems.com
10/22/2003 8:02:02 AM
nicely done, this worked flawlessly (changed the between to 0 and 1 to
get dates in the current month or next month).

I hope you are well compensated for your skills!

Thank you,

Damon


[quoted text, click to view]
Re: Convert text to MM/YY date in Microsoft SQL Tibor Karaszi
10/22/2003 9:15:16 AM
Aren't missing information here? You don't have year, so how can we calculate month spans? (Think
leapyears.)

--
Tibor Karaszi, SQL Server MVP
Archive at: http://groups.google.com/groups?oi=djq&as_ugroup=microsoft.public.sqlserver


[quoted text, click to view]

AddThis Social Bookmark Button