Showing posts with label mysql. Show all posts
Showing posts with label mysql. Show all posts

Sunday, December 14, 2014

Mysql date format and string parsing!!


Recently I worked on Mysql and need to work on date and time formats and faced one issue where I need to convert from long date format(e.g Fir Dec 12 00:00:00 IST 2014 ) to date format yyyy-mm-dd format. So for doing this, we need to parse the long date format for that I used concat, str_to_date and substring inbuilt Mysql methods.

substring in Mysql: Substring function used to get the substring from given string. For that we need to specify the ranges of the substring. Below is the example

Syntax:

substring(input string,substringStartingPos[,lenghtOfTheString])

Example:

select substring('Hello substring',4) gives 'lo substring' as it starts from 4th position to end of the string

select substring('Hello substring',4,4) gives 'lo s' as it starts from the 4th position and it takes 4 characters as length specified 4

select substring('Hello substring',-4) gives 'ring' as we specified the range four characters rom back(minus specifies from back).

Similarly I have taken the Day, Month and year from the long date pattern format as below.

select substring('Fir Dec 12 00:00:00 IST 2014',-4) gives '2014' which is a year
select substring('Fir Dec 12 00:00:00 IST 2014',4,4) gives 'Dec' which is a month
select substring('Fir Dec 12 00:00:00 IST 2014',9,2) gives '12' which is a day

Concat in Mysql: Concat function will combine more than one string into one string. Below is the syntax and example.

Syntax:

concat(str1,str2,str3,....)

Example:

select concat('Hello',' ', 'World!!') gives 'Hello World!!' as we passed three string to concat function.

Concat string function used to separate the strings with special characters as delimiters.


Coming to our problem of converting string to date format now as we know concat and substring functions. For that we need to use str_to_date inbuilt Mysql function.

str_to_date in Mysql: str_to_date function as name says, it converts string to date format. Its inverse of date_format() function which converts from date to string.


Syntax:

str_to_date(string,dateformat)


Example:
select str_to_date('Dec 12,2014', '%M %e,%Y') gives '2014-12-12' as %M is for month, %e as day and %Y is for Year. There are many formats for the date, we can check here for more.

And finally the resulted query for converting from long date format to yyyy-mm-dd is below.

select STR_TO_DATE(concat(substring('Fir Dec 12 00:00:00 IST 2014',4,7),',',substring('Fir Dec 12 00:00:00 IST 2014',-4)), '%M %e,%Y') as date

Which will give the date as '2014-12-12' which is required format.


Happy Coding!!


Saturday, March 22, 2014

function date_trunc("unknown", "unknown") is not unique - SQL


Recently I started working on Japser Studio professional for my new project Cloud to generate the reports. I was very new to all cloud technologies including PostgreSql. I faced lot of problems during this time. below is one of such problem, which kill lot of my time. Here is problem and its solution.

Problem statement: Need to generate usage data reports.

To generate usage data reports, I need to compare the existing timestamp from the table with user given data. For comparing data I used date_trunc() to get year/month/day etc. Here I was getting above error. Below is the query for that where ts is from test_reports table.

select * from test_reports where (date_trunc('day',ts)


ERROR: function date_trunc("unknown", "unknown") is not unique
Hint: Could not choose a best candidate function. You may need to add explicit type casts. [SQL State=42725]

This error is due to data type(timestamp) mismatching. By seeing/analysing the error, I found its due to mismatching the time. But no idea how to solve it and googled for it like hell. Luckily our TL has suggested the solution that prepending timestamp to the user input variable.

select * from test_reports where (date_trunc('day',ts)timestamp
'2014-03-21 14:07:00'))
Jasper studio professional:

Need to create the input parameter and pass that parameter as an argument to the sql query. Here are the steps to create the parameter.


  1. Go to outline window in the Jasper studio
  2. Right click on the Parameters tab and click on "Create Parameter"
  3. Then click on the created parameter and go to the properties window
  4. Change the name to appropriate one
  5. Change the class name to java.sql.timestamp
  6. Change the default expression to new Date() as it will give the current date as default value
  7. Make the isPrompt check box enable for getting the data from user
Now we can able to read the date value from the user into this newly created parameter.

Usage of the parameter in generating reports:

Need to follow below syntax for using parameter in the reports generation.

$P!{parameter_name}

select * from test_reports where ts < '$P!{user_date}'


If we use same parameter in date_trunc() function, we will face type mismatch error.

select * from test_report where date_trunc('day',update_ts)

To solve that error, you can prepend 'timestamp' which will convert to required type.

select * from test_report where date_trunc('day',update_ts)timestamp
'$P!{user_date}')

Thanks for reading !!!!



Subscribe to: Posts (Atom)

Popular Posts

AltStyle によって変換されたページ (->オリジナル) /