[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Setting default value when creating table

Posted on 2015-02-04
5
Medium Priority
?
84 Views
Last Modified: 2015-02-04
I am trying to create a table and set a default value for one of the field to 'yyyymm'
the code I am using is

qry_mmyy varchar(6) default (CONVERT(VARCHAR(4),YEAR(getdate()) + CONVERT(VARCHAR(2),MONTH(getdate())))),

Open in new window


I am getting 2017 still though instead of 201502

any ideas would be welcome

thanks
0
Comment
Question by:damixa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40589563
-- This is the varchar
SELECT CONVERT(VARCHAR(4),YEAR(getdate())) + RIGHT('0' + CONVERT(VARCHAR(2),MONTH(getdate())),2)

The use of RIGHT('0'.. is required because February will evaluate to 2, and not 02, so to correct this add a zero to the left side and take the right-most two characters.

-- This is an int, assuming you'll want to do math on this value then any numeric is better
SELECT (CONVERT(VARCHAR(4),YEAR(getdate())) * 100) + CONVERT(VARCHAR(2),MONTH(getdate()))
0
 
LVL 66

Accepted Solution

by:
Jim Horn earned 2000 total points
ID: 40589566
Also, if this value will always derive from something else and never be edited, perhaps a calculated column would be better than a default, or always calculate it in whatever query uses it.
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 40589588
qry_mmyy varchar(6) default CONVERT(varchar(6), getdate(), 112)
0
 

Author Closing Comment

by:damixa
ID: 40589591
Thanks
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 40589597
Just to clarify for sure:
I assume you mean "computed column" rather than "calculated column"?
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question