How to convert a string variable to a dateformat (yyyy_mm_dd)

Hi there are so many solutions to convert , but I would like specifically how I can preserve the format as date itself after converting to my required format. I have to perform SQL query, so I need the value in date format only and also specifically as yyyy_mm_dd. Can anyone help please?

Hi @Saranyajk ,

Where you want to preserve in data table or some variable ?

variable

Check below link

I guess the OP wants to preserve it to date format , not string @ermanoj3101.

1 Like

hi sorry may be I am not very clear, I dont want a string, I am using convert.todatetime , my question is how do I get or specify the format I want.

you are right, I want to specifically preserve it as date variable not string and also I need to format as yyyy-mm-dd

Got it.

You want to preserve it to match it with sql data.
Is it ?

How your query looks like, can you show that ?
May be we can convert both sql and passed value to get records from sql in that case we will not be required to preserve any value.

Can we do this or not ?

yyyy_mm_dd is not a date format. It’s a format you can use to print out a date. Datetime values don’t have a format. yyyy_mm_dd is string format.

1 Like

Date variables do not have a format. They are date variables. Like how integers don’t have a format, they’re numbers. If you need to provide the date to the SQL query as yyyy-mm-dd then you output it as string myDateVar.ToString(“yyyy-mm-dd”)

Thank you so much everyone for your help. We are trying to change the SQL format, hopefully that should help.