Avatar of printmedia
printmedia

asked on 

convert 2 fields into a date field in sql

Hi all.

I have a sql table with 2 columns: SaleMonth and SaleYear.

I want to be able to have the end user enter date range parameters (i.e. 11/2010 - 01/2011) the only way I think I can do this is by creating another column by combining my 2 columns (SaleMonth and SaleYear) like this: 11/1/2010 and 1/1/2011

Any ideas how I can do this?

Thank you in advance.
Microsoft SQL Server

Avatar of undefined
Last Comment
ralmada
Avatar of Jesus Rodriguez
Jesus Rodriguez
Flag of United States of America image

You can have only oen column with the date and them let the client select the range that he wants. Ex:

SaleDate (datetime)
05/01/2011
05/20/2011
04/2/2011
03/2/2011

Then you can query your table like this
select * from table where saledate between '04/04/2011' and  '05/20/2011'
and create the two dates as parameter in the query and also give the date range on the days if you want


If you have to use the two columns then you can say this

select * from table
where cdate(salemonth+'/01/'+saleyear) between Date1 and Date2

where date1 and date2 will be two dates, but as I told you before, I recommend you the first solution.
Avatar of bhoenig
bhoenig
Flag of United States of America image

This should get you started.
declare @InputParameterStart varchar(10)
set @InputParameterStart = '11/2010'

declare @month char(2)
declare @year char(4)
declare @StartDate datetime

set @month = substring(@InputParameterStart,1,2)
set @year = substring(@InputParameterStart, 4,4)
--print @month
--print @year

set @StartDate = cast(@year + '/' + @month + '/01' as datetime)
print @StartDate

Open in new window

Avatar of printmedia
printmedia

ASKER

Thanks k-designers. But when I try using cdate I get the following error: 'cdate' is not a recognized function name.

I'm using SQL Server 2000.
Avatar of printmedia
printmedia

ASKER

By the way I would be inserting that into a datetime field
ASKER CERTIFIED SOLUTION
Avatar of ralmada
ralmada
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo