I have a datepicker on my form that I need to use as an integer value in the form yyyymmyy in an sql procedure. Can anyone tell me how to extract the datepicker value to an integer variable? Thanks

Dani AI

Generated

It looks like you really do want yyyymmyy (year+month+2-digit year), not the more typical yyyymmdd. ’s arithmetic produces that shape, but it is easy to misread and it will not zero-pad months; @serkan sendur’s string concat drops leading zeros for month/day. Safer is to format explicitly and then parse.

C# (WinForms DateTimePicker) with zero-padding and no locale surprises:

using System.Globalization;

// yyyymmyy (e.g., 2009-08-05 -> 20090809)
var dt = dateTimePicker1.Value;
int keyYy = int.Parse(dt.ToString("yyyyMMyy", CultureInfo.InvariantCulture));

// If you actually meant yyyymmdd, use:
int keyYmd = int.Parse(dt.ToString("yyyyMMdd", CultureInfo.InvariantCulture));

Prefer passing a real date into SQL Server and letting SQL handle any shape you need. That keeps your procedure index-friendly and avoids integer date bugs:

using (var cmd = new SqlCommand("dbo.YourProc", conn) { CommandType = CommandType.StoredProcedure })
{
    cmd.Parameters.Add("@d", SqlDbType.Date).Value = dateTimePicker1.Value.Date;
    cmd.ExecuteNonQuery();
}

If your stored proc truly requires the integer, you can derive it in T-SQL from a DATE parameter without relying on client-side math:

-- yyyymmyy
SELECT CAST(CONVERT(char(6), @d, 112) + RIGHT(CONVERT(char(4), @d, 112), 2) AS int);

-- yyyymmdd (common alternative)
SELECT CAST(CONVERT(char(8), @d, 112) AS int);

Quick checks:

  • Zero-padding: guaranteed by the format strings and CONVERT styles.
  • Range: both shapes fit comfortably in INT.
  • Sorting: integer yyyymmyy/yyyymmdd sort chronologically, but still store true DATE/DATETIME in tables and compute the integer only when needed.

Recommended Answers

All 3 Replies

YYYY*10000 + MM*100 + YYYY % 100 should do the tric.
YYYY being DateTimePicker.Value.year and MM being DateTimePicker.Value.month of course.

see below, i also attach the solution to this post

:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DatePickerToInteger
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
		{
			DateTime dt = dateTimePicker1.Value;
			string dtS = dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString();
			label1.Text = dtS;
			// you can just convert it to integer if there is a constraint
			int dtI = Convert.ToInt32(dtS);
		}
	}
}

:

namespace DatePickerToInteger
{
	partial class Form1
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.BackColor = System.Drawing.SystemColors.ButtonHighlight;
			this.label1.Location = new System.Drawing.Point(31, 38);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(100, 23);
			this.label1.TabIndex = 0;
			// 
			// dateTimePicker1
			// 
			this.dateTimePicker1.Location = new System.Drawing.Point(166, 41);
			this.dateTimePicker1.Name = "dateTimePicker1";
			this.dateTimePicker1.Size = new System.Drawing.Size(200, 20);
			this.dateTimePicker1.TabIndex = 1;
			this.dateTimePicker1.ValueChanged += new System.EventHandler(this.dateTimePicker1_ValueChanged);
			// 
			// Form1
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(493, 289);
			this.Controls.Add(this.dateTimePicker1);
			this.Controls.Add(this.label1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.ResumeLayout(false);

		}

		#endregion

		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.DateTimePicker dateTimePicker1;
	}
}

attachment is in this post

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.