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 2012 Forums
 Transact-SQL (2012)
 Simple question.

Author  Topic 

NguyenL71
Posting Yak Master

228 Posts

Posted - 2015-03-26 : 12:01:20
Hi,

Is there a way to check if data in this format YYYYMM; Otherwise, raiser an ERROR.
If the user enter MMYYYY ex: 072014 wrong, should be 201407. I am using SQL 2012.

Thank you so much in advance.

DECLARE @SSISAccountingMonth CHAR(6) = '072014' --'201407'
DECLARE @SSISYear INT
,@SSISMonth TINYINT

--Raise an error. Must be yyyymm format.
SELECT @SSISAccountingMonth
------
072014

--Testing.
SET @SSISMonth = RIGHT(@SSISAccountingMonth, 2)
PRINT @SSISMonth

SET @SSISYear = LEFT(@SSISAccountingMonth, 4)
PRINT @SSISYear

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-03-26 : 12:05:26
if @SSISAccountingMonth not like '%[^0-9]%' or left(@SSISAccountingMonth, 1) <> '2' or RIGHT(@SSISAccountingMonth, 2) not between '01' and '12'
raiserror ...
Go to Top of Page
   

- Advertisement -