• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 548
  • Last Modified:

Check format of field

Hi experts,

Is there a way I can check the format of the an nvarchar field? Meaning I want to make sure the text in this field is XX-XXXX, where X is a number 0-9.

Thanks.
0
MAVSS
Asked:
MAVSS
  • 3
  • 2
1 Solution
 
sarabhaiCommented:
Yes you can  used the constraints For this
0
 
MAVSSAuthor Commented:
Thanks, sarabhai but I should've clarified. I have to use a query as this will be displayed in a report.
0
 
MAVSSAuthor Commented:
Can the format of a field be checked using a SQL query?
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
lcohanDatabase AnalystCommented:
Here - try this:

select * from tablename where columnname like '%[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]%'
0
 
lcohanDatabase AnalystCommented:
sorry you had XX-XXXX - updated below:

create table #t1 (c1 text)
insert #t1 values ('12-1234')
insert #t1 values ('ab-1234')
select * from #t1 where c1 like '%[0-9][0-9]-[0-9][0-9][0-9][0-9]%'
0
 
MAVSSAuthor Commented:
Thanks. Used:
where c1 like '[0-9][0-9]-[0-9][0-9][0-9][0-9]' (Did not want to use %%)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now