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
 General SQL Server Forums
 New to SQL Server Programming
 SQL Query

Author  Topic 

edchamp
Starting Member

1 Post

Posted - 2008-02-28 : 15:25:13
I want to write a sql query for the following scenario. I have a date that is split into 3 fields (Yr, Mo, Dy). Each are decimal. I want to change all 3 fields to char and add a leading '0' (zero) if the month or day less than 10. For example:

select *
from dbo.anytable
where isdate(
convert(char(4), opm_yr) +
convert(char(2), opm_mo) +
convert(char(2), opm_dy)) = 0

When I run this query, invalid dates are showing up as '2001 8 7'. Since there is a space in the month and day fields, this causes a problem.

Thank you in advance.

Ed

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-02-28 : 15:59:01
select *
from dbo.anytable
where isdate(
right('0000' + cast(opm_yr as varchar(4)), 4) +
right('00' + cast(opm_mo) as varchar(2)), 2) +
right('00' + cast(opm_dy) as varchar(2)), 2)) = 0



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -