0

I am using a MS Datepicker to capture a date and need to save it to my SQLdb as a sting in the following format: yyyymmdd. How do I convert the value from the DateTimePicker either in VB or in SQL?

example: 3/1/2018 should be 20180301 etc...

2
  • 1
    If it is a` DateTime, you should save it as a DateTime` so it acts like a DateTime and doesnt need the be converted when reading or writing. Commented Feb 23, 2018 at 17:27
  • I'm using a DB that was built years ago and don't have much flexibility to change it. The original designer did it this way and I am trying to work within the bounds that I have. Commented Feb 23, 2018 at 18:29

3 Answers 3

1

Storing dates as strings is always questionable, however, it you must

Example

Select convert(varchar(8),convert(date,'3/1/2018'),112)

or if 2012+

Select format(convert(date,'3/1/2018'),'yyyyMMdd')

Returns

20180301
Sign up to request clarification or add additional context in comments.

Comments

0

well C# is:

[DateTiemVar].ToString("yyyyMMdd") ;

I have to assume it is very siomilar in VB.

Comments

0

You can do like this, suppose cDate is you field name

SELECT STR_TO_DATE(cDate,'%m/%d/%Y %h:%i:%s %p') as cDate;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.